clap/examples/16_app_settings.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

28 lines
1.1 KiB
Rust

use clap::{arg, App, AppSettings};
fn main() {
let matches = App::new("myapp")
.setting(AppSettings::SubcommandsNegateReqs)
// Negates requirement of parent command.
.arg(arg!(<input> "input file to use"))
// Required positional argument called input. This
// will be only required if subcommand is not present.
.subcommand(App::new("test").about("does some testing"))
// if program is invoked with subcommand, you do not
// need to specify the <input> argument anymore due to
// the AppSettings::SubcommandsNegateReqs setting.
.get_matches();
// Calling unwrap() on "input" would not be advised here, because although it's required,
// if the user uses a subcommand, those requirements are no longer required. Hence, we should
// use some sort of 'if let' construct
if let Some(inp) = matches.value_of("input") {
println!("The input file is: {}", inp);
}
match matches.subcommand_name() {
Some("test") => println!("The 'test' subcommand was used"),
None => {}
_ => unreachable!(),
}
}