Skip to content

Commit

Permalink
test: adds failing doc test
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodie committed May 11, 2016
1 parent 57c8777 commit 6ba910e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
39 changes: 39 additions & 0 deletions examples/20_aliases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
extern crate clap;

use clap::{App, Arg, SubCommand};

fn main() {

let matches = App::new("MyApp")
.subcommand(SubCommand::with_name("ls")
.aliases(&["list", "dir"])
.about("Adds files to myapp")
.version("0.1")
.author("Kevin K.")
.arg(Arg::with_name("input")
.help("the file to add")
.index(1)
.required(true))
)
.get_matches();

// You can check if a subcommand was used like normal
if matches.is_present("add") {
println!("'myapp add' was run.");
}

// You can get the independent subcommand matches (which function exactly like App matches)
if let Some(ref matches) = matches.subcommand_matches("add") {
// Safe to use unwrap() because of the required() option
println!("Adding file: {}", matches.value_of("input").unwrap());
}

// You can also match on a subcommand's name
match matches.subcommand_name() {
Some("add") => println!("'myapp add' was used"),
None => println!("No subcommand was used"),
_ => println!("Some other subcommand was used"),
}

// Continued program logic goes here...
}
6 changes: 5 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,15 @@ impl<'a, 'b> App<'a, 'b> {
///
/// # Examples
///
/// ```no_run
/// ```rust
/// # use clap::{App, Arg, SubCommand};
/// let m = App::new("myprog")
/// .subcommand(SubCommand::with_name("test")
/// .aliases(&["do-stuff", "do-tests", "tests"]))
/// .arg(Arg::with_name("input")
/// .help("the file to add")
/// .index(1)
/// .required(true))
/// .get_matches_from(vec!["myprog", "do-tests"]);
/// assert_eq!(m.subcommand_name(), Some("test"));
/// ```
Expand Down

0 comments on commit 6ba910e

Please sign in to comment.