Skip to content

Commit 177fe5c

Browse files
committed
feat: adds support for external subcommands
External subcommands are now supported via the following: ```rust extern crate clap; use clap::{App, AppSettings}; fn main() { // Assume there is a third party subcommand named myprog-subcmd let m = App::new("myprog") .setting(AppSettings::AllowExternalSubcommands) .get_matches_from(vec![ "myprog", "subcmd", "--option", "value", "-fff", "--flag" ]); // All trailing arguments will be stored under the subcommands sub-matches under a // value of their runtime name (in this case "subcmd") match m.subcommand() { (external, Some(ext_m)) => { let ext_args: Vec<&str> = ext_m.values_of(external).unwrap().collect(); assert_eq!(ext_args ,["--option", "value", "-fff", "--flag"]); }, _ => unreachable!() } } ``` Closes #372
1 parent 89462a1 commit 177fe5c

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

src/app/settings.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -257,27 +257,19 @@ pub enum AppSettings {
257257
/// # Examples
258258
///
259259
/// ```no_run
260-
/// # use clap::{App, Arg, AppSettings};
261-
/// use std::process::{self, Command};
262-
///
260+
/// # use clap::{App, AppSettings};
263261
/// // Assume there is a third party subcommand named myprog-subcmd
264262
/// let m = App::new("myprog")
265263
/// .setting(AppSettings::AllowExternalSubcommands)
266-
/// .get_matches_from(vec!["myprog", "subcmd", "--option", "value"]);
267-
///
264+
/// .get_matches_from(vec![
265+
/// "myprog", "subcmd", "--option", "value", "-fff", "--flag"
266+
/// ]);
268267
/// // All trailing arguments will be stored under the subcommands sub-matches under a value
269268
/// // of their runtime name (in this case "subcmd")
270269
/// match m.subcommand() {
271270
/// (external, Some(ext_m)) => {
272-
/// let args: Vec<&str> = ext_m.values_of(external).unwrap().collect();
273-
/// let exit_status = Command::new(format!("myprog-{}", external))
274-
/// .args(&*args)
275-
/// .status()
276-
/// .unwrap_or_else(|e| {
277-
/// // Invalid subcommand. Here you would probably inform the user and list valid
278-
/// // subcommands for them to try...but in this example we just panic!
279-
/// process::exit(1);
280-
/// });
271+
/// let ext_args: Vec<&str> = ext_m.values_of(external).unwrap().collect();
272+
/// assert_eq!(ext_args, ["--option", "value", "-fff", "--flag"]);
281273
/// },
282274
/// _ => unreachable!()
283275
/// }

0 commit comments

Comments
 (0)