From 15c77150a97630b8286f3e8d6b0d508a8e21f9ee Mon Sep 17 00:00:00 2001 From: Donough Liu Date: Sat, 10 Oct 2020 23:11:42 +0800 Subject: [PATCH] Sort options base on option's short and long representation --- src/output/help.rs | 24 ++++++++++++++++++++--- tests/help.rs | 46 +++++++++++++++++++++++++++++++++++++++----- tests/hidden_args.rs | 24 +++++++++++------------ 3 files changed, 74 insertions(+), 20 deletions(-) diff --git a/src/output/help.rs b/src/output/help.rs index 684ca078..458327f6 100644 --- a/src/output/help.rs +++ b/src/output/help.rs @@ -210,9 +210,27 @@ impl<'help, 'app, 'parser, 'writer> Help<'help, 'app, 'parser, 'writer> { debug!("Help::write_args: New Longest...{}", self.longest); } let btm = ord_m.entry(arg.disp_ord).or_insert(BTreeMap::new()); - // We use name here for alphabetic sorting - // @TODO @maybe perhaps we could do some sort of ordering off of keys? - btm.insert(arg.name, arg); + + // Formatting key like this to ensure that: + // 1. Argument has long flags are printed just after short flags. + // 2. For two args both have short flags like `-c` and `-C`, the + // `-C` arg is printed just after the `-c` arg + // 3. For args without short or long flag, print them at last(sorted + // by arg name). + // Example order: -a, -b, -B, -s, --select-file, --select-folder, -x + + let key = if let Some(x) = arg.short { + let mut s = x.to_ascii_lowercase().to_string(); + s.push(if x.is_ascii_lowercase() { '0' } else { '1' }); + s + } else if let Some(x) = arg.long { + x.to_string() + } else { + let mut s = '{'.to_string(); + s.push_str(arg.name); + s + }; + btm.insert(key, arg); } let mut first = true; for btm in ord_m.values() { diff --git a/tests/help.rs b/tests/help.rs index 428d2f53..e18c1a15 100644 --- a/tests/help.rs +++ b/tests/help.rs @@ -35,13 +35,13 @@ FLAGS: -V, --version Prints version information OPTIONS: - -O, --Option specific vals [possible values: fast, slow] --long-option-2 tests long options with exclusions --maxvals3 ... Tests 3 max vals --minvals2 ... Tests 2 min vals --multvals Tests multiple values, not mult occs --multvalsmo Tests multiple values, and mult occs -o, --option ... tests options + -O, --Option specific vals [possible values: fast, slow] SUBCOMMANDS: help Prints this message or the help of the given subcommand(s) @@ -179,8 +179,8 @@ FLAGS: -V, --version Prints version information OPTIONS: - -O, --opt tests options - -o, --option