fix(usage): Move positional args to end of usage string (#2492)

* Move positional args to end of USAGE

* Remove -- from usage string
This commit is contained in:
Ajeet D'Souza 2021-05-21 00:29:21 +05:30 committed by GitHub
parent abe2373cca
commit 460459c771
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 30 deletions

View file

@ -408,23 +408,6 @@ impl<'help, 'app, 'parser> Usage<'help, 'app, 'parser> {
.flat_map(|g| self.p.app.unroll_args_in_group(&g.id))
.collect::<Vec<_>>();
let pmap = unrolled_reqs
.iter()
.chain(incls.iter())
.filter(|a| self.p.app.get_positionals().any(|p| &&p.id == a))
.filter(|&pos| matcher.map_or(true, |m| !m.contains(pos)))
.filter_map(|pos| self.p.app.find(pos))
.filter(|&pos| incl_last || !pos.is_set(ArgSettings::Last))
.filter(|pos| !args_in_groups.contains(&pos.id))
.map(|pos| (pos.index.unwrap(), pos))
.collect::<BTreeMap<usize, &Arg>>(); // sort by index
for p in pmap.values() {
debug!("Usage::get_required_usage_from:iter:{:?}", p.id);
if !args_in_groups.contains(&p.id) {
ret_val.push(p.to_string());
}
}
for a in unrolled_reqs
.iter()
.chain(incls.iter())
@ -434,12 +417,7 @@ impl<'help, 'app, 'parser> Usage<'help, 'app, 'parser> {
.filter(|name| !(matcher.is_some() && matcher.as_ref().unwrap().contains(name)))
{
debug!("Usage::get_required_usage_from:iter:{:?}", a);
let arg = self
.p
.app
.find(&a)
.map(ToString::to_string)
.expect(INTERNAL_ERROR_MSG);
let arg = self.p.app.find(&a).expect(INTERNAL_ERROR_MSG).to_string();
ret_val.push(arg);
}
let mut g_vec: Vec<String> = vec![];
@ -465,8 +443,24 @@ impl<'help, 'app, 'parser> Usage<'help, 'app, 'parser> {
g_vec.push(elem);
}
}
for g in g_vec {
ret_val.push(g);
ret_val.extend_from_slice(&g_vec);
let pmap = unrolled_reqs
.iter()
.chain(incls.iter())
.filter(|a| self.p.app.get_positionals().any(|p| &&p.id == a))
.filter(|&pos| matcher.map_or(true, |m| !m.contains(pos)))
.filter_map(|pos| self.p.app.find(pos))
.filter(|&pos| incl_last || !pos.is_set(ArgSettings::Last))
.filter(|pos| !args_in_groups.contains(&pos.id))
.map(|pos| (pos.index.unwrap(), pos))
.collect::<BTreeMap<usize, &Arg>>(); // sort by index
for p in pmap.values() {
debug!("Usage::get_required_usage_from:iter:{:?}", p.id);
if !args_in_groups.contains(&p.id) {
ret_val.push(p.to_string());
}
}
debug!("Usage::get_required_usage_from: ret_val={:?}", ret_val);

View file

@ -5,14 +5,14 @@ use clap::{App, Arg, ArgGroup, ErrorKind};
static CONFLICT_ERR: &str = "error: The argument '-F' cannot be used with '--flag'
USAGE:
clap-test <positional> <positional2> --flag --long-option-2 <option2>
clap-test --flag --long-option-2 <option2> <positional> <positional2>
For more information try --help";
static CONFLICT_ERR_REV: &str = "error: The argument '--flag' cannot be used with '-F'
USAGE:
clap-test <positional> <positional2> -F --long-option-2 <option2>
clap-test -F --long-option-2 <option2> <positional> <positional2>
For more information try --help";

View file

@ -28,11 +28,11 @@ USAGE:
For more information try --help";
static MISSING_REQ: &str = "error: The following required arguments were not provided:
<positional2>
--long-option-2 <option2>
<positional2>
USAGE:
clap-test <positional2> --long-option-2 <option2> -F
clap-test --long-option-2 <option2> -F <positional2>
For more information try --help";
@ -913,7 +913,7 @@ static ISSUE_1158: &str = "error: The following required arguments were not prov
-z <Z>
USAGE:
example <ID> -x <X> -y <Y> -z <Z>
example -x <X> -y <Y> -z <Z> <ID>
For more information try --help";