2022-05-23 15:50:11 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-06-06 17:35:00 +00:00
|
|
|
use clap::{arg, command, value_parser, ArgAction, Command};
|
2021-11-30 18:30:19 +00:00
|
|
|
|
|
|
|
fn main() {
|
2022-07-19 18:29:31 +00:00
|
|
|
let matches = command!() // requires `cargo` feature
|
2021-11-30 18:30:19 +00:00
|
|
|
.arg(arg!([name] "Optional name to operate on"))
|
|
|
|
.arg(
|
|
|
|
arg!(
|
|
|
|
-c --config <FILE> "Sets a custom config file"
|
|
|
|
)
|
|
|
|
// We don't have syntax yet for optional options, so manually calling `required`
|
|
|
|
.required(false)
|
2022-05-23 15:50:11 +00:00
|
|
|
.value_parser(value_parser!(PathBuf)),
|
2021-11-30 18:30:19 +00:00
|
|
|
)
|
2022-08-08 19:35:31 +00:00
|
|
|
.arg(arg!(
|
|
|
|
-d --debug ... "Turn debugging information on"
|
|
|
|
))
|
2021-11-30 18:30:19 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("test")
|
2021-11-30 18:30:19 +00:00
|
|
|
.about("does testing things")
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(arg!(-l --list "lists test values").action(ArgAction::SetTrue)),
|
2021-11-30 18:30:19 +00:00
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
// You can check the value provided by positional arguments, or option arguments
|
2022-05-25 15:46:42 +00:00
|
|
|
if let Some(name) = matches.get_one::<String>("name") {
|
2023-05-04 19:53:36 +00:00
|
|
|
println!("Value for name: {name}");
|
2021-11-30 18:30:19 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 15:46:42 +00:00
|
|
|
if let Some(config_path) = matches.get_one::<PathBuf>("config") {
|
2021-11-30 18:30:19 +00:00
|
|
|
println!("Value for config: {}", config_path.display());
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can see how many times a particular flag or argument occurred
|
|
|
|
// Note, only flags can have multiple occurrences
|
2022-06-06 17:35:00 +00:00
|
|
|
match matches
|
2022-06-09 16:09:36 +00:00
|
|
|
.get_one::<u8>("debug")
|
2022-06-06 17:35:00 +00:00
|
|
|
.expect("Count's are defaulted")
|
|
|
|
{
|
2021-11-30 18:30:19 +00:00
|
|
|
0 => println!("Debug mode is off"),
|
|
|
|
1 => println!("Debug mode is kind of on"),
|
|
|
|
2 => println!("Debug mode is on"),
|
|
|
|
_ => println!("Don't be crazy"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can check for the existence of subcommands, and if found use their
|
2022-02-14 21:47:20 +00:00
|
|
|
// matches just as you would the top level cmd
|
2021-11-30 18:30:19 +00:00
|
|
|
if let Some(matches) = matches.subcommand_matches("test") {
|
|
|
|
// "$ myapp test" was run
|
2023-01-23 15:23:38 +00:00
|
|
|
if matches.get_flag("list") {
|
2021-11-30 18:30:19 +00:00
|
|
|
// "$ myapp test -l" was run
|
|
|
|
println!("Printing testing lists...");
|
|
|
|
} else {
|
|
|
|
println!("Not printing testing lists...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continued program logic goes here...
|
|
|
|
}
|