]>
git.proxmox.com Git - cargo.git/blob - vendor/clap/examples/tutorial_derive/01_quick.rs
1 use std
::path
::PathBuf
;
3 use clap
::{Parser, Subcommand}
;
6 #[clap(author, version, about, long_about = None)]
8 /// Optional name to operate on
12 /// Sets a custom config file
13 #[clap(short, long, value_parser, value_name = "FILE")]
14 config
: Option
<PathBuf
>,
16 /// Turn debugging information on
17 #[clap(short, long, action = clap::ArgAction::Count)]
21 command
: Option
<Commands
>,
26 /// does testing things
29 #[clap(short, long, action)]
35 let cli
= Cli
::parse();
37 // You can check the value provided by positional arguments, or option arguments
38 if let Some(name
) = cli
.name
.as_deref() {
39 println
!("Value for name: {}", name
);
42 if let Some(config_path
) = cli
.config
.as_deref() {
43 println
!("Value for config: {}", config_path
.display());
46 // You can see how many times a particular flag or argument occurred
47 // Note, only flags can have multiple occurrences
49 0 => println
!("Debug mode is off"),
50 1 => println
!("Debug mode is kind of on"),
51 2 => println
!("Debug mode is on"),
52 _
=> println
!("Don't be crazy"),
55 // You can check for the existence of subcommands, and if found use their
56 // matches just as you would the top level cmd
58 Some(Commands
::Test { list }
) => {
60 println
!("Printing testing lists...");
62 println
!("Not printing testing lists...");
68 // Continued program logic goes here...