From 177fe5cce745c2164a8e38c23be4c4460d2d7211 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Tue, 26 Jan 2016 12:25:56 -0500 Subject: [PATCH] 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 --- src/app/settings.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/app/settings.rs b/src/app/settings.rs index 60584f4f4021..6560f4d42377 100644 --- a/src/app/settings.rs +++ b/src/app/settings.rs @@ -257,27 +257,19 @@ pub enum AppSettings { /// # Examples /// /// ```no_run - /// # use clap::{App, Arg, AppSettings}; - /// use std::process::{self, Command}; - /// + /// # use clap::{App, AppSettings}; /// // 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"]); - /// + /// .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 args: Vec<&str> = ext_m.values_of(external).unwrap().collect(); - /// let exit_status = Command::new(format!("myprog-{}", external)) - /// .args(&*args) - /// .status() - /// .unwrap_or_else(|e| { - /// // Invalid subcommand. Here you would probably inform the user and list valid - /// // subcommands for them to try...but in this example we just panic! - /// process::exit(1); - /// }); + /// let ext_args: Vec<&str> = ext_m.values_of(external).unwrap().collect(); + /// assert_eq!(ext_args, ["--option", "value", "-fff", "--flag"]); /// }, /// _ => unreachable!() /// }