Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame] | 1 | //! A somewhat comprehensive example of a typical `StructOpt` usage.use |
| 2 | |
| 3 | use std::path::PathBuf; |
| 4 | use structopt::StructOpt; |
| 5 | |
| 6 | /// A basic example |
| 7 | #[derive(StructOpt, Debug)] |
| 8 | #[structopt(name = "basic")] |
| 9 | struct Opt { |
| 10 | // A flag, true if used in the command line. Note doc comment will |
| 11 | // be used for the help message of the flag. The name of the |
| 12 | // argument will be, by default, based on the name of the field. |
| 13 | /// Activate debug mode |
| 14 | #[structopt(short, long)] |
| 15 | debug: bool, |
| 16 | |
| 17 | // The number of occurrences of the `v/verbose` flag |
| 18 | /// Verbose mode (-v, -vv, -vvv, etc.) |
| 19 | #[structopt(short, long, parse(from_occurrences))] |
| 20 | verbose: u8, |
| 21 | |
| 22 | /// Set speed |
| 23 | #[structopt(short, long, default_value = "42")] |
| 24 | speed: f64, |
| 25 | |
| 26 | /// Output file |
| 27 | #[structopt(short, long, parse(from_os_str))] |
| 28 | output: PathBuf, |
| 29 | |
| 30 | // the long option will be translated by default to kebab case, |
| 31 | // i.e. `--nb-cars`. |
| 32 | /// Number of cars |
| 33 | #[structopt(short = "c", long)] |
| 34 | nb_cars: Option<i32>, |
| 35 | |
| 36 | /// admin_level to consider |
| 37 | #[structopt(short, long)] |
| 38 | level: Vec<String>, |
| 39 | |
| 40 | /// Files to process |
| 41 | #[structopt(name = "FILE", parse(from_os_str))] |
| 42 | files: Vec<PathBuf>, |
| 43 | } |
| 44 | |
| 45 | fn main() { |
| 46 | let opt = Opt::from_args(); |
| 47 | println!("{:#?}", opt); |
| 48 | } |