Skip to content

Commit

Permalink
feat(usage): add ability to get usage string for subcommands too
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kbknapp committed Apr 10, 2015
1 parent 5137278 commit 3636afc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions src/args/argmatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
""
}
}

0 comments on commit 3636afc

Please sign in to comment.