feat(usage): add ability to get usage string for subcommands too

You can now get the usage even for sub-commands by calling the
usage() method.

Breaking Change
ArgMatches::usage() now returns a slice (no longer an Option<&str>)
This is to improve ergonomics, as there should always be at least a
default usage statement, there should never be None
This commit is contained in:
Kevin K 2015-04-10 10:46:07 -04:00
parent 5137278942
commit 3636afc401
2 changed files with 8 additions and 6 deletions

View file

@ -706,7 +706,6 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
}
}
}
matches.usage = Some(self.create_usage());
self.get_matches_from(&mut matches, &mut it );
matches
@ -879,7 +878,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
}
}
} else {
self.report_error(format!("Positional argument \"{}\" was found, but {} wasn't expecting any", arg, self.name), true, true);
self.report_error(format!("Argument \"{}\" isn't a valid argument for {}", arg, self.bin_name.clone().unwrap_or(self.name.clone())), true, true);
}
}
}
@ -899,6 +898,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
true, true);
}
matches.usage = Some(self.create_usage());
if let Some(sc_name) = subcmd_name {
if let Some(ref mut sc) = self.subcommands.get_mut(&sc_name) {

View file

@ -266,7 +266,7 @@ impl<'a> ArgMatches<'a> {
("", None)
}
/// Returns a slice of the default usage for the *top level parent App only*
/// Returns a slice of the usage
///
///
/// # Example
@ -276,10 +276,12 @@ impl<'a> ArgMatches<'a> {
/// # let app_matches = App::new("myapp").subcommand(SubCommand::new("test")).get_matches();
/// println!("{}",app_matches.usage().unwrap());
/// ```
pub fn usage(&self) -> Option<&str> {
pub fn usage(&self) -> &str {
if let Some( ref u ) = self.usage {
return Some(&u[..]);
return &u[..];
}
None
// Should be un-reachable
""
}
}