Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame] | 1 | //! Somewhat complex example of usage of structopt. |
David LeGare | 12ae7f8 | 2022-03-02 16:20:50 +0000 | [diff] [blame^] | 2 | //! |
| 3 | //! Running this example with --help prints this message: |
| 4 | //! ----------------------------------------------------- |
| 5 | //! example 0.3.25 |
| 6 | //! An example of StructOpt usage |
| 7 | //! |
| 8 | //! USAGE: |
| 9 | //! example [FLAGS] [OPTIONS] <input> [--] [output] |
| 10 | //! |
| 11 | //! FLAGS: |
| 12 | //! -d, --debug Activate debug mode |
| 13 | //! -h, --help Prints help information |
| 14 | //! -V, --version Prints version information |
| 15 | //! |
| 16 | //! OPTIONS: |
| 17 | //! --log <log> Log file, stdout if no file, no logging if not present |
| 18 | //! --optv <optv>... |
| 19 | //! -s, --speed <speed> Set speed [default: 42] |
| 20 | //! |
| 21 | //! ARGS: |
| 22 | //! <input> Input file |
| 23 | //! <output> Output file, stdout if not present |
| 24 | //! ----------------------------------------------------- |
Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame] | 25 | |
| 26 | use structopt::StructOpt; |
| 27 | |
| 28 | #[derive(StructOpt, Debug)] |
| 29 | #[structopt(name = "example")] |
| 30 | /// An example of StructOpt usage. |
| 31 | struct Opt { |
| 32 | // A flag, true if used in the command line. |
| 33 | #[structopt(short, long)] |
| 34 | /// Activate debug mode |
| 35 | debug: bool, |
| 36 | |
| 37 | // An argument of type float, with a default value. |
| 38 | #[structopt(short, long, default_value = "42")] |
| 39 | /// Set speed |
| 40 | speed: f64, |
| 41 | |
| 42 | // Needed parameter, the first on the command line. |
| 43 | /// Input file |
| 44 | input: String, |
| 45 | |
| 46 | // An optional parameter, will be `None` if not present on the |
| 47 | // command line. |
| 48 | /// Output file, stdout if not present |
| 49 | output: Option<String>, |
| 50 | |
| 51 | // An optional parameter with optional value, will be `None` if |
| 52 | // not present on the command line, will be `Some(None)` if no |
| 53 | // argument is provided (i.e. `--log`) and will be |
| 54 | // `Some(Some(String))` if argument is provided (e.g. `--log |
| 55 | // log.txt`). |
| 56 | #[structopt(long)] |
| 57 | #[allow(clippy::option_option)] |
| 58 | /// Log file, stdout if no file, no logging if not present |
| 59 | log: Option<Option<String>>, |
| 60 | |
| 61 | // An optional list of values, will be `None` if not present on |
| 62 | // the command line, will be `Some(vec![])` if no argument is |
Joel Galenson | 108a00f | 2021-08-09 10:44:46 -0700 | [diff] [blame] | 63 | // provided (i.e. `--optv`) and will be `Some(Vec<String>)` if |
Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame] | 64 | // argument list is provided (e.g. `--optv a b c`). |
| 65 | #[structopt(long)] |
| 66 | optv: Option<Vec<String>>, |
| 67 | |
| 68 | // Skipped option: it won't be parsed and will be filled with the |
| 69 | // default value for its type (in this case it'll be an empty string). |
| 70 | #[structopt(skip)] |
| 71 | skipped: String, |
| 72 | } |
| 73 | |
| 74 | fn main() { |
| 75 | let opt = Opt::from_args(); |
| 76 | println!("{:?}", opt); |
| 77 | } |