2021-11-30 18:30:19 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use clap::{Parser, Subcommand};
|
|
|
|
|
|
|
|
#[derive(Parser)]
|
2022-01-11 00:47:20 +00:00
|
|
|
#[clap(author, version, about, long_about = None)]
|
2021-11-30 18:30:19 +00:00
|
|
|
struct Cli {
|
|
|
|
/// Optional name to operate on
|
2022-05-21 00:52:04 +00:00
|
|
|
#[clap(value_parser)]
|
2021-11-30 18:30:19 +00:00
|
|
|
name: Option<String>,
|
|
|
|
|
|
|
|
/// Sets a custom config file
|
2022-05-21 00:52:04 +00:00
|
|
|
#[clap(short, long, value_parser, value_name = "FILE")]
|
2021-11-30 18:30:19 +00:00
|
|
|
config: Option<PathBuf>,
|
|
|
|
|
|
|
|
/// Turn debugging information on
|
2022-06-01 15:31:23 +00:00
|
|
|
#[clap(short, long, action = clap::ArgAction::Count)]
|
2022-06-09 16:09:36 +00:00
|
|
|
debug: u8,
|
2021-11-30 18:30:19 +00:00
|
|
|
|
|
|
|
#[clap(subcommand)]
|
|
|
|
command: Option<Commands>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
|
|
|
enum Commands {
|
|
|
|
/// does testing things
|
|
|
|
Test {
|
|
|
|
/// lists test values
|
2022-06-01 15:31:23 +00:00
|
|
|
#[clap(short, long, action)]
|
2021-11-30 18:30:19 +00:00
|
|
|
list: bool,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let cli = Cli::parse();
|
|
|
|
|
|
|
|
// You can check the value provided by positional arguments, or option arguments
|
|
|
|
if let Some(name) = cli.name.as_deref() {
|
|
|
|
println!("Value for name: {}", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(config_path) = cli.config.as_deref() {
|
|
|
|
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
|
|
|
|
match cli.debug {
|
|
|
|
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
|
|
|
match &cli.command {
|
|
|
|
Some(Commands::Test { list }) => {
|
|
|
|
if *list {
|
|
|
|
println!("Printing testing lists...");
|
|
|
|
} else {
|
|
|
|
println!("Not printing testing lists...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continued program logic goes here...
|
|
|
|
}
|