mirror of
https://github.com/clap-rs/clap
synced 2025-01-07 10:18:48 +00:00
7763a7c52a
The subcommand examples focus on the various ways of looking up subcommands except for the main one people should be using. This has previously caused confusion, see https://github.com/clap-rs/clap/discussions/3000 To keep the examples focused, I only show `ArgMatches::subcommand`. I figure the examples are not exhaustive and that for the cases when someone wants to do something more specialized, they can pull up the API reference.
32 lines
987 B
Rust
32 lines
987 B
Rust
use clap::{App, Arg};
|
|
|
|
fn main() {
|
|
let matches = App::new("MyApp")
|
|
.subcommand(
|
|
App::new("ls")
|
|
.aliases(&["list", "dir"])
|
|
.about("Adds files to myapp")
|
|
.version("0.1")
|
|
.author("Kevin K.")
|
|
.arg(
|
|
Arg::new("input")
|
|
.about("the file to add")
|
|
.index(1)
|
|
.required(true),
|
|
),
|
|
)
|
|
.get_matches();
|
|
|
|
// You can also match on a subcommand's name
|
|
match matches.subcommand() {
|
|
Some(("ls", sub_matches)) => println!(
|
|
"'myapp add' was used, input is: {}",
|
|
// Safe to use unwrap() because of the required() option
|
|
sub_matches.value_of("input").unwrap()
|
|
),
|
|
None => println!("No subcommand was used"),
|
|
_ => println!("Some other subcommand was used"),
|
|
}
|
|
|
|
// Continued program logic goes here...
|
|
}
|