blob: 4dfd341209e1e792b13cd211c21924babd3517f6 [file] [log] [blame]
Matthew Maurer32e76692020-06-02 11:15:15 -07001//! Somewhat complex example of usage of structopt.
David LeGare12ae7f82022-03-02 16:20:50 +00002//!
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 Maurer32e76692020-06-02 11:15:15 -070025
26use structopt::StructOpt;
27
28#[derive(StructOpt, Debug)]
29#[structopt(name = "example")]
30/// An example of StructOpt usage.
31struct 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 Galenson108a00f2021-08-09 10:44:46 -070063 // provided (i.e. `--optv`) and will be `Some(Vec<String>)` if
Matthew Maurer32e76692020-06-02 11:15:15 -070064 // 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
74fn main() {
75 let opt = Opt::from_args();
76 println!("{:?}", opt);
77}