Skip to content

Commit

Permalink
feat: adds support for external subcommands
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kbknapp committed Jan 28, 2016
1 parent 89462a1 commit 177fe5c
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
/// }
Expand Down

0 comments on commit 177fe5c

Please sign in to comment.