2021-11-18 20:17:31 +00:00
|
|
|
use clap::{App, AppSettings, Arg};
|
2015-08-28 04:24:00 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// You can use AppSettings to change the application level behavior of clap. .setting() function
|
2021-02-11 00:12:36 +00:00
|
|
|
// of App struct takes AppSettings enum as argument. You can learn more about AppSettings in the
|
|
|
|
// documentation, which also has examples on each setting.
|
2015-08-28 04:24:00 +00:00
|
|
|
//
|
|
|
|
// This example will only show usage of one AppSettings setting. See documentation for more
|
|
|
|
// information.
|
|
|
|
|
|
|
|
let matches = App::new("myapp")
|
2018-11-14 17:05:06 +00:00
|
|
|
.setting(AppSettings::SubcommandsNegateReqs)
|
|
|
|
// Negates requirement of parent command.
|
2021-11-18 20:17:31 +00:00
|
|
|
.arg(Arg::from_usage("<input> 'input file to use'"))
|
2018-11-14 17:05:06 +00:00
|
|
|
// 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();
|
2015-08-28 04:24:00 +00:00
|
|
|
|
2015-08-30 20:16:35 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:36:02 +00:00
|
|
|
match matches.subcommand_name() {
|
|
|
|
Some("test") => println!("The 'test' subcommand was used"),
|
2021-11-12 01:04:26 +00:00
|
|
|
None => {}
|
2018-01-25 04:05:05 +00:00
|
|
|
_ => unreachable!(),
|
2015-08-30 20:16:35 +00:00
|
|
|
}
|
2015-08-28 04:24:00 +00:00
|
|
|
}
|