* [pbs-devel] [PATCH proxmox] router: sort cli properties in usage output
@ 2024-08-09 8:25 Dominik Csapak
2024-08-30 11:33 ` [pbs-devel] applied: " Wolfgang Bumiller
0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2024-08-09 8:25 UTC (permalink / raw)
To: pbs-devel
If we don't do this, then properties from a serde flattened struct will
be positioned at the end of the list, rather than properly sorted with
the other properties.
Since the tests also feature non-sorted properties, we have to adapt
them too.
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
i wanted to add tests for the serde flattened structs, but the test
don't use the api macro, nor serde, so i was not sure how i could
replicate that. If somebody has an idea for that, please tell, then
I'll send a v2 or follow up
proxmox-router/src/cli/format.rs | 20 ++++++++++++-----
proxmox-router/tests/docs.rs | 38 ++++++++++++++++----------------
2 files changed, 33 insertions(+), 25 deletions(-)
diff --git a/proxmox-router/src/cli/format.rs b/proxmox-router/src/cli/format.rs
index 6f0ec50c..a35821a6 100644
--- a/proxmox-router/src/cli/format.rs
+++ b/proxmox-router/src/cli/format.rs
@@ -139,7 +139,10 @@ pub(crate) fn generate_usage_str_do<'cli>(
let mut options = String::new();
- for (prop, optional, param_schema) in schema.properties() {
+ let mut properties: Vec<_> = schema.properties().collect();
+ properties.sort_by(|a, b| a.0.cmp(b.0));
+
+ for (prop, optional, param_schema) in properties {
if done_hash.contains(prop) {
continue;
}
@@ -212,9 +215,12 @@ pub(crate) fn generate_usage_str_do<'cli>(
let mut global_options = String::new();
- for (name, _optional, param_schema) in
- global_options_iter.flat_map(|o| o.schema.any_object().unwrap().properties())
- {
+ let mut properties: Vec<_> = global_options_iter
+ .flat_map(|o| o.schema.any_object().unwrap().properties())
+ .collect();
+ properties.sort_by(|a, b| a.0.cmp(b.0));
+
+ for (name, _optional, param_schema) in properties {
if done_hash.contains(name) {
continue;
}
@@ -325,12 +331,14 @@ impl<'cli> UsageState<'cli> {
let mut out = String::new();
let _ = write!(out, "Options available for command group ``{prefix}``:");
for opt in opts {
- for (name, _optional, schema) in opt
+ let mut properties: Vec<_> = opt
.schema
.any_object()
.expect("non-object schema in global optiosn")
.properties()
- {
+ .collect();
+ properties.sort_by(|a, b| a.0.cmp(b.0));
+ for (name, _optional, schema) in properties {
let _ = write!(
out,
"\n\n{}",
diff --git a/proxmox-router/tests/docs.rs b/proxmox-router/tests/docs.rs
index f23a872a..74bc99b6 100644
--- a/proxmox-router/tests/docs.rs
+++ b/proxmox-router/tests/docs.rs
@@ -109,26 +109,26 @@ fn expected_toplevel_help_text() -> &'static str {
Usage:
clicmd help [{<command>}] [OPTIONS]
-clicmd l0c1 --required-arg <string> --another-required-arg <string> [OPTIONS]
+clicmd l0c1 --another-required-arg <string> --required-arg <string> [OPTIONS]
clicmd l0c2 <required-arg> --another-required-arg <string> [OPTIONS]
-clicmd l0sub l1c1 --required-arg <string> --another-required-arg <string> [OPTIONS]
-clicmd l0sub l1c2 --required-arg <string> --another-required-arg <string> [OPTIONS]
+clicmd l0sub l1c1 --another-required-arg <string> --required-arg <string> [OPTIONS]
+clicmd l0sub l1c2 --another-required-arg <string> --required-arg <string> [OPTIONS]
"##
.trim_start()
}
fn expected_group_help_text() -> &'static str {
r##"
-Usage: clicmd l0sub l1c1 --required-arg <string> --another-required-arg <string> [OPTIONS]
+Usage: clicmd l0sub l1c1 --another-required-arg <string> --required-arg <string> [OPTIONS]
Simple API method with one required and one optional argument.
- --required-arg <string>
- Required string argument.
-
--another-required-arg <string>
A second required string argument.
+ --required-arg <string>
+ Required string argument.
+
Optional parameters:
--optional-arg <boolean> (default=false)
@@ -161,16 +161,16 @@ Optional parameters:
----
-``clicmd l0c1 --required-arg <string> --another-required-arg <string> [OPTIONS]``
+``clicmd l0c1 --another-required-arg <string> --required-arg <string> [OPTIONS]``
Simple API method with one required and one optional argument.
-``--required-arg`` ``<string>``
- Required string argument.
-
``--another-required-arg`` ``<string>``
A second required string argument.
+``--required-arg`` ``<string>``
+ Required string argument.
+
Optional parameters:
``--optional-arg`` ``<boolean> (default=false)``
@@ -205,16 +205,16 @@ Options available for command group ``clicmd l0sub``:
----
-``clicmd l0sub l1c1 --required-arg <string> --another-required-arg <string> [OPTIONS]``
+``clicmd l0sub l1c1 --another-required-arg <string> --required-arg <string> [OPTIONS]``
Simple API method with one required and one optional argument.
-``--required-arg`` ``<string>``
- Required string argument.
-
``--another-required-arg`` ``<string>``
A second required string argument.
+``--required-arg`` ``<string>``
+ Required string argument.
+
Optional parameters:
``--optional-arg`` ``<boolean> (default=false)``
@@ -228,16 +228,16 @@ Inherited group parameters:
----
-``clicmd l0sub l1c2 --required-arg <string> --another-required-arg <string> [OPTIONS]``
+``clicmd l0sub l1c2 --another-required-arg <string> --required-arg <string> [OPTIONS]``
Simple API method with one required and one optional argument.
-``--required-arg`` ``<string>``
- Required string argument.
-
``--another-required-arg`` ``<string>``
A second required string argument.
+``--required-arg`` ``<string>``
+ Required string argument.
+
Optional parameters:
``--optional-arg`` ``<boolean> (default=false)``
--
2.39.2
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
^ permalink raw reply [flat|nested] 2+ messages in thread
* [pbs-devel] applied: [PATCH proxmox] router: sort cli properties in usage output
2024-08-09 8:25 [pbs-devel] [PATCH proxmox] router: sort cli properties in usage output Dominik Csapak
@ 2024-08-30 11:33 ` Wolfgang Bumiller
0 siblings, 0 replies; 2+ messages in thread
From: Wolfgang Bumiller @ 2024-08-30 11:33 UTC (permalink / raw)
To: Dominik Csapak; +Cc: pbs-devel
On Fri, Aug 09, 2024 at 10:25:25AM GMT, Dominik Csapak wrote:
> If we don't do this, then properties from a serde flattened struct will
> be positioned at the end of the list, rather than properly sorted with
> the other properties.
>
> Since the tests also feature non-sorted properties, we have to adapt
> them too.
>
> Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
> ---
> i wanted to add tests for the serde flattened structs, but the test
> don't use the api macro, nor serde, so i was not sure how i could
> replicate that. If somebody has an idea for that, please tell, then
> I'll send a v2 or follow up
Those are just `AllOfSchemas` , you'd just have to make 2
`ObjectSchemas` and put them into an `AllOfSchema`.
I do wonder, though, whether we want some way to declare "groups" for
the documentation.
Using a flattened structs might sometimes work, but probably not all the
time. With this applied, flattened structs won't make a difference. But
given that the visible portion of the struct in code doesn't represent
the way it would be grouped anyway, we'd need a separate solution to
declare groups anyway.
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-08-30 11:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-09 8:25 [pbs-devel] [PATCH proxmox] router: sort cli properties in usage output Dominik Csapak
2024-08-30 11:33 ` [pbs-devel] applied: " Wolfgang Bumiller
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal