2020-10-09 01:05:05 +03:00
|
|
|
//! A somewhat comprehensive example of a typical `clap_derive` usage.
|
2017-02-03 01:03:51 +01:00
|
|
|
|
2021-07-13 12:50:55 -05:00
|
|
|
use clap::{Parser, ValueHint};
|
2018-02-02 00:00:08 +01:00
|
|
|
use std::path::PathBuf;
|
2017-02-03 01:03:51 +01:00
|
|
|
|
2017-06-15 15:18:17 +02:00
|
|
|
/// A basic example
|
2021-07-13 12:50:55 -05:00
|
|
|
#[derive(Parser, Debug)]
|
2018-07-02 14:45:17 -04:00
|
|
|
#[clap(name = "basic")]
|
2017-02-03 01:03:51 +01:00
|
|
|
struct Opt {
|
2018-02-02 00:00:08 +01:00
|
|
|
// A flag, true if used in the command line. Note doc comment will
|
2020-01-07 15:47:23 +05:30
|
|
|
// be used for the help message of the flag. The name of the
|
|
|
|
// argument will be, by default, based on the name of the field.
|
2017-06-15 15:18:17 +02:00
|
|
|
/// Activate debug mode
|
2020-01-07 15:47:23 +05:30
|
|
|
#[clap(short, long)]
|
2017-02-04 01:43:44 +01:00
|
|
|
debug: bool,
|
2017-06-15 15:18:17 +02:00
|
|
|
|
2020-01-07 15:47:23 +05:30
|
|
|
// The number of occurrences of the `v/verbose` flag
|
2018-02-03 06:15:03 -08:00
|
|
|
/// Verbose mode (-v, -vv, -vvv, etc.)
|
2020-01-07 15:47:23 +05:30
|
|
|
#[clap(short, long, parse(from_occurrences))]
|
2018-02-02 00:00:08 +01:00
|
|
|
verbose: u8,
|
2017-06-15 15:18:17 +02:00
|
|
|
|
|
|
|
/// Set speed
|
2020-01-07 15:47:23 +05:30
|
|
|
#[clap(short, long, default_value = "42")]
|
2017-02-06 23:55:46 +01:00
|
|
|
speed: f64,
|
2017-06-15 15:18:17 +02:00
|
|
|
|
|
|
|
/// Output file
|
2020-10-09 01:05:05 +03:00
|
|
|
#[clap(short, long, parse(from_os_str), value_hint = ValueHint::FilePath)]
|
2018-02-02 00:00:08 +01:00
|
|
|
output: PathBuf,
|
2017-06-15 15:18:17 +02:00
|
|
|
|
2020-01-07 15:47:23 +05:30
|
|
|
// the long option will be translated by default to kebab case,
|
|
|
|
// i.e. `--nb-cars`.
|
2018-02-10 20:57:33 +01:00
|
|
|
/// Number of cars
|
2020-06-16 17:43:31 -07:00
|
|
|
#[clap(short = 'c', long)]
|
2018-02-10 20:57:33 +01:00
|
|
|
nb_cars: Option<i32>,
|
2017-06-15 15:18:17 +02:00
|
|
|
|
|
|
|
/// admin_level to consider
|
2020-01-07 15:47:23 +05:30
|
|
|
#[clap(short, long)]
|
2017-02-04 01:43:44 +01:00
|
|
|
level: Vec<String>,
|
2017-06-15 15:18:17 +02:00
|
|
|
|
|
|
|
/// Files to process
|
2020-10-09 01:05:05 +03:00
|
|
|
#[clap(name = "FILE", parse(from_os_str), value_hint = ValueHint::AnyPath)]
|
2018-02-02 00:00:08 +01:00
|
|
|
files: Vec<PathBuf>,
|
2017-02-03 01:03:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-07-02 14:45:17 -04:00
|
|
|
let opt = Opt::parse();
|
2020-01-07 15:47:23 +05:30
|
|
|
println!("{:#?}", opt);
|
2017-02-03 01:03:51 +01:00
|
|
|
}
|