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