2018-11-14 17:05:06 +00:00
|
|
|
use clap::{App, Arg};
|
2015-03-20 19:06:44 +00:00
|
|
|
|
|
|
|
fn main() {
|
2015-08-27 15:08:49 +00:00
|
|
|
// Just like arg() and args(), subcommands can be specified one at a time via subcommand() or
|
2021-06-12 20:25:56 +00:00
|
|
|
// multiple ones at once with a Vec<App> provided to subcommands().
|
2015-03-20 19:06:44 +00:00
|
|
|
let matches = App::new("MyApp")
|
2021-11-10 22:15:30 +00:00
|
|
|
.version("1.0")
|
2018-11-14 17:05:06 +00:00
|
|
|
// 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(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("input") // And their own arguments
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("the file to add")
|
2018-11-14 17:05:06 +00:00
|
|
|
.index(1)
|
|
|
|
.required(true),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.get_matches();
|
2015-03-20 19:06:44 +00:00
|
|
|
|
2021-11-12 14:17:51 +00:00
|
|
|
// 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: {}",
|
2021-11-12 14:42:25 +00:00
|
|
|
sub_matches
|
|
|
|
.value_of("input")
|
|
|
|
.expect("'input' is required and parsing will fail if its missing")
|
2021-11-12 14:17:51 +00:00
|
|
|
),
|
2018-01-25 04:05:05 +00:00
|
|
|
None => println!("No subcommand was used"),
|
|
|
|
_ => println!("Some other subcommand was used"),
|
2015-03-20 19:06:44 +00:00
|
|
|
}
|
2015-08-27 15:08:49 +00:00
|
|
|
|
2015-03-20 19:06:44 +00:00
|
|
|
// Continued program logic goes here...
|
|
|
|
}
|