1 use gumdrop::Options;
2
3 // Defines options that can be parsed from the command line.
4 //
5 // `derive(Options)` will generate an implementation of the trait `Options`.
6 // Each field must either have a `Default` implementation or an inline
7 // default value provided.
8 //
9 // (`Debug` is only derived here for demonstration purposes.)
10 #[derive(Debug, Options)]
11 struct MyOptions {
12 // Contains "free" arguments -- those that are not options.
13 // If no `free` field is declared, free arguments will result in an error.
14 #[options(free)]
15 free: Vec<String>,
16
17 // Boolean options are treated as flags, taking no additional values.
18 // The optional `help` attribute is displayed in `usage` text.
19 #[options(help = "print help message")]
20 help: bool,
21
22 // Non-boolean fields will take a value from the command line.
23 // Wrapping the type in an `Option` is not necessary, but provides clarity.
24 #[options(help = "give a string argument")]
25 string: Option<String>,
26
27 // A field can be any type that implements `FromStr`.
28 // The optional `meta` attribute is displayed in `usage` text.
29 #[options(help = "give a number as an argument", meta = "N")]
30 number: Option<i32>,
31
32 // A `Vec` field will accumulate all values received from the command line.
33 #[options(help = "give a list of string items")]
34 item: Vec<String>,
35
36 // The `count` flag will treat the option as a counter.
37 // Each time the option is encountered, the field is incremented.
38 #[options(count, help = "increase a counting value")]
39 count: u32,
40
41 // Option names are automatically generated from field names, but these
42 // can be overriden. The attributes `short = "?"`, `long = "..."`,
43 // `no_short`, and `no_long` are used to control option names.
44 #[options(no_short, help = "this option has no short form")]
45 long_option_only: bool,
46 }
47
main()48 fn main() {
49 // Parse options from the environment.
50 // If there's an error or the user requests help,
51 // the process will exit after giving the appropriate response.
52 let opts = MyOptions::parse_args_default_or_exit();
53
54 println!("{:#?}", opts);
55 }
56