mirror of
https://github.com/clap-rs/clap
synced 2024-12-15 07:12:32 +00:00
31b22d1a51
Someone should not reasonably expect a coun flag to go up to billions, millions, or even thousands. 255 should be sufficient for anyone, right? The original type was selected to be consistent with `ArgMatches::occurrences_of` but that is also used for tracking how many values appear which can be large with `xargs`. I'm still conflicted on what the "right type" is an wish we could support any numeric type. When I did a search on github though, every case was for debug/quiet flags and only supported 2-3 occurrences, making a `u8` overkill. This came out of a discussion on #3792
69 lines
1.8 KiB
Rust
69 lines
1.8 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use clap::{Parser, Subcommand};
|
|
|
|
#[derive(Parser)]
|
|
#[clap(author, version, about, long_about = None)]
|
|
struct Cli {
|
|
/// Optional name to operate on
|
|
#[clap(value_parser)]
|
|
name: Option<String>,
|
|
|
|
/// Sets a custom config file
|
|
#[clap(short, long, value_parser, value_name = "FILE")]
|
|
config: Option<PathBuf>,
|
|
|
|
/// Turn debugging information on
|
|
#[clap(short, long, action = clap::ArgAction::Count)]
|
|
debug: u8,
|
|
|
|
#[clap(subcommand)]
|
|
command: Option<Commands>,
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum Commands {
|
|
/// does testing things
|
|
Test {
|
|
/// lists test values
|
|
#[clap(short, long, action)]
|
|
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
|
|
// matches just as you would the top level cmd
|
|
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...
|
|
}
|