Skip to content

Commit

Permalink
imp(Usage Strings): improves the default usage string when only a sin…
Browse files Browse the repository at this point in the history
…gle positional arg is present

Instead of blindly printing `[ARGS]` when only a single positional arg is present, it will now
print `[NAME]` (or `[NAME]...` for multiple values allowed)

Closes #518
  • Loading branch information
kbknapp committed Jun 4, 2016
1 parent 17bc17c commit ec86f2d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,17 @@ impl<'a, 'b> Parser<'a, 'b>
}
if self.has_positionals() &&
self.positionals.values().any(|a| !a.settings.is_set(ArgSettings::Required)) {
usage.push_str(" [ARGS]");
if self.positionals.len() == 1 {
let p = self.positionals.values().next().expect(INTERNAL_ERROR_MSG);
if !self.groups.values().any(|g| g.args.iter().any(|a| a == &p.name)) {
usage.push_str(&*format!(" [{}]{}", p.name_no_brackets(),
p.multiple_str()));
} else {
usage.push_str(" [ARGS]");
}
} else {
usage.push_str(" [ARGS]");
}
}


Expand Down
20 changes: 20 additions & 0 deletions src/args/arg_builder/positional.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::{Display, Formatter, Result};
use std::result::Result as StdResult;
use std::rc::Rc;
use std::borrow::Cow;

use vec_map::VecMap;

Expand Down Expand Up @@ -108,6 +109,25 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
}
pb
}

pub fn multiple_str(&self) -> &str {
if self.settings.is_set(ArgSettings::Multiple) && self.val_names.is_none() {
"..."
} else {
""
}
}

pub fn name_no_brackets(&self) -> Cow<str> {
if let Some(ref names) = self.val_names {
Cow::Owned(names.values()
.map(|n| format!("<{}>", n))
.collect::<Vec<_>>()
.join(" "))
} else {
Cow::Borrowed(self.name)
}
}
}

impl<'n, 'e> Display for PosBuilder<'n, 'e> {
Expand Down

0 comments on commit ec86f2d

Please sign in to comment.