|
1 |
| -<!-- omit in TOC --> |
2 | 1 | # clap
|
3 | 2 |
|
4 | 3 | > **Command Line Argument Parser for Rust**
|
|
13 | 12 |
|
14 | 13 | Dual-licensed under [Apache 2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT).
|
15 | 14 |
|
16 |
| -1. [About](#about) |
17 |
| -2. Tutorial: [Builder API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_builder/README.md), [Derive API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_derive/README.md) |
18 |
| -3. [Examples](https://github.com/clap-rs/clap/blob/v3.2.12/examples/README.md) |
19 |
| -4. [API Reference](https://docs.rs/clap) |
20 |
| - - [Derive Reference](https://github.com/clap-rs/clap/blob/v3.2.12/examples/derive_ref/README.md) |
21 |
| - - [Feature Flags](#feature-flags) |
22 |
| -5. [CHANGELOG](https://github.com/clap-rs/clap/blob/v3.2.12/CHANGELOG.md) |
23 |
| -6. [FAQ](https://github.com/clap-rs/clap/blob/v3.2.12/docs/FAQ.md) |
24 |
| -7. [Questions & Discussions](https://github.com/clap-rs/clap/discussions) |
25 |
| -8. [Contributing](https://github.com/clap-rs/clap/blob/v3.2.12/CONTRIBUTING.md) |
26 |
| -8. [Sponsors](#sponsors) |
27 |
| - |
28 | 15 | ## About
|
29 | 16 |
|
30 | 17 | Create your command-line parser, with all of the bells and whistles, declaratively or procedurally.
|
31 | 18 |
|
32 |
| -### Example |
33 |
| - |
34 |
| -Add `clap` as a dependency with the `derive` feature enabled: |
35 |
| -```console |
36 |
| -cargo add clap -F derive |
37 |
| -``` |
38 |
| - |
39 |
| -This allows using the |
40 |
| -[Derive API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_derive/README.md) |
41 |
| -which provides access to the [Builder API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_builder/README.md) as attributes on a `struct`: |
42 |
| - |
43 |
| -<!-- Copied from examples/demo.{rs,md} --> |
44 |
| -```rust,no_run |
45 |
| -use clap::Parser; |
46 |
| -
|
47 |
| -/// Simple program to greet a person |
48 |
| -#[derive(Parser, Debug)] |
49 |
| -#[clap(author, version, about, long_about = None)] |
50 |
| -struct Args { |
51 |
| - /// Name of the person to greet |
52 |
| - #[clap(short, long, value_parser)] |
53 |
| - name: String, |
54 |
| -
|
55 |
| - /// Number of times to greet |
56 |
| - #[clap(short, long, value_parser, default_value_t = 1)] |
57 |
| - count: u8, |
58 |
| -} |
59 |
| -
|
60 |
| -fn main() { |
61 |
| - let args = Args::parse(); |
62 |
| -
|
63 |
| - for _ in 0..args.count { |
64 |
| - println!("Hello {}!", args.name) |
65 |
| - } |
66 |
| -} |
67 |
| -``` |
68 |
| - |
69 |
| -```bash |
70 |
| -$ demo --help |
71 |
| -clap [..] |
72 |
| -Simple program to greet a person |
73 |
| - |
74 |
| -USAGE: |
75 |
| - demo[EXE] [OPTIONS] --name <NAME> |
76 |
| - |
77 |
| -OPTIONS: |
78 |
| - -c, --count <COUNT> Number of times to greet [default: 1] |
79 |
| - -h, --help Print help information |
80 |
| - -n, --name <NAME> Name of the person to greet |
81 |
| - -V, --version Print version information |
82 |
| -``` |
83 |
| -*(version number and `.exe` extension on windows replaced by placeholders)* |
84 |
| - |
85 |
| -### Aspirations |
86 |
| - |
87 |
| -- Out of the box, users get a polished CLI experience |
88 |
| - - Including common argument behavior, help generation, suggested fixes for users, colored output, [shell completions](https://github.com/clap-rs/clap/tree/master/clap_complete), etc |
89 |
| -- Flexible enough to port your existing CLI interface |
90 |
| - - However, we won't necessarily streamline support for each use case |
91 |
| -- Reasonable parse performance |
92 |
| -- Resilient maintainership, including |
93 |
| - - Willing to break compatibility rather than batching up breaking changes in large releases |
94 |
| - - Leverage feature flags to keep to one active branch |
95 |
| - - Being under [WG-CLI](https://github.com/rust-cli/team/) to increase the bus factor |
96 |
| -- We follow semver and will wait about 6-9 months between major breaking changes |
97 |
| -- We will support the last two minor Rust releases (MSRV, currently 1.56.1) |
98 |
| - |
99 |
| -While these aspirations can be at odds with fast build times and low binary |
100 |
| -size, we will still strive to keep these reasonable for the flexibility you |
101 |
| -get. Check out the |
102 |
| -[argparse-benchmarks](https://github.com/rust-cli/argparse-benchmarks-rs) for |
103 |
| -CLI parsers optimized for other use cases. |
104 |
| - |
105 |
| -### Selecting an API |
106 |
| - |
107 |
| -Why use the declarative [Derive API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_derive/README.md): |
108 |
| -- Easier to read, write, and modify |
109 |
| -- Easier to keep the argument declaration and reading of argument in sync |
110 |
| -- Easier to reuse, e.g. [clap-verbosity-flag](https://crates.io/crates/clap-verbosity-flag) |
111 |
| - |
112 |
| -Why use the procedural [Builder API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_builder/README.md): |
113 |
| -- Faster compile times if you aren't already using other procedural macros |
114 |
| -- More flexible, e.g. you can look up how many times an argument showed up, |
115 |
| - what its values were, and what were the indexes of those values. The Derive |
116 |
| - API can only report presence, number of occurrences, or values but no indices |
117 |
| - or combinations of data. |
118 |
| - |
119 |
| -### Related Projects |
120 |
| - |
121 |
| -- [wild](https://crates.io/crates/wild) for supporting wildcards (`*`) on Windows like you do Linux |
122 |
| -- [argfile](https://crates.io/crates/argfile) for loading additional arguments from a file (aka response files) |
123 |
| -- [shadow-rs](https://crates.io/crates/shadow-rs) for generating `Command::long_version` |
124 |
| -- [clap_lex](https://crates.io/crates/clap_lex) for a lighter-weight, battle-tested CLI parser |
125 |
| -- [clap_mangen](https://crates.io/crates/clap_mangen) for generating man page source (roff) |
126 |
| -- [clap_complete](https://crates.io/crates/clap_complete) for shell completion support |
127 |
| -- [clap-verbosity-flag](https://crates.io/crates/clap-verbosity-flag) |
128 |
| -- [clap-cargo](https://crates.io/crates/clap-cargo) |
129 |
| -- [concolor-clap](https://crates.io/crates/concolor-clap) |
130 |
| -- [Command-line Apps for Rust](https://rust-cli.github.io/book/index.html) book |
131 |
| -- [`trycmd`](https://crates.io/crates/trycmd): Snapshot testing |
132 |
| - - Or for more control, [`assert_cmd`](https://crates.io/crates/assert_cmd) and [`assert_fs`](https://crates.io/crates/assert_fs) |
133 |
| - |
134 |
| -## Feature Flags |
135 |
| - |
136 |
| -### Default Features |
137 |
| - |
138 |
| -* **std**: _Not Currently Used._ Placeholder for supporting `no_std` environments in a backwards compatible manner. |
139 |
| -* **color**: Turns on colored error messages. |
140 |
| -* **suggestions**: Turns on the `Did you mean '--myoption'?` feature for when users make typos. |
141 |
| - |
142 |
| -#### Optional features |
143 |
| - |
144 |
| -* **deprecated**: Guided experience to prepare for next breaking release (at different stages of development, this may become default) |
145 |
| -* **derive**: Enables the custom derive (i.e. `#[derive(Parser)]`). Without this you must use one of the other methods of creating a `clap` CLI listed above. |
146 |
| -* **cargo**: Turns on macros that read values from `CARGO_*` environment variables. |
147 |
| -* **env**: Turns on the usage of environment variables during parsing. |
148 |
| -* **regex**: Enables regex validators. |
149 |
| -* **unicode**: Turns on support for unicode characters (including emoji) in arguments and help messages. |
150 |
| -* **wrap_help**: Turns on the help text wrapping feature, based on the terminal size. |
151 |
| - |
152 |
| -#### Experimental features |
153 |
| - |
154 |
| -**Warning:** These may contain breaking changes between minor releases. |
155 |
| - |
156 |
| -* **unstable-replace**: Enable [`Command::replace`](https://github.com/clap-rs/clap/issues/2836) |
157 |
| -* **unstable-grouped**: Enable [`ArgMatches::grouped_values_of`](https://github.com/clap-rs/clap/issues/2924) |
158 |
| -* **unstable-v4**: Preview features which will be stable on the v4.0 release |
| 19 | +For more details, see: |
| 20 | +- [docs.rs](https://docs.rs/clap/latest/clap/) |
| 21 | +- [examples](examples/) |
159 | 22 |
|
160 | 23 | ## Sponsors
|
161 | 24 |
|
|
0 commit comments