2021-07-13 17:50:55 +00:00
|
|
|
//! Somewhat complex example of usage of #[derive(Parser)].
|
2017-02-09 21:51:41 +00:00
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
use clap::Parser;
|
2017-02-09 21:51:41 +00:00
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug)]
|
2020-01-07 10:17:23 +00:00
|
|
|
#[clap(name = "example")]
|
|
|
|
/// An example of clap_derive usage.
|
2017-02-09 21:51:41 +00:00
|
|
|
struct Opt {
|
2020-01-07 10:17:23 +00:00
|
|
|
// A flag, true if used in the command line.
|
|
|
|
#[clap(short, long)]
|
|
|
|
/// Activate debug mode
|
2017-02-09 21:51:41 +00:00
|
|
|
debug: bool,
|
|
|
|
|
2020-01-07 10:17:23 +00:00
|
|
|
// An argument of type float, with a default value.
|
|
|
|
#[clap(short, long, default_value = "42")]
|
|
|
|
/// Set speed
|
2017-02-09 21:51:41 +00:00
|
|
|
speed: f64,
|
|
|
|
|
2020-01-07 10:17:23 +00:00
|
|
|
// Needed parameter, the first on the command line.
|
|
|
|
/// Input file
|
2017-02-09 21:51:41 +00:00
|
|
|
input: String,
|
|
|
|
|
2020-01-07 10:17:23 +00:00
|
|
|
// An optional parameter, will be `None` if not present on the
|
|
|
|
// command line.
|
|
|
|
/// Output file, stdout if not present
|
2017-02-09 21:51:41 +00:00
|
|
|
output: Option<String>,
|
2020-01-07 10:17:23 +00:00
|
|
|
|
|
|
|
// An optional parameter with optional value, will be `None` if
|
|
|
|
// not present on the command line, will be `Some(None)` if no
|
|
|
|
// argument is provided (i.e. `--log`) and will be
|
|
|
|
// `Some(Some(String))` if argument is provided (e.g. `--log
|
|
|
|
// log.txt`).
|
|
|
|
#[clap(long)]
|
|
|
|
#[allow(clippy::option_option)]
|
|
|
|
/// Log file, stdout if no file, no logging if not present
|
|
|
|
log: Option<Option<String>>,
|
|
|
|
|
|
|
|
// An optional list of values, will be `None` if not present on
|
|
|
|
// the command line, will be `Some(vec![])` if no argument is
|
2021-10-06 18:14:04 +00:00
|
|
|
// provided (i.e. `--optv`) and will be `Some(Vec<String>)` if
|
2020-01-07 10:17:23 +00:00
|
|
|
// argument list is provided (e.g. `--optv a b c`).
|
|
|
|
#[clap(long)]
|
|
|
|
optv: Option<Vec<String>>,
|
|
|
|
|
|
|
|
// Skipped option: it won't be parsed and will be filled with the
|
|
|
|
// default value for its type (in this case it'll be an empty string).
|
|
|
|
#[clap(skip)]
|
|
|
|
skipped: String,
|
2017-02-09 21:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-07-02 18:45:17 +00:00
|
|
|
let opt = Opt::parse();
|
2021-10-08 17:51:58 +00:00
|
|
|
println!("{:?}", opt.skipped);
|
2017-02-09 21:51:41 +00:00
|
|
|
}
|