clap/examples/08_subcommands.rs

41 lines
1.6 KiB
Rust
Raw Normal View History

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
// multiple ones at once with a Vec<App> provided to subcommands().
2015-03-20 19:06:44 +00:00
let matches = App::new("MyApp")
.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(
Arg::new("input") // And their own arguments
.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
// 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")
),
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...
}