2166: Sort options base on option's short and long representation r=ldm0 a=ldm0



Co-authored-by: Donough Liu <ldm2993593805@163.com>
This commit is contained in:
bors[bot] 2020-10-10 18:54:49 +00:00 committed by GitHub
commit 4f90f3e4bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 20 deletions

View file

@ -210,9 +210,27 @@ impl<'help, 'app, 'parser, 'writer> Help<'help, 'app, 'parser, 'writer> {
debug!("Help::write_args: New Longest...{}", self.longest); debug!("Help::write_args: New Longest...{}", self.longest);
} }
let btm = ord_m.entry(arg.disp_ord).or_insert(BTreeMap::new()); 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? // Formatting key like this to ensure that:
btm.insert(arg.name, arg); // 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; let mut first = true;
for btm in ord_m.values() { for btm in ord_m.values() {

View file

@ -35,13 +35,13 @@ FLAGS:
-V, --version Prints version information -V, --version Prints version information
OPTIONS: OPTIONS:
-O, --Option <option3> specific vals [possible values: fast, slow]
--long-option-2 <option2> tests long options with exclusions --long-option-2 <option2> tests long options with exclusions
--maxvals3 <maxvals>... Tests 3 max vals --maxvals3 <maxvals>... Tests 3 max vals
--minvals2 <minvals>... Tests 2 min vals --minvals2 <minvals>... Tests 2 min vals
--multvals <one> <two> Tests multiple values, not mult occs --multvals <one> <two> Tests multiple values, not mult occs
--multvalsmo <one> <two> Tests multiple values, and mult occs --multvalsmo <one> <two> Tests multiple values, and mult occs
-o, --option <opt>... tests options -o, --option <opt>... tests options
-O, --Option <option3> specific vals [possible values: fast, slow]
SUBCOMMANDS: SUBCOMMANDS:
help Prints this message or the help of the given subcommand(s) help Prints this message or the help of the given subcommand(s)
@ -179,8 +179,8 @@ FLAGS:
-V, --version Prints version information -V, --version Prints version information
OPTIONS: OPTIONS:
-O, --opt <opt> tests options -o, --option <option>... tests options
-o, --option <option>... tests options"; -O, --opt <opt> tests options";
static RIPGREP_USAGE: &str = "ripgrep 0.5 static RIPGREP_USAGE: &str = "ripgrep 0.5
@ -390,8 +390,8 @@ USAGE:
conflict [FLAGS] conflict [FLAGS]
FLAGS: FLAGS:
--help Prints help information
-h -h
--help Prints help information
-V, --version Prints version information"; -V, --version Prints version information";
static LAST_ARG: &str = "last 0.1 static LAST_ARG: &str = "last 0.1
@ -614,6 +614,22 @@ FLAGS:
-h, --help Prints help information -h, --help Prints help information
-V, --version Prints version information"; -V, --version Prints version information";
static OPTION_USAGE_ORDER: &str = "order
USAGE:
order [FLAGS]
FLAGS:
-a
-b
-B
-h, --help Prints help information
-s
--select_file
--select_folder
-V, --version Prints version information
-x";
fn setup() -> App<'static> { fn setup() -> App<'static> {
App::new("test") App::new("test")
.author("Kevin K.") .author("Kevin K.")
@ -1587,8 +1603,8 @@ OPTIONS:
-s, --speed <SPEED> How fast? [possible values: fast, slow] -s, --speed <SPEED> How fast? [possible values: fast, slow]
NETWORKING: NETWORKING:
-n, --no-proxy Do not use system proxy settings
-a, --server-addr Set server address -a, --server-addr Set server address
-n, --no-proxy Do not use system proxy settings
SPECIAL: SPECIAL:
-b, --birthday-song <song> Change which song is played for birthdays -b, --birthday-song <song> Change which song is played for birthdays
@ -2159,3 +2175,23 @@ fn version_about_multi_subcmd_override_long_flag() {
false false
)); ));
} }
#[test]
fn option_usage_order() {
let app = App::new("order").args(&[
Arg::new("a").short('a'),
Arg::new("B").short('B'),
Arg::new("b").short('b'),
Arg::new("save").short('s'),
Arg::new("select_file").long("select_file"),
Arg::new("select_folder").long("select_folder"),
Arg::new("x").short('x'),
]);
assert!(utils::compare_output(
app,
"order --help",
OPTION_USAGE_ORDER,
false
));
}

View file

@ -46,8 +46,8 @@ USAGE:
FLAGS: FLAGS:
-h, --help Prints help information -h, --help Prints help information
-V, --version Prints version information -v, --visible This text should be visible
-v, --visible This text should be visible"; -V, --version Prints version information";
static HIDDEN_SHORT_ARGS_LONG_HELP: &str = "test 2.31.2 static HIDDEN_SHORT_ARGS_LONG_HELP: &str = "test 2.31.2
Steve P. Steve P.
@ -63,11 +63,11 @@ FLAGS:
-h, --help -h, --help
Prints help information Prints help information
-V, --version
Prints version information
-v, --visible -v, --visible
This text should be visible"; This text should be visible
-V, --version
Prints version information";
/// Ensure hidden with short option /// Ensure hidden with short option
#[test] #[test]
@ -134,11 +134,11 @@ FLAGS:
-h, --help -h, --help
Prints help information Prints help information
-V, --version
Prints version information
-v, --visible -v, --visible
This text should be visible"; This text should be visible
-V, --version
Prints version information";
#[test] #[test]
fn hidden_long_args() { fn hidden_long_args() {
@ -176,8 +176,8 @@ USAGE:
FLAGS: FLAGS:
-c, --config Some help text describing the --config arg -c, --config Some help text describing the --config arg
-h, --help Prints help information -h, --help Prints help information
-V, --version Prints version information -v, --visible This text should be visible
-v, --visible This text should be visible"; -V, --version Prints version information";
#[test] #[test]
fn hidden_long_args_short_help() { fn hidden_long_args_short_help() {