clap/README.md

133 lines
5.8 KiB
Markdown
Raw Normal View History

2020-03-02 08:52:15 +00:00
<!-- omit in TOC -->
# clap
2015-03-18 23:43:33 +00:00
2021-11-30 03:05:42 +00:00
> **Command Line Argument Parser for Rust**
2020-03-02 08:52:15 +00:00
[![Crates.io](https://img.shields.io/crates/v/clap?style=flat-square)](https://crates.io/crates/clap)
[![Crates.io](https://img.shields.io/crates/d/clap?style=flat-square)](https://crates.io/crates/clap)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue?style=flat-square)](https://github.com/clap-rs/clap/blob/master/LICENSE-APACHE)
[![License](https://img.shields.io/badge/license-MIT-blue?style=flat-square)](https://github.com/clap-rs/clap/blob/master/LICENSE-MIT)
2021-10-09 18:50:16 +00:00
[![Build Status](https://img.shields.io/github/workflow/status/clap-rs/clap/CI/staging?style=flat-square)](https://github.com/clap-rs/clap/actions/workflows/ci.yml?query=branch%3Astaging)
2020-03-02 08:52:15 +00:00
[![Coverage Status](https://img.shields.io/coveralls/github/clap-rs/clap/master?style=flat-square)](https://coveralls.io/github/clap-rs/clap?branch=master)
2021-05-25 22:19:32 +00:00
[![Contributors](https://img.shields.io/github/contributors/clap-rs/clap?style=flat-square)](https://github.com/clap-rs/clap/graphs/contributors)
2015-03-18 23:43:33 +00:00
2021-11-30 03:05:42 +00:00
Dual-licensed under [Apache 2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT).
2020-03-02 08:52:15 +00:00
1. [About](#about)
2021-11-30 03:05:42 +00:00
5. [API Reference](https://docs.rs/clap)
- [Feature Flags](#feature-flags)
2. [CHANGELOG](https://github.com/clap-rs/clap/blob/master/docs/CHANGELOG.md)
2. [FAQ](https://github.com/clap-rs/clap/blob/master/docs/FAQ.md)
3. [Questions & Discussions](https://github.com/clap-rs/clap/discussions)
8. [Contributing](https://github.com/clap-rs/clap/blob/master/CONTRIBUTING.md)
## About
2021-11-30 03:05:42 +00:00
Create your command-line parser, with all of the bells and whistles, declaratively or procedurally.
2021-11-30 03:05:42 +00:00
### Example
2021-11-30 03:05:42 +00:00
<!-- Copied from examples/demo.{rs,md} -->
2021-10-23 15:46:26 +00:00
```rust,no_run
use clap::Parser;
#[derive(Parser)]
2021-11-30 03:05:42 +00:00
#[clap(about, version, author)] // Pull these from `Cargo.toml`
struct Cli {
/// Sets a custom config file. Could have been an Option<T> with no default too
2021-11-30 03:05:42 +00:00
#[clap(short, long, default_value = "default.toml", value_name = "PATH")]
config: std::path::PathBuf,
/// Some input. Because this isn't an Option<T> it's required to be used
2019-10-25 17:53:17 +00:00
input: String,
/// A level of verbosity, and can be used multiple times
2020-03-02 08:52:15 +00:00
#[clap(short, long, parse(from_occurrences))]
verbose: i32,
}
fn main() {
2021-11-30 03:05:42 +00:00
let args = Cli::parse();
2021-11-30 03:05:42 +00:00
println!("Value for config: {}", args.config.display());
println!("Using input file: {}", args.input);
// Vary the output based on how many times the user used the "verbose" flag
// (i.e. 'myprog -v -v -v' or 'myprog -vvv' vs 'myprog -v'
2021-11-30 03:05:42 +00:00
match args.verbose {
0 => println!("No verbose info"),
1 => println!("Some verbose info"),
2 => println!("Tons of verbose info"),
_ => println!("Don't be ridiculous"),
}
// more program logic goes here...
}
```
2021-11-30 03:05:42 +00:00
```bash
$ demo --help
clap [..]
2021-11-30 03:05:42 +00:00
Kevin K. <kbknapp@gmail.com>, Clap Maintainers
2020-03-02 08:52:15 +00:00
2021-11-30 03:05:42 +00:00
A simple to use, efficient, and full-featured Command Line Argument Parser
2015-03-01 00:22:23 +00:00
2021-11-30 03:05:42 +00:00
USAGE:
demo[EXE] [OPTIONS] <INPUT>
2015-03-16 14:38:43 +00:00
ARGS:
2021-11-30 03:05:42 +00:00
<INPUT> Some input. Because this isn't an Option<T> it's required to be used
2015-03-16 14:38:43 +00:00
OPTIONS:
2021-11-30 03:05:42 +00:00
-c, --config <PATH> Sets a custom config file. Could have been an Option<T> with no default
too [default: default.toml]
-h, --help Print help information
2021-11-30 03:05:42 +00:00
-v, --verbose A level of verbosity, and can be used multiple times
-V, --version Print version information
2015-03-16 14:38:43 +00:00
```
2021-11-30 03:05:42 +00:00
*(version number and `.exe` extension on windows replaced by placeholders)*
2021-11-30 03:05:42 +00:00
### Aspirations
2021-11-30 03:05:42 +00:00
- Out of the box, users get a polished CLI experience
- Including common argument behavior, help generation, suggested fixes for users, colored output, [shell completions](https://github.com/clap-rs/clap/tree/master/clap_generate), etc
- Flexible enough to port your existing CLI interface
- However, we won't necessarily streamline support for each use case
- Reasonable parse performance
- We will support the last two minor Rust releases (MSRV)
- We follow semver and will wait about 6 months between major breaking changes
2021-11-30 03:05:42 +00:00
While these aspirations can be at odds with fast build times and low binary
size, we will still strive to keep these reasonable. Check out the
[argparse-benchmarks](https://github.com/rust-cli/argparse-benchmarks-rs) for
CLI parsers optimized for other use cases.
2021-11-30 03:05:42 +00:00
### Related Projects
2021-11-30 03:05:42 +00:00
- [Command-line Apps for Rust](https://rust-cli.github.io/book/index.html) book
- [`trycmd`](https://github.com/epage/trycmd): Snapshot testing
- Or for more control, [`assert_cmd`](https://github.com/assert-rs/assert_cmd) and [`assert_fs`](https://github.com/assert-rs/assert_fs)
2021-11-30 03:05:42 +00:00
## Feature Flags
2021-08-14 00:04:49 +00:00
2021-11-30 03:05:42 +00:00
### Default Features
2021-08-14 00:04:49 +00:00
* **std**: _Not Currently Used._ Placeholder for supporting `no_std` environments in a backwards compatible manner.
2021-11-30 03:05:42 +00:00
* **color**: Turns on colored error messages.
* **suggestions**: Turns on the `Did you mean '--myoption'?` feature for when users make typos.
2021-11-30 03:05:42 +00:00
#### Optional features
2021-11-30 03:05:42 +00:00
* **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.
* **cargo**: Turns on macros that read values from `CARGO_*` environment variables.
* **env**: Turns on the usage of environment variables during parsing.
2021-11-30 03:05:42 +00:00
* **regex**: Enables regex validators.
* **unicode**: Turns on support for unicode characters (including emoji) in arguments and help messages.
* **wrap_help**: Turns on the help text wrapping feature, based on the terminal size.
#### Experimental features
2021-11-30 03:05:42 +00:00
**Warning:** These may contain breaking changes between minor releases.
2021-10-09 18:50:16 +00:00
* **unstable-replace**: Enable [`App::replace`](https://github.com/clap-rs/clap/issues/2836)
2021-10-12 19:51:26 +00:00
* **unstable-multicall**: Enable [`AppSettings::Multicall`](https://github.com/clap-rs/clap/issues/2861)
* **unstable-grouped**: Enable [`ArgMatches::grouped_values_of`](https://github.com/clap-rs/clap/issues/2924)