README.md
1# StructOpt
2
3[](https://app.travis-ci.com/github/TeXitoi/structopt) [](https://crates.io/crates/structopt) [](https://docs.rs/structopt)
4[](https://github.com/rust-secure-code/safety-dance/)
5
6Parse command line arguments by defining a struct. It combines [clap](https://crates.io/crates/clap) with custom derive.
7
8## Maintenance
9
10As clap v3 is now out, and the structopt features are integrated into (almost as-is), structopt is now in maintenance mode: no new feature will be added.
11
12Bugs will be fixed, and documentation improvements will be accepted.
13
14## Documentation
15
16Find it on [Docs.rs](https://docs.rs/structopt). You can also check the [examples](https://github.com/TeXitoi/structopt/tree/master/examples) and the [changelog](https://github.com/TeXitoi/structopt/blob/master/CHANGELOG.md).
17
18## Example
19
20Add `structopt` to your dependencies of your `Cargo.toml`:
21```toml
22[dependencies]
23structopt = "0.3"
24```
25
26And then, in your rust file:
27```rust
28use std::path::PathBuf;
29use structopt::StructOpt;
30
31/// A basic example
32#[derive(StructOpt, Debug)]
33#[structopt(name = "basic")]
34struct Opt {
35 // A flag, true if used in the command line. Note doc comment will
36 // be used for the help message of the flag. The name of the
37 // argument will be, by default, based on the name of the field.
38 /// Activate debug mode
39 #[structopt(short, long)]
40 debug: bool,
41
42 // The number of occurrences of the `v/verbose` flag
43 /// Verbose mode (-v, -vv, -vvv, etc.)
44 #[structopt(short, long, parse(from_occurrences))]
45 verbose: u8,
46
47 /// Set speed
48 #[structopt(short, long, default_value = "42")]
49 speed: f64,
50
51 /// Output file
52 #[structopt(short, long, parse(from_os_str))]
53 output: PathBuf,
54
55 // the long option will be translated by default to kebab case,
56 // i.e. `--nb-cars`.
57 /// Number of cars
58 #[structopt(short = "c", long)]
59 nb_cars: Option<i32>,
60
61 /// admin_level to consider
62 #[structopt(short, long)]
63 level: Vec<String>,
64
65 /// Files to process
66 #[structopt(name = "FILE", parse(from_os_str))]
67 files: Vec<PathBuf>,
68}
69
70fn main() {
71 let opt = Opt::from_args();
72 println!("{:#?}", opt);
73}
74```
75
76Using this example:
77```
78$ ./basic
79error: The following required arguments were not provided:
80 --output <output>
81
82USAGE:
83 basic --output <output> --speed <speed>
84
85For more information try --help
86$ ./basic --help
87basic 0.3.0
88Guillaume Pinot <texitoi@texitoi.eu>, others
89A basic example
90
91USAGE:
92 basic [FLAGS] [OPTIONS] --output <output> [--] [file]...
93
94FLAGS:
95 -d, --debug Activate debug mode
96 -h, --help Prints help information
97 -V, --version Prints version information
98 -v, --verbose Verbose mode (-v, -vv, -vvv, etc.)
99
100OPTIONS:
101 -l, --level <level>... admin_level to consider
102 -c, --nb-cars <nb-cars> Number of cars
103 -o, --output <output> Output file
104 -s, --speed <speed> Set speed [default: 42]
105
106ARGS:
107 <file>... Files to process
108$ ./basic -o foo.txt
109Opt {
110 debug: false,
111 verbose: 0,
112 speed: 42.0,
113 output: "foo.txt",
114 nb_cars: None,
115 level: [],
116 files: [],
117}
118$ ./basic -o foo.txt -dvvvs 1337 -l alice -l bob --nb-cars 4 bar.txt baz.txt
119Opt {
120 debug: true,
121 verbose: 3,
122 speed: 1337.0,
123 output: "foo.txt",
124 nb_cars: Some(
125 4,
126 ),
127 level: [
128 "alice",
129 "bob",
130 ],
131 files: [
132 "bar.txt",
133 "baz.txt",
134 ],
135}
136```
137
138## StructOpt rustc version policy
139
140- Minimum rustc version modification must be specified in the [changelog](https://github.com/TeXitoi/structopt/blob/master/CHANGELOG.md) and in the [travis configuration](https://github.com/TeXitoi/structopt/blob/master/.travis.yml).
141- Contributors can increment minimum rustc version without any justification if the new version is required by the latest version of one of StructOpt's dependencies (`cargo update` will not fail on StructOpt).
142- Contributors can increment minimum rustc version if the library user experience is improved.
143
144## License
145
146Licensed under either of
147
148- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <https://www.apache.org/licenses/LICENSE-2.0>)
149- MIT license ([LICENSE-MIT](LICENSE-MIT) or <https://opensource.org/licenses/MIT>)
150
151at your option.
152
153### Contribution
154
155Unless you explicitly state otherwise, any contribution intentionally submitted
156for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
157dual licensed as above, without any additional terms or conditions.
158