router: sort cli properties in usage output

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>
This commit is contained in:
Dominik Csapak 2024-08-09 10:25:25 +02:00 committed by Wolfgang Bumiller
parent d72ea40c80
commit 237b317678
2 changed files with 33 additions and 25 deletions

View File

@ -139,7 +139,10 @@ pub(crate) fn generate_usage_str_do<'cli>(
let mut options = String::new(); 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) { if done_hash.contains(prop) {
continue; continue;
} }
@ -212,9 +215,12 @@ pub(crate) fn generate_usage_str_do<'cli>(
let mut global_options = String::new(); let mut global_options = String::new();
for (name, _optional, param_schema) in let mut properties: Vec<_> = global_options_iter
global_options_iter.flat_map(|o| o.schema.any_object().unwrap().properties()) .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) { if done_hash.contains(name) {
continue; continue;
} }
@ -325,12 +331,14 @@ impl<'cli> UsageState<'cli> {
let mut out = String::new(); let mut out = String::new();
let _ = write!(out, "Options available for command group ``{prefix}``:"); let _ = write!(out, "Options available for command group ``{prefix}``:");
for opt in opts { for opt in opts {
for (name, _optional, schema) in opt let mut properties: Vec<_> = opt
.schema .schema
.any_object() .any_object()
.expect("non-object schema in global optiosn") .expect("non-object schema in global optiosn")
.properties() .properties()
{ .collect();
properties.sort_by(|a, b| a.0.cmp(b.0));
for (name, _optional, schema) in properties {
let _ = write!( let _ = write!(
out, out,
"\n\n{}", "\n\n{}",

View File

@ -109,26 +109,26 @@ fn expected_toplevel_help_text() -> &'static str {
Usage: Usage:
clicmd help [{<command>}] [OPTIONS] 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 l0c2 <required-arg> --another-required-arg <string> [OPTIONS]
clicmd l0sub l1c1 --required-arg <string> --another-required-arg <string> [OPTIONS] clicmd l0sub l1c1 --another-required-arg <string> --required-arg <string> [OPTIONS]
clicmd l0sub l1c2 --required-arg <string> --another-required-arg <string> [OPTIONS] clicmd l0sub l1c2 --another-required-arg <string> --required-arg <string> [OPTIONS]
"## "##
.trim_start() .trim_start()
} }
fn expected_group_help_text() -> &'static str { fn expected_group_help_text() -> &'static str {
r##" 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. Simple API method with one required and one optional argument.
--required-arg <string>
Required string argument.
--another-required-arg <string> --another-required-arg <string>
A second required string argument. A second required string argument.
--required-arg <string>
Required string argument.
Optional parameters: Optional parameters:
--optional-arg <boolean> (default=false) --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. Simple API method with one required and one optional argument.
``--required-arg`` ``<string>``
Required string argument.
``--another-required-arg`` ``<string>`` ``--another-required-arg`` ``<string>``
A second required string argument. A second required string argument.
``--required-arg`` ``<string>``
Required string argument.
Optional parameters: Optional parameters:
``--optional-arg`` ``<boolean> (default=false)`` ``--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. Simple API method with one required and one optional argument.
``--required-arg`` ``<string>``
Required string argument.
``--another-required-arg`` ``<string>`` ``--another-required-arg`` ``<string>``
A second required string argument. A second required string argument.
``--required-arg`` ``<string>``
Required string argument.
Optional parameters: Optional parameters:
``--optional-arg`` ``<boolean> (default=false)`` ``--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. Simple API method with one required and one optional argument.
``--required-arg`` ``<string>``
Required string argument.
``--another-required-arg`` ``<string>`` ``--another-required-arg`` ``<string>``
A second required string argument. A second required string argument.
``--required-arg`` ``<string>``
Required string argument.
Optional parameters: Optional parameters:
``--optional-arg`` ``<boolean> (default=false)`` ``--optional-arg`` ``<boolean> (default=false)``