clap/examples/08_subcommands.rs
Ed Page bfa02fd418 test: More thoroughly test examples
This ports our example testing over to [trycmd](https://docs.rs/) so
we can:
- More thoroughly test our examples
- Provide always-up-to-date example usage

The old way of testing automatically picked up examples.  This new way
requires we have a `.md` file that uses the example in some way.

Notes:
- Moved overall example description to the `.md` file
- I added cross-linking between related examples
- `14_groups` had a redundant paragraph (twice talked about "one and
  only one"
2021-11-23 13:13:41 -06:00

40 lines
1.6 KiB
Rust

use clap::{App, Arg};
fn main() {
// Just like arg() and args(), subcommands can be specified one at a time via subcommand() or
// multiple ones at once with a Vec<App> provided to subcommands().
let matches = App::new("MyApp")
.version("1.0")
// Normal App and Arg configuration goes here...
// In the following example assume we wanted an application which
// supported an "add" subcommand, this "add" subcommand also took
// one positional argument of a file to add:
.subcommand(
App::new("add") // The name we call argument with
.about("Adds files to myapp") // The message displayed in "myapp -h"
// or "myapp help"
.version("0.1") // Subcommands can have independent version
.author("Kevin K.") // And authors
.arg(
Arg::new("input") // And their own arguments
.help("the file to add")
.index(1)
.required(true),
),
)
.get_matches();
// Most commonly, you'll get the name and matches at the same time
match matches.subcommand() {
Some(("add", sub_matches)) => println!(
"'myapp add' was used, input is: {}",
sub_matches
.value_of("input")
.expect("'input' is required and parsing will fail if its missing")
),
None => println!("No subcommand was used"),
_ => println!("Some other subcommand was used"),
}
// Continued program logic goes here...
}