fix(AppSettings): fixes bug where subcmds didn't receive parent ver

Subcommands now receive the parent version when the setting
AppSettings::GlobalVersion has been set.
This commit is contained in:
Kevin K 2016-02-04 11:54:46 -05:00
parent 990ddfbc9a
commit a62e452754
2 changed files with 67 additions and 56 deletions

View file

@ -50,7 +50,11 @@ use errors::Result as ClapResult;
/// // Your program logic starts here... /// // Your program logic starts here...
/// ``` /// ```
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct App<'a, 'b>(Parser<'a, 'b>) where 'a: 'b; pub struct App<'a, 'b> where 'a: 'b {
#[doc(hidden)]
pub p: Parser<'a, 'b>
}
impl<'a, 'b> App<'a, 'b> { impl<'a, 'b> App<'a, 'b> {
/// Creates a new instance of an application requiring a name. The name may be, but doesn't /// Creates a new instance of an application requiring a name. The name may be, but doesn't
@ -64,7 +68,7 @@ impl<'a, 'b> App<'a, 'b> {
/// let prog = App::new("My Program") /// let prog = App::new("My Program")
/// # ; /// # ;
/// ``` /// ```
pub fn new<S: Into<String>>(n: S) -> Self { App(Parser::with_name(n.into())) } pub fn new<S: Into<String>>(n: S) -> Self { App { p: Parser::with_name(n.into()) } }
/// Creates a new instace of `App` from a .yml (YAML) file. A full example of supported YAML /// Creates a new instace of `App` from a .yml (YAML) file. A full example of supported YAML
/// objects can be found in `examples/17_yaml.rs` and `examples/17_yaml.yml`. One great use for /// objects can be found in `examples/17_yaml.rs` and `examples/17_yaml.yml`. One great use for
@ -114,7 +118,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn author<S: Into<&'b str>>(mut self, author: S) -> Self { pub fn author<S: Into<&'b str>>(mut self, author: S) -> Self {
self.0.meta.author = Some(author.into()); self.p.meta.author = Some(author.into());
self self
} }
@ -136,7 +140,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn bin_name<S: Into<String>>(mut self, name: S) -> Self { pub fn bin_name<S: Into<String>>(mut self, name: S) -> Self {
self.0.meta.bin_name = Some(name.into()); self.p.meta.bin_name = Some(name.into());
self self
} }
@ -152,7 +156,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn about<S: Into<&'b str>>(mut self, about: S) -> Self { pub fn about<S: Into<&'b str>>(mut self, about: S) -> Self {
self.0.meta.about = Some(about.into()); self.p.meta.about = Some(about.into());
self self
} }
@ -169,7 +173,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn after_help<S: Into<&'b str>>(mut self, help: S) -> Self { pub fn after_help<S: Into<&'b str>>(mut self, help: S) -> Self {
self.0.meta.more_help = Some(help.into()); self.p.meta.more_help = Some(help.into());
self self
} }
@ -189,7 +193,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn version<S: Into<&'b str>>(mut self, ver: S) -> Self { pub fn version<S: Into<&'b str>>(mut self, ver: S) -> Self {
self.0.meta.version = Some(ver.into()); self.p.meta.version = Some(ver.into());
self self
} }
@ -217,7 +221,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn usage<S: Into<&'b str>>(mut self, usage: S) -> Self { pub fn usage<S: Into<&'b str>>(mut self, usage: S) -> Self {
self.0.meta.usage_str = Some(usage.into()); self.p.meta.usage_str = Some(usage.into());
self self
} }
@ -255,7 +259,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn help<S: Into<&'b str>>(mut self, help: S) -> Self { pub fn help<S: Into<&'b str>>(mut self, help: S) -> Self {
self.0.meta.help_str = Some(help.into()); self.p.meta.help_str = Some(help.into());
self self
} }
@ -277,7 +281,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn help_short<S: AsRef<str> + 'b>(mut self, s: S) -> Self { pub fn help_short<S: AsRef<str> + 'b>(mut self, s: S) -> Self {
self.0.help_short(s.as_ref()); self.p.help_short(s.as_ref());
self self
} }
@ -299,7 +303,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn version_short<S: AsRef<str>>(mut self, s: S) -> Self { pub fn version_short<S: AsRef<str>>(mut self, s: S) -> Self {
self.0.version_short(s.as_ref()); self.p.version_short(s.as_ref());
self self
} }
@ -317,7 +321,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn setting(mut self, setting: AppSettings) -> Self { pub fn setting(mut self, setting: AppSettings) -> Self {
self.0.set(setting); self.p.set(setting);
self self
} }
@ -336,7 +340,7 @@ impl<'a, 'b> App<'a, 'b> {
/// ``` /// ```
pub fn settings(mut self, settings: &[AppSettings]) -> Self { pub fn settings(mut self, settings: &[AppSettings]) -> Self {
for s in settings { for s in settings {
self.0.set(*s); self.p.set(*s);
} }
self self
} }
@ -362,7 +366,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn arg<A: Borrow<Arg<'a, 'b>> + 'a>(mut self, a: A) -> Self { pub fn arg<A: Borrow<Arg<'a, 'b>> + 'a>(mut self, a: A) -> Self {
self.0.add_arg(a.borrow()); self.p.add_arg(a.borrow());
self self
} }
@ -381,7 +385,7 @@ impl<'a, 'b> App<'a, 'b> {
/// ``` /// ```
pub fn args(mut self, args: &[Arg<'a, 'b>]) -> Self { pub fn args(mut self, args: &[Arg<'a, 'b>]) -> Self {
for arg in args { for arg in args {
self.0.add_arg(arg); self.p.add_arg(arg);
} }
self self
} }
@ -401,7 +405,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn arg_from_usage(mut self, usage: &'a str) -> Self { pub fn arg_from_usage(mut self, usage: &'a str) -> Self {
self.0.add_arg(&Arg::from_usage(usage)); self.p.add_arg(&Arg::from_usage(usage));
self self
} }
@ -426,7 +430,7 @@ impl<'a, 'b> App<'a, 'b> {
pub fn args_from_usage(mut self, usage: &'a str) -> Self { pub fn args_from_usage(mut self, usage: &'a str) -> Self {
for l in usage.lines() { for l in usage.lines() {
if l.len() == 0 { continue; } if l.len() == 0 { continue; }
self.0.add_arg(&Arg::from_usage(l.trim())); self.p.add_arg(&Arg::from_usage(l.trim()));
} }
self self
} }
@ -464,7 +468,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn group(mut self, group: ArgGroup<'a>) -> Self { pub fn group(mut self, group: ArgGroup<'a>) -> Self {
self.0.add_group(group); self.p.add_group(group);
self self
} }
@ -514,7 +518,7 @@ impl<'a, 'b> App<'a, 'b> {
/// # ; /// # ;
/// ``` /// ```
pub fn subcommand(mut self, subcmd: App<'a, 'b>) -> Self { pub fn subcommand(mut self, subcmd: App<'a, 'b>) -> Self {
self.0.add_subcommand(subcmd); self.p.add_subcommand(subcmd);
self self
} }
@ -536,7 +540,7 @@ impl<'a, 'b> App<'a, 'b> {
where I: IntoIterator<Item = App<'a, 'b>> where I: IntoIterator<Item = App<'a, 'b>>
{ {
for subcmd in subcmds.into_iter() { for subcmd in subcmds.into_iter() {
self.0.add_subcommand(subcmd); self.p.add_subcommand(subcmd);
} }
self self
} }
@ -569,7 +573,7 @@ impl<'a, 'b> App<'a, 'b> {
/// app.write_help(&mut out).ok().expect("failed to write to stdout"); /// app.write_help(&mut out).ok().expect("failed to write to stdout");
/// ``` /// ```
pub fn write_help<W: Write>(&self, w: &mut W) -> ClapResult<()> { pub fn write_help<W: Write>(&self, w: &mut W) -> ClapResult<()> {
self.0.write_help(w) self.p.write_help(w)
} }
/// Starts the parsing process, upon a failed parse an error will be displayed to the user and /// Starts the parsing process, upon a failed parse an error will be displayed to the user and
@ -690,10 +694,10 @@ impl<'a, 'b> App<'a, 'b> {
T: Into<OsString> T: Into<OsString>
{ {
// Verify all positional assertions pass // Verify all positional assertions pass
self.0.verify_positionals(); self.p.verify_positionals();
// If there are global arguments, we need to propgate them down to subcommands // If there are global arguments, we need to propgate them down to subcommands
// before parsing incase we run into a subcommand // before parsing incase we run into a subcommand
self.0.propogate_globals(); self.p.propogate_globals();
let mut matcher = ArgMatcher::new(); let mut matcher = ArgMatcher::new();
@ -705,14 +709,14 @@ impl<'a, 'b> App<'a, 'b> {
// will have two arguments, './target/release/my_prog', '-a' but we don't want // will have two arguments, './target/release/my_prog', '-a' but we don't want
// to display // to display
// the full path when displaying help messages and such // the full path when displaying help messages and such
if !self.0.is_set(AppSettings::NoBinaryName) { if !self.p.is_set(AppSettings::NoBinaryName) {
if let Some(name) = it.next() { if let Some(name) = it.next() {
let bn_os = name.into(); let bn_os = name.into();
let p = Path::new(&*bn_os); let p = Path::new(&*bn_os);
if let Some(f) = p.file_name() { if let Some(f) = p.file_name() {
if let Some(s) = f.to_os_string().to_str() { if let Some(s) = f.to_os_string().to_str() {
if let None = self.0.meta.bin_name { if let None = self.p.meta.bin_name {
self.0.meta.bin_name = Some(s.to_owned()); self.p.meta.bin_name = Some(s.to_owned());
} }
} }
} }
@ -720,7 +724,7 @@ impl<'a, 'b> App<'a, 'b> {
} }
// do the real parsing // do the real parsing
if let Err(e) = self.0.get_matches_with(&mut matcher, &mut it) { if let Err(e) = self.p.get_matches_with(&mut matcher, &mut it) {
return Err(e); return Err(e);
} }
@ -732,7 +736,7 @@ impl<'a, 'b> App<'a, 'b> {
fn maybe_wait_for_exit(&self, e: Error) -> ! { fn maybe_wait_for_exit(&self, e: Error) -> ! {
if e.use_stderr() { if e.use_stderr() {
wlnerr!("{}", e.message); wlnerr!("{}", e.message);
if self.0.is_set(AppSettings::WaitOnError) { if self.p.is_set(AppSettings::WaitOnError) {
wlnerr!("\nPress [ENTER] / [RETURN] to continue..."); wlnerr!("\nPress [ENTER] / [RETURN] to continue...");
let mut s = String::new(); let mut s = String::new();
let i = io::stdin(); let i = io::stdin();

View file

@ -37,14 +37,14 @@ pub struct Parser<'a, 'b> where 'a: 'b {
// A list of positional arguments // A list of positional arguments
positionals: VecMap<PosBuilder<'a, 'b>>, positionals: VecMap<PosBuilder<'a, 'b>>,
// A list of subcommands // A list of subcommands
subcommands: Vec<App<'a, 'b>>, #[doc(hidden)]
pub subcommands: Vec<App<'a, 'b>>,
groups: HashMap<&'a str, ArgGroup<'a>>, groups: HashMap<&'a str, ArgGroup<'a>>,
global_args: Vec<Arg<'a, 'b>>, global_args: Vec<Arg<'a, 'b>>,
overrides: Vec<&'b str>, overrides: Vec<&'b str>,
help_short: Option<char>, help_short: Option<char>,
version_short: Option<char>, version_short: Option<char>,
settings: AppFlags, settings: AppFlags,
version: Option<&'b str>,
pub meta: AppMeta<'b>, pub meta: AppMeta<'b>,
} }
@ -65,7 +65,6 @@ impl<'a, 'b> Default for Parser<'a, 'b> {
global_args: vec![], global_args: vec![],
overrides: vec![], overrides: vec![],
settings: AppFlags::new(), settings: AppFlags::new(),
version: None,
meta: AppMeta::new(), meta: AppMeta::new(),
} }
} }
@ -192,16 +191,24 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
} }
pub fn add_subcommand(&mut self, mut subcmd: App<'a, 'b>) { pub fn add_subcommand(&mut self, mut subcmd: App<'a, 'b>) {
if subcmd.0.meta.name == "help" { debugln!("fn=Parser::add_subcommand;");
debug!("Is help...");
if subcmd.p.meta.name == "help" {
sdebugln!("Yes");
self.settings.set(AppSettings::NeedsSubcommandHelp); self.settings.set(AppSettings::NeedsSubcommandHelp);
} } else { sdebugln!("No"); }
debug!("Using Setting VersionlessSubcommands...");
if self.settings.is_set(AppSettings::VersionlessSubcommands) { if self.settings.is_set(AppSettings::VersionlessSubcommands) {
subcmd.0.settings.set(AppSettings::DisableVersion); sdebugln!("Yes");
} subcmd.p.settings.set(AppSettings::DisableVersion);
if self.settings.is_set(AppSettings::GlobalVersion) && subcmd.0.meta.version.is_none() && } else { sdebugln!("No"); }
self.version.is_some() { debug!("Using Setting GlobalVersion...");
subcmd.0.meta.version = Some(self.version.unwrap()); if self.settings.is_set(AppSettings::GlobalVersion) && subcmd.p.meta.version.is_none() &&
} self.meta.version.is_some() {
sdebugln!("Yes");
subcmd = subcmd.setting(AppSettings::GlobalVersion)
.version(self.meta.version.unwrap());
} else { sdebugln!("No"); }
self.subcommands.push(subcmd); self.subcommands.push(subcmd);
} }
@ -386,10 +393,10 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
// done and to recursively call this method // done and to recursively call this method
{ {
for a in &self.global_args { for a in &self.global_args {
sc.0.add_arg(a); sc.p.add_arg(a);
} }
} }
sc.0.propogate_globals(); sc.p.propogate_globals();
} }
} }
@ -427,7 +434,7 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
// Has the user already passed '--'? // Has the user already passed '--'?
if !pos_only { if !pos_only {
let pos_sc = self.subcommands.iter().any(|s| &s.0.meta.name[..] == &*arg_os); let pos_sc = self.subcommands.iter().any(|s| &s.p.meta.name[..] == &*arg_os);
if (!starts_new_arg || self.is_set(AppSettings::AllowLeadingHyphen)) && !pos_sc { if (!starts_new_arg || self.is_set(AppSettings::AllowLeadingHyphen)) && !pos_sc {
// Check to see if parsing a value from an option // Check to see if parsing a value from an option
if let Some(nvo) = needs_val_of { if let Some(nvo) = needs_val_of {
@ -465,7 +472,7 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
break; break;
} else if let Some(candidate) = suggestions::did_you_mean( } else if let Some(candidate) = suggestions::did_you_mean(
&*arg_os.to_string_lossy(), &*arg_os.to_string_lossy(),
self.subcommands.iter().map(|s| &s.0.meta.name)) { self.subcommands.iter().map(|s| &s.p.meta.name)) {
return Err( return Err(
Error::invalid_subcommand(arg_os.to_string_lossy().into_owned(), Error::invalid_subcommand(arg_os.to_string_lossy().into_owned(),
candidate, candidate,
@ -584,30 +591,30 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
mid_string.push_str(" "); mid_string.push_str(" ");
if let Some(ref mut sc) = self.subcommands if let Some(ref mut sc) = self.subcommands
.iter_mut() .iter_mut()
.filter(|s| &s.0.meta.name == &sc_name) .filter(|s| &s.p.meta.name == &sc_name)
.next() { .next() {
let mut sc_matcher = ArgMatcher::new(); let mut sc_matcher = ArgMatcher::new();
// bin_name should be parent's bin_name + [<reqs>] + the sc's name separated by // bin_name should be parent's bin_name + [<reqs>] + the sc's name separated by
// a space // a space
sc.0.meta.usage = Some(format!("{}{}{}", sc.p.meta.usage = Some(format!("{}{}{}",
self.meta.bin_name.as_ref().unwrap_or(&String::new()), self.meta.bin_name.as_ref().unwrap_or(&String::new()),
if self.meta.bin_name.is_some() { if self.meta.bin_name.is_some() {
&*mid_string &*mid_string
} else { } else {
"" ""
}, },
&*sc.0.meta.name)); &*sc.p.meta.name));
sc.0.meta.bin_name = Some(format!("{}{}{}", sc.p.meta.bin_name = Some(format!("{}{}{}",
self.meta.bin_name.as_ref().unwrap_or(&String::new()), self.meta.bin_name.as_ref().unwrap_or(&String::new()),
if self.meta.bin_name.is_some() { if self.meta.bin_name.is_some() {
" " " "
} else { } else {
"" ""
}, },
&*sc.0.meta.name)); &*sc.p.meta.name));
try!(sc.0.get_matches_with(&mut sc_matcher, it)); try!(sc.p.get_matches_with(&mut sc_matcher, it));
matcher.subcommand(SubCommand { matcher.subcommand(SubCommand {
name: sc.0.meta.name.clone(), name: sc.p.meta.name.clone(),
matches: sc_matcher.into(), matches: sc_matcher.into(),
}); });
} }
@ -783,7 +790,7 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
if !self.subcommands.is_empty() && if !self.subcommands.is_empty() &&
!self.subcommands !self.subcommands
.iter() .iter()
.any(|s| &s.0.meta.name[..] == "help") { .any(|s| &s.p.meta.name[..] == "help") {
self.subcommands.push(App::new("help").about("Prints this message")); self.subcommands.push(App::new("help").about("Prints this message"));
} }
} }
@ -1445,8 +1452,8 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
let mut longest_sc = 0; let mut longest_sc = 0;
for scl in self.subcommands for scl in self.subcommands
.iter() .iter()
.filter(|s| !s.0.is_set(AppSettings::Hidden)) .filter(|s| !s.p.is_set(AppSettings::Hidden))
.map(|s| s.0.meta.name.len()) { .map(|s| s.p.meta.name.len()) {
if scl > longest_sc { if scl > longest_sc {
longest_sc = scl; longest_sc = scl;
} }
@ -1509,12 +1516,12 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
if subcmds { if subcmds {
try!(write!(w, "\nSUBCOMMANDS:\n")); try!(write!(w, "\nSUBCOMMANDS:\n"));
for (name, sc) in self.subcommands.iter() for (name, sc) in self.subcommands.iter()
.filter(|s| !s.0.is_set(AppSettings::Hidden)) .filter(|s| !s.p.is_set(AppSettings::Hidden))
.map(|s| (&s.0.meta.name[..], s)) .map(|s| (&s.p.meta.name[..], s))
.collect::<BTreeMap<_, _>>() { .collect::<BTreeMap<_, _>>() {
try!(write!(w, "{}{}", tab, name)); try!(write!(w, "{}{}", tab, name));
write_spaces!((longest_sc + 4) - (name.len()), w); write_spaces!((longest_sc + 4) - (name.len()), w);
if let Some(a) = sc.0.meta.about { if let Some(a) = sc.p.meta.about {
if a.contains("{n}") { if a.contains("{n}") {
let mut ab = a.split("{n}"); let mut ab = a.split("{n}");
while let Some(part) = ab.next() { while let Some(part) = ab.next() {