3 [![Build status](https://travis-ci.com/TeXitoi/structopt.svg?branch=master)](https://app.travis-ci.com/github/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)
4 [![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)
6 Parse command line arguments by defining a struct. It combines [clap](https://crates.io/crates/clap) with custom derive.
10 Find 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).
14 Add `structopt` to your dependencies of your `Cargo.toml`:
20 And then, in your rust file:
22 use std::path::PathBuf;
23 use structopt::StructOpt;
26 #[derive(StructOpt, Debug)]
27 #[structopt(name = "basic")]
29 // A flag, true if used in the command line. Note doc comment will
30 // be used for the help message of the flag. The name of the
31 // argument will be, by default, based on the name of the field.
32 /// Activate debug mode
33 #[structopt(short, long)]
36 // The number of occurrences of the `v/verbose` flag
37 /// Verbose mode (-v, -vv, -vvv, etc.)
38 #[structopt(short, long, parse(from_occurrences))]
42 #[structopt(short, long, default_value = "42")]
46 #[structopt(short, long, parse(from_os_str))]
49 // the long option will be translated by default to kebab case,
52 #[structopt(short = "c", long)]
55 /// admin_level to consider
56 #[structopt(short, long)]
60 #[structopt(name = "FILE", parse(from_os_str))]
65 let opt = Opt::from_args();
66 println!("{:#?}", opt);
73 error: The following required arguments were not provided:
77 basic --output <output> --speed <speed>
79 For more information try --help
82 Guillaume Pinot <texitoi@texitoi.eu>, others
86 basic [FLAGS] [OPTIONS] --output <output> [--] [file]...
89 -d, --debug Activate debug mode
90 -h, --help Prints help information
91 -V, --version Prints version information
92 -v, --verbose Verbose mode (-v, -vv, -vvv, etc.)
95 -l, --level <level>... admin_level to consider
96 -c, --nb-cars <nb-cars> Number of cars
97 -o, --output <output> Output file
98 -s, --speed <speed> Set speed [default: 42]
101 <file>... Files to process
112 $ ./basic -o foo.txt -dvvvs 1337 -l alice -l bob --nb-cars 4 bar.txt baz.txt
132 ## StructOpt rustc version policy
134 - 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).
135 - 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).
136 - Contributors can increment minimum rustc version if the library user experience is improved.
140 Licensed under either of
142 - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <https://www.apache.org/licenses/LICENSE-2.0>)
143 - MIT license ([LICENSE-MIT](LICENSE-MIT) or <https://opensource.org/licenses/MIT>)
149 Unless you explicitly state otherwise, any contribution intentionally submitted
150 for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
151 dual licensed as above, without any additional terms or conditions.