Skip to content

Commit 3636afc

Browse files
committed
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
1 parent 5137278 commit 3636afc

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/app.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,6 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
706706
}
707707
}
708708
}
709-
matches.usage = Some(self.create_usage());
710709
self.get_matches_from(&mut matches, &mut it );
711710

712711
matches
@@ -879,7 +878,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
879878
}
880879
}
881880
} else {
882-
self.report_error(format!("Positional argument \"{}\" was found, but {} wasn't expecting any", arg, self.name), true, true);
881+
self.report_error(format!("Argument \"{}\" isn't a valid argument for {}", arg, self.bin_name.clone().unwrap_or(self.name.clone())), true, true);
883882
}
884883
}
885884
}
@@ -899,6 +898,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{
899898
true, true);
900899
}
901900

901+
matches.usage = Some(self.create_usage());
902902

903903
if let Some(sc_name) = subcmd_name {
904904
if let Some(ref mut sc) = self.subcommands.get_mut(&sc_name) {

src/args/argmatches.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl<'a> ArgMatches<'a> {
266266
("", None)
267267
}
268268

269-
/// Returns a slice of the default usage for the *top level parent App only*
269+
/// Returns a slice of the usage
270270
///
271271
///
272272
/// # Example
@@ -276,10 +276,12 @@ impl<'a> ArgMatches<'a> {
276276
/// # let app_matches = App::new("myapp").subcommand(SubCommand::new("test")).get_matches();
277277
/// println!("{}",app_matches.usage().unwrap());
278278
/// ```
279-
pub fn usage(&self) -> Option<&str> {
279+
pub fn usage(&self) -> &str {
280280
if let Some( ref u ) = self.usage {
281-
return Some(&u[..]);
281+
return &u[..];
282282
}
283-
None
283+
284+
// Should be un-reachable
285+
""
284286
}
285287
}

0 commit comments

Comments
 (0)