2020-03-02 08:52:15 +00:00
<!-- omit in TOC -->
# clap
2015-03-18 23:43:33 +00:00
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)
[![Coverage Status ](https://img.shields.io/coveralls/github/clap-rs/clap/master?style=flat-square )](https://coveralls.io/github/clap-rs/clap?branch=master)
[![Linux Build Status ](https://img.shields.io/travis/clap-rs/clap/master?style=flat-square&logo=linux&logoColor=fff )](https://travis-ci.org/clap-rs/clap)
[![Windows Build Status ](https://img.shields.io/azure-devops/build/clap-rs/3b9343e1-29b0-47be-acec-1edcdab69de9/1/master?style=flat-square&logo=windows )](https://dev.azure.com/clap-rs/clap/_build/latest?definitionId=1& branchName=master)
2015-03-18 23:43:33 +00:00
2015-04-16 14:59:34 +00:00
Command Line Argument Parser for Rust
2015-03-01 00:22:23 +00:00
2020-03-02 08:52:15 +00:00
It is a simple-to-use, efficient, and full-featured library for parsing command line arguments and subcommands when writing command line, console or terminal applications.
2015-03-01 00:22:23 +00:00
2020-03-02 08:52:15 +00:00
* [Documentation][docs]
2020-04-01 20:32:51 +00:00
* [Questions & Discussions ](https://github.com/clap-rs/clap/discussions )
2020-03-27 15:00:47 +00:00
* [Website ](https://clap.rs/ )
2020-03-02 08:52:15 +00:00
We are currently hard at work trying to release `3.0` . We have a `3.0.0-beta.1` prerelease out but we do not give any guarantees that it's API is stable. We do not have a changelog yet which will be written down after we are sure about the API stability. We recommend users to not update to the prerelease version yet and to wait for the official `3.0` .
> If you're looking for the readme & examples for `clap v2.33` - find it on [github](https://github.com/clap-rs/clap/tree/v2.33.0), [crates.io](https://crates.io/crates/clap/2.33.0), [docs.rs](https://docs.rs/clap/2.33.0/clap/).
1. [About ](#about )
2. [FAQ ](#faq )
3. [Features ](#features )
4. [Quick Example ](#quick-example )
1. [Using Derive Macros ](#using-derive-macros )
2. [Using Builder Pattern ](#using-builder-pattern )
3. [Using YAML ](#using-yaml )
4. [Using Macros ](#using-macros )
5. [Running it ](#running-it )
5. [Try it! ](#try-it )
1. [Pre-Built Test ](#pre-built-test )
2. [Build Your Own Binary ](#build-your-own-binary )
6. [Usage ](#usage )
1. [Optional Dependencies / Features ](#optional-dependencies--features )
1. [Features enabled by default ](#features-enabled-by-default )
2. [Opt-in features ](#opt-in-features )
2. [More Information ](#more-information )
7. [Contributing ](#contributing )
1. [Compatibility Policy ](#compatibility-policy )
1. [Minimum Supported Version of Rust (MSRV) ](#minimum-supported-version-of-rust-msrv )
2. [Breaking Changes ](#breaking-changes )
8. [License ](#license )
9. [Related Crates ](#related-crates )
2015-06-30 02:22:13 +00:00
2015-04-06 17:39:13 +00:00
## About
2017-11-03 03:15:02 +00:00
`clap` is used to parse *and validate* the string of command line arguments provided by a user at runtime. You provide the list of valid possibilities, and `clap` handles the rest. This means you focus on your *applications* functionality, and less on the parsing and validating of arguments.
2015-03-01 00:22:23 +00:00
2017-11-03 03:15:02 +00:00
`clap` provides many things 'for free' (with no configuration) including the traditional version and help switches (or flags) along with associated messages. If you are using subcommands, `clap` will also auto-generate a `help` subcommand and separate associated help messages.
2015-03-16 14:38:43 +00:00
2017-11-03 03:15:02 +00:00
Once `clap` parses the user provided string of arguments, it returns the matches along with any applicable values. If the user made an error or typo, `clap` informs them with a friendly message and exits gracefully (or returns a `Result` type and allows you to perform any clean up prior to exit). Because of this, you can make reasonable assumptions in your code about the validity of the arguments prior to your applications main execution.
2015-03-01 00:22:23 +00:00
2015-10-29 05:07:41 +00:00
## FAQ
2020-03-02 08:52:15 +00:00
For a full FAQ, see [this ](FAQ.md )
2017-11-03 03:15:02 +00:00
2015-03-31 17:56:02 +00:00
## Features
2020-03-02 08:52:15 +00:00
Below are a few of the features which `clap` supports, full descriptions and usage can be found in the [documentation][docs] and [examples][examples] directory
2015-03-31 17:56:02 +00:00
2018-08-02 03:05:52 +00:00
* Generate a CLI simply by defining a struct!
2015-03-31 17:56:02 +00:00
* **Auto-generated Help, Version, and Usage information**
2017-11-03 03:15:02 +00:00
- Can optionally be fully, or partially overridden if you want a custom help, version, or usage statements
2018-08-02 03:05:52 +00:00
* **Auto-generated completion scripts (Bash, Zsh, Fish, Elvish and PowerShell)**
2020-04-17 20:30:40 +00:00
- Using [`clap_generate` ](https://github.com/clap-rs/clap/tree/master/clap_generate )
2016-07-01 04:25:01 +00:00
- Even works through many multiple levels of subcommands
2016-07-01 17:07:08 +00:00
- Works with options which only accept certain values
- Works with subcommand aliases
2015-03-31 17:56:02 +00:00
* **Flags / Switches** (i.e. bool fields)
- Both short and long versions supported (i.e. `-f` and `--flag` respectively)
- Supports combining short versions (i.e. `-fBgoZ` is the same as `-f -B -g -o -Z` )
2016-01-27 21:37:09 +00:00
- Supports multiple occurrences (i.e. `-vvv` or `-v -v -v` )
2015-04-09 18:25:03 +00:00
* **Positional Arguments** (i.e. those which are based off an index from the program name)
2016-01-27 21:37:09 +00:00
- Supports multiple values (i.e. `myprog <file>...` such as `myprog file1.txt file2.txt` being two values for the same "file" argument)
- Supports Specific Value Sets (See below)
- Can set value parameters (such as the minimum number of values, the maximum number of values, or the exact number of values)
- Can set custom validations on values to extend the argument parsing capability to truly custom domains
* **Option Arguments** (i.e. those that take values)
- Both short and long versions supported (i.e. `-o value` , `-ovalue` , `-o=value` and `--option value` or `--option=value` respectively)
- Supports multiple values (i.e. `-o <val1> -o <val2>` or `-o <val1> <val2>` )
- Supports delimited values (i.e. `-o=val1,val2,val3` , can also change the delimiter)
- Supports Specific Value Sets (See below)
- Supports named values so that the usage/help info appears as `-o <FILE> <INTERFACE>` etc. for when you require specific multiple values
- Can set value parameters (such as the minimum number of values, the maximum number of values, or the exact number of values)
- Can set custom validations on values to extend the argument parsing capability to truly custom domains
2015-03-31 17:56:02 +00:00
* **Sub-Commands** (i.e. `git add <file>` where `add` is a sub-command of `git` )
2015-05-06 00:21:13 +00:00
- Support their own sub-arguments, and sub-sub-commands independent of the parent
- Get their own auto-generated Help, Version, and Usage independent of parent
2016-01-27 21:37:09 +00:00
* **Support for building CLIs from YAML** - This keeps your Rust source nice and tidy and makes supporting localized translation very simple!
* **Requirement Rules**: Arguments can define the following types of requirement rules
- Can be required by default
- Can be required only if certain arguments are present
2015-03-31 17:56:02 +00:00
- Can require other arguments to be present
2017-11-03 03:15:02 +00:00
- Can be required only if certain values of other arguments are used
2016-01-27 21:37:09 +00:00
* **Confliction Rules**: Arguments can optionally define the following types of exclusion rules
2015-03-31 17:56:02 +00:00
- Can be disallowed when certain arguments are present
- Can disallow use of other arguments when present
2016-01-27 21:37:09 +00:00
* **Groups**: Arguments can be made part of a group
- Fully compatible with other relational rules (requirements, conflicts, and overrides) which allows things like requiring the use of any arg in a group, or denying the use of an entire group conditionally
* **Specific Value Sets**: Positional or Option Arguments can define a specific set of allowed values (i.e. imagine a `--mode` option which may *only* have one of two values `fast` or `slow` such as `--mode fast` or `--mode slow` )
2016-09-13 01:59:46 +00:00
* **Default Values**
2017-11-03 03:15:02 +00:00
- Also supports conditional default values (i.e. a default which only applies if specific arguments are used, or specific values of those arguments)
2015-09-29 20:25:15 +00:00
* **Automatic Version from Cargo.toml**: `clap` is fully compatible with Rust's `env!()` macro for automatically setting the version of your application to the version in your Cargo.toml. See [09_auto_version example ](examples/09_auto_version.rs ) for how to do this (Thanks to [jhelwig ](https://github.com/jhelwig ) for pointing this out)
2020-05-01 20:32:43 +00:00
* **Typed Values**: You can use several convenience macros provided by `clap` to get typed values (i.e. `i32` , `u8` , etc.) from positional or option arguments so long as the type you request implements `std::str::FromStr` See the [12_typed_values example ](examples/12_typed_values.rs ). You can also use `clap` s `arg_enum!` macro to create an enum with variants that automatically implement `std::str::FromStr` . See [13_enum_values example ](examples/13_enum_values.rs ) for details
2016-07-05 20:01:04 +00:00
* **Suggestions**: Suggests corrections when the user enters a typo. For example, if you defined a `--myoption` argument, and the user mistakenly typed `--moyption` (notice `y` and `o` transposed), they would receive a `Did you mean '--myoption'?` error and exit gracefully. This also works for subcommands and flags. (Thanks to [Byron ](https://github.com/Byron ) for the implementation) (This feature can optionally be disabled, see 'Optional Dependencies / Features')
2020-05-30 09:55:06 +00:00
* **Colorized Errors (Non Windows OS only)**: Error message are printed in colored text (this feature can optionally be disabled, see 'Optional Dependencies / Features').
2017-11-03 03:15:02 +00:00
* **Global Arguments**: Arguments can optionally be defined once, and be available to all child subcommands. There values will also be propagated up/down throughout all subcommands.
2015-09-20 23:32:26 +00:00
* **Custom Validations**: You can define a function to use as a validator of argument values. Imagine defining a function to validate IP addresses, or fail parsing upon error. This means your application logic can be solely focused on *using* values.
2016-09-13 01:59:46 +00:00
* **POSIX Compatible Conflicts/Overrides** - In POSIX args can be conflicting, but not fail parsing because whichever arg comes *last* "wins" so to speak. This allows things such as aliases (i.e. `alias ls='ls -l'` but then using `ls -C` in your terminal which ends up passing `ls -l -C` as the final arguments. Since `-l` and `-C` aren't compatible, this effectively runs `ls -C` in `clap` if you choose...`clap` also supports hard conflicts that fail parsing). (Thanks to [Vinatorul ](https://github.com/Vinatorul )!)
2016-04-21 04:51:09 +00:00
* Supports the Unix `--` meaning, only positional arguments follow
2015-03-31 17:56:02 +00:00
2015-03-16 14:55:45 +00:00
## Quick Example
2015-05-05 23:31:41 +00:00
2020-03-02 08:52:15 +00:00
The following examples show a quick example of some of the very basic functionality of `clap` . For more advanced usage, such as requirements, conflicts, groups, multiple values and occurrences see the [documentation][docs], [examples][examples] directory of this repository.
2015-05-05 23:31:41 +00:00
2017-11-03 03:15:02 +00:00
**NOTE:** All of these examples are functionally the same, but show different styles in which to use `clap` . These different styles are purely a matter of personal preference.
2015-05-05 23:31:41 +00:00
2020-03-02 08:52:15 +00:00
Add `clap` to your `Cargo.toml`
```toml
[dependencies]
clap = "3.0.0-beta.1"
```
#### Using Derive Macros
2018-08-02 03:05:52 +00:00
The first example shows the simplest way to use `clap` , by defining a struct. If you're familiar with the `structopt` crate you're in luck, it's the same! (In fact it's the exact same code running under the covers!)
2016-01-27 21:37:09 +00:00
2020-05-02 12:11:50 +00:00
```rust,no_run
2018-08-02 03:05:52 +00:00
// (Full example with detailed comments in examples/01d_quick_example.rs)
2015-04-28 02:52:50 +00:00
//
2018-08-02 03:05:52 +00:00
// This example demonstrates clap's full 'custom derive' style of creating arguments which is the
// simplest method of use, but sacrifices some flexibility.
2020-01-09 03:56:27 +00:00
use clap::Clap;
2018-08-02 03:05:52 +00:00
/// This doc string acts as a help message when the user runs '--help'
/// as do all doc strings on fields
#[derive(Clap)]
2020-03-02 08:52:15 +00:00
#[clap(version = "1.0", author = "Kevin K. <kbknapp@gmail.com>")]
2018-08-02 03:05:52 +00:00
struct Opts {
/// Sets a custom config file. Could have been an Option< T > with no default too
2020-03-02 08:52:15 +00:00
#[clap(short, long, default_value = "default.conf")]
2019-11-29 23:35:26 +00:00
config: String,
2018-08-02 03:05:52 +00:00
/// 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,
2018-08-02 03:05:52 +00:00
/// A level of verbosity, and can be used multiple times
2020-03-02 08:52:15 +00:00
#[clap(short, long, parse(from_occurrences))]
2019-10-31 00:26:41 +00:00
verbose: i32,
2018-08-02 03:05:52 +00:00
#[clap(subcommand)]
subcmd: SubCommand,
}
#[derive(Clap)]
enum SubCommand {
2020-03-02 08:52:15 +00:00
#[clap(version = "1.3", author = "Someone E. < someone_else @ other . com > ")]
2019-10-31 00:26:41 +00:00
Test(Test),
2018-08-02 03:05:52 +00:00
}
2019-10-31 00:26:41 +00:00
/// A subcommand for controlling testing
#[derive(Clap)]
2019-11-29 23:35:26 +00:00
struct Test {
2019-10-31 00:26:41 +00:00
/// Print debug info
2020-03-02 08:52:15 +00:00
#[clap(short)]
2019-10-31 00:26:41 +00:00
debug: bool
}
2018-08-02 03:05:52 +00:00
fn main() {
let opts: Opts = Opts::parse();
// Gets a value for config if supplied by user, or defaults to "default.conf"
println!("Value for config: {}", opts.config);
println!("Using input file: {}", opts.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'
2019-10-31 00:26:41 +00:00
match opts.verbose {
2018-08-02 03:05:52 +00:00
0 => println!("No verbose info"),
1 => println!("Some verbose info"),
2 => println!("Tons of verbose info"),
3 | _ => println!("Don't be crazy"),
}
// You can handle information about subcommands by requesting their matches by name
// (as below), requesting just the name used, or both at the same time
2019-10-05 13:39:18 +00:00
match opts.subcmd {
2019-10-31 00:26:41 +00:00
SubCommand::Test(t) => {
2018-08-02 03:05:52 +00:00
if t.debug {
println!("Printing debug info...");
} else {
println!("Printing normally...");
}
}
}
// more program logic goes here...
}
```
2020-03-02 08:52:15 +00:00
#### Using Builder Pattern
2018-08-02 03:05:52 +00:00
This second method shows a method using the 'Builder Pattern' which allows more advanced configuration options (not shown in this small example), or even dynamically generating arguments when desired. The downside is it's more verbose.
2020-05-02 12:11:50 +00:00
```rust,no_run
2018-08-02 03:05:52 +00:00
// (Full example with detailed comments in examples/01a_quick_example.rs)
//
// This example demonstrates clap's "builder pattern" method of creating arguments
// which the most flexible, but also most verbose.
2020-03-02 08:52:15 +00:00
use clap::{Arg, App};
2015-04-13 18:19:56 +00:00
fn main() {
2016-01-27 21:37:09 +00:00
let matches = App::new("My Super Program")
2020-03-02 08:52:15 +00:00
.version("1.0")
.author("Kevin K. < kbknapp @ gmail . com > ")
.about("Does awesome things")
2020-05-14 20:50:56 +00:00
.arg(Arg::new("config")
2020-03-02 08:52:15 +00:00
.short('c')
.long("config")
.value_name("FILE")
2020-05-02 12:11:50 +00:00
.about("Sets a custom config file")
2020-03-02 08:52:15 +00:00
.takes_value(true))
2020-05-14 20:50:56 +00:00
.arg(Arg::new("INPUT")
2020-05-02 12:11:50 +00:00
.about("Sets the input file to use")
2020-03-02 08:52:15 +00:00
.required(true)
.index(1))
2020-05-14 20:50:56 +00:00
.arg(Arg::new("v")
2020-03-02 08:52:15 +00:00
.short('v')
.multiple(true)
2020-05-02 12:11:50 +00:00
.about("Sets the level of verbosity"))
2020-03-02 08:52:15 +00:00
.subcommand(App::new("test")
.about("controls testing features")
.version("1.3")
.author("Someone E. < someone_else @ other . com > ")
2020-05-14 20:50:56 +00:00
.arg(Arg::new("debug")
2020-03-02 08:52:15 +00:00
.short('d')
2020-05-02 12:11:50 +00:00
.about("print debug information verbosely")))
2020-03-02 08:52:15 +00:00
.get_matches();
2015-04-13 18:19:56 +00:00
2018-08-02 03:05:52 +00:00
// Same as above examples...
2015-04-13 18:19:56 +00:00
}
```
2015-04-13 18:29:40 +00:00
2016-11-21 01:48:33 +00:00
The next example shows a far less verbose method, but sacrifices some of the advanced configuration options (not shown in this small example). This method also takes a *very* minor runtime penalty.
2015-05-05 23:31:41 +00:00
2020-05-02 12:11:50 +00:00
```rust,no_run
2016-01-27 21:37:09 +00:00
// (Full example with detailed comments in examples/01a_quick_example.rs)
2015-04-28 02:52:50 +00:00
//
2016-10-15 02:58:36 +00:00
// This example demonstrates clap's "usage strings" method of creating arguments
// which is less verbose
2020-03-02 08:52:15 +00:00
use clap::{Arg, App};
2015-03-01 00:22:23 +00:00
2015-03-16 14:55:45 +00:00
fn main() {
2015-04-09 18:25:03 +00:00
let matches = App::new("myapp")
2020-03-02 08:52:15 +00:00
.version("1.0")
.author("Kevin K. < kbknapp @ gmail . com > ")
.about("Does awesome things")
2020-05-02 12:11:50 +00:00
.arg("-c, --config=[FILE] 'Sets a custom config file'")
.arg("< INPUT > 'Sets the input file to use'")
.arg("-v... 'Sets the level of verbosity'")
2020-03-02 08:52:15 +00:00
.subcommand(App::new("test")
.about("controls testing features")
.version("1.3")
.author("Someone E. < someone_else @ other . com > ")
.arg("-d, --debug 'Print debug information'"))
.get_matches();
2015-03-16 19:56:42 +00:00
2016-01-27 21:37:09 +00:00
// Same as previous example...
2015-03-16 14:38:43 +00:00
}
```
2015-09-09 04:00:17 +00:00
2020-03-02 08:52:15 +00:00
#### Using YAML
2016-11-21 01:48:33 +00:00
This third method shows how you can use a YAML file to build your CLI and keep your Rust source tidy
2016-10-15 02:58:36 +00:00
or support multiple localized translations by having different YAML files for each localization.
2016-11-21 01:48:33 +00:00
2020-07-20 23:22:54 +00:00
First, create the `cli.yaml` file to hold your CLI options, but it could be called anything we like:
2015-09-01 04:28:33 +00:00
```yaml
name: myapp
2016-11-12 06:55:25 +00:00
version: "1.0"
2015-09-01 04:28:33 +00:00
author: Kevin K. < kbknapp @ gmail . com >
about: Does awesome things
args:
2016-01-27 21:37:09 +00:00
- config:
2015-09-01 04:28:33 +00:00
short: c
long: config
2016-01-27 21:37:09 +00:00
value_name: FILE
2020-03-02 08:52:15 +00:00
about: Sets a custom config file
2015-09-01 04:28:33 +00:00
takes_value: true
- INPUT:
2020-03-02 08:52:15 +00:00
about: Sets the input file to use
2015-09-01 04:28:33 +00:00
required: true
index: 1
2016-01-27 21:37:09 +00:00
- verbose:
short: v
2015-09-01 04:28:33 +00:00
multiple: true
2020-03-02 08:52:15 +00:00
about: Sets the level of verbosity
2015-09-01 04:28:33 +00:00
subcommands:
- test:
about: controls testing features
2016-10-29 14:59:41 +00:00
version: "1.3"
2015-09-01 04:28:33 +00:00
author: Someone E. < someone_else @ other . com >
args:
2016-01-27 21:37:09 +00:00
- debug:
short: d
2020-03-02 08:52:15 +00:00
about: print debug information
2015-09-01 04:28:33 +00:00
```
2016-11-21 01:48:33 +00:00
Since this feature requires additional dependencies that not everyone may want, it is *not* compiled in by default and we need to enable a feature flag in Cargo.toml:
2020-03-02 08:52:15 +00:00
Simply add the `yaml` feature flag to your `Cargo.toml` .
```toml
[dependencies]
clap = { version = "3.0.0-beta.1", features = ["yaml"] }
```
2016-10-15 21:37:59 +00:00
2017-11-03 03:15:02 +00:00
Finally we create our `main.rs` file just like we would have with the previous two examples:
2015-09-01 04:28:33 +00:00
2020-05-02 12:11:50 +00:00
```rust,ignore
2015-09-01 04:28:33 +00:00
// (Full example with detailed comments in examples/17_yaml.rs)
//
// This example demonstrates clap's building from YAML style of creating arguments which is far
// more clean, but takes a very small performance hit compared to the other two methods.
2020-01-09 03:56:27 +00:00
use clap::{App, load_yaml};
2015-09-01 04:28:33 +00:00
fn main() {
// The YAML file is found relative to the current file, similar to how modules are found
2020-07-20 23:22:54 +00:00
let yaml = load_yaml!("cli.yaml");
2019-10-25 17:53:17 +00:00
let matches = App::from(yaml).get_matches();
2015-09-01 04:28:33 +00:00
2016-01-27 21:37:09 +00:00
// Same as previous examples...
2015-09-01 04:28:33 +00:00
}
```
2020-03-02 08:52:15 +00:00
#### Using Macros
Finally there is a macro version, which is like a hybrid approach offering the speed of the
builder pattern (the first example), but without all the verbosity.
2020-05-02 12:11:50 +00:00
```rust,no_run
2020-03-02 08:52:15 +00:00
use clap::clap_app;
fn main() {
let matches = clap_app!(myapp =>
(version: "1.0")
(author: "Kevin K. < kbknapp @ gmail . com > ")
(about: "Does awesome things")
(@arg CONFIG: -c --config +takes_value "Sets a custom config file")
(@arg INPUT: +required "Sets the input file to use")
2020-06-28 12:34:48 +00:00
(@arg verbose: -v --verbose "Print test information verbosely")
2020-03-02 08:52:15 +00:00
(@subcommand test =>
(about: "controls testing features")
(version: "1.3")
(author: "Someone E. < someone_else @ other . com > ")
2020-06-28 12:34:48 +00:00
(@arg debug: -d ... "Sets the level of debugging information")
2020-03-02 08:52:15 +00:00
)
).get_matches();
// Same as previous examples...
}
```
#### Running it
2018-08-02 03:05:52 +00:00
If you were to compile any of the above programs and run them with the flag `--help` or `-h` (or `help` subcommand, since we defined `test` as a subcommand) the following would be output (except the first example where the help message sort of explains the Rust code).
2015-03-01 00:22:23 +00:00
2020-05-02 12:11:50 +00:00
```bash
2016-01-27 21:37:09 +00:00
$ myprog --help
My Super Program 1.0
2015-03-16 14:38:43 +00:00
Kevin K. < kbknapp @ gmail . com >
Does awesome things
2018-08-02 03:05:52 +00:00
ARGS:
INPUT The input file to use
2015-03-16 14:38:43 +00:00
USAGE:
2015-07-09 15:46:12 +00:00
MyApp [FLAGS] [OPTIONS] < INPUT > [SUBCOMMAND]
2015-03-16 14:38:43 +00:00
FLAGS:
2017-11-03 03:15:02 +00:00
-h, --help Prints help information
2016-01-27 21:37:09 +00:00
-v Sets the level of verbosity
2015-06-17 00:46:11 +00:00
-V, --version Prints version information
2015-05-05 23:31:41 +00:00
2015-03-16 14:38:43 +00:00
OPTIONS:
2016-01-27 21:37:09 +00:00
-c, --config < FILE > Sets a custom config file
2015-03-01 00:22:23 +00:00
2015-03-16 14:38:43 +00:00
SUBCOMMANDS:
2017-11-03 03:15:02 +00:00
help Prints this message or the help of the given subcommand(s)
2015-04-09 18:25:03 +00:00
test Controls testing features
2015-03-16 14:38:43 +00:00
```
2015-03-01 00:33:18 +00:00
2017-11-03 03:15:02 +00:00
**NOTE:** You could also run `myapp test --help` or `myapp help test` to see the help message for the `test` subcommand.
2015-04-09 18:25:03 +00:00
2015-04-28 02:52:50 +00:00
## Try it!
2015-03-31 17:56:02 +00:00
2015-04-28 02:52:50 +00:00
### Pre-Built Test
2020-03-02 08:52:15 +00:00
To try out the pre-built [examples][examples], use the following steps:
2015-04-28 02:52:50 +00:00
2018-08-02 03:05:52 +00:00
* Clone the repository `$ git clone https://github.com/clap-rs/clap && cd clap/`
2017-11-27 15:28:28 +00:00
* Compile the example `$ cargo build --example <EXAMPLE>`
* Run the help info `$ ./target/debug/examples/<EXAMPLE> --help`
2015-04-28 02:52:50 +00:00
* Play with the arguments!
2018-06-29 22:31:57 +00:00
* You can also do a onetime run via `$ cargo run --example <EXAMPLE> -- [args to example]`
2015-04-28 02:52:50 +00:00
2020-03-02 08:52:15 +00:00
### Build Your Own Binary
2015-04-28 02:52:50 +00:00
2015-04-28 03:25:39 +00:00
To test out `clap` 's default auto-generated help/version follow these steps:
2015-04-28 02:52:50 +00:00
* Create a new cargo project `$ cargo new fake --bin && cd fake`
2020-06-25 13:01:52 +00:00
* Write your program as described in the quick example section.
2015-04-28 02:52:50 +00:00
* Build your program `$ cargo build --release`
* Run with help or version `$ ./target/release/fake --help` or `$ ./target/release/fake --version`
## Usage
2020-03-02 08:52:15 +00:00
For full usage, add `clap` as a dependency in your `Cargo.toml` to use from crates.io:
2015-03-01 00:33:18 +00:00
2016-10-15 02:58:36 +00:00
```toml
[dependencies]
2020-03-02 08:52:15 +00:00
clap = "3.0.0-beta.1"
2016-10-15 02:58:36 +00:00
```
2020-03-02 08:52:15 +00:00
Define a list of valid arguments for your program (see the [documentation][docs] or [examples][examples] directory of this repo)
2015-03-16 14:38:43 +00:00
2015-03-31 17:56:02 +00:00
Then run `cargo build` or `cargo update && cargo build` for your project.
2015-03-16 14:38:43 +00:00
2015-05-05 23:31:41 +00:00
### Optional Dependencies / Features
2016-10-15 02:58:36 +00:00
#### Features enabled by default
2018-08-02 03:05:52 +00:00
* **derive**: Enables the custom derive (i.e. `#[derive(Clap)]` ). Without this you must use one of the other methods of creating a `clap` CLI listed above
2020-04-04 20:16:30 +00:00
* **suggestions**: Turns on the `Did you mean '--myoption'?` feature for when users make typos. (builds dependency `strsim` )
2020-03-02 08:52:15 +00:00
* **color**: Turns on colored error messages. (builds dependency `termcolor` )
2016-10-15 02:58:36 +00:00
To disable these, add this to your `Cargo.toml` :
2015-05-05 23:31:41 +00:00
```toml
[dependencies.clap]
2018-11-09 01:34:13 +00:00
version = "3.0.0-beta.1"
2015-05-05 23:31:41 +00:00
default-features = false
```
You can also selectively enable only the features you'd like to include, by adding:
```toml
[dependencies.clap]
2018-11-09 01:34:13 +00:00
version = "3.0.0-beta.1"
2015-05-05 23:31:41 +00:00
default-features = false
# Cherry-pick the features you'd like to use
features = [ "suggestions", "color" ]
```
2016-10-15 02:58:36 +00:00
#### Opt-in features
* **"yaml"**: Enables building CLIs from YAML documents. (builds dependency `yaml-rust` )
2016-11-20 14:32:18 +00:00
* **"unstable"**: Enables unstable `clap` features that may change from release to release
2018-02-25 12:51:11 +00:00
* **"wrap_help"**: Turns on the help text wrapping feature, based on the terminal size. (builds dependency `term-size` )
2015-05-05 23:31:41 +00:00
2015-04-08 21:08:59 +00:00
### More Information
2015-03-16 14:38:43 +00:00
2020-03-02 08:52:15 +00:00
You can find complete documentation on the [docs.rs][docs] for this project.
2015-04-16 15:01:24 +00:00
2020-03-02 08:52:15 +00:00
You can also find usage examples in the [examples][examples] directory of this repo.
2015-04-16 14:59:34 +00:00
2020-03-02 08:52:15 +00:00
## Contributing
2015-03-18 23:45:30 +00:00
2020-05-02 18:56:35 +00:00
Details on how to contribute can be found in the [CONTRIBUTING.md ](CONTRIBUTING.md ) file.
2015-03-31 17:56:02 +00:00
2016-11-20 14:30:56 +00:00
### Compatibility Policy
2016-12-06 19:52:24 +00:00
Because `clap` takes SemVer and compatibility seriously, this is the official policy regarding breaking changes and minimum required versions of Rust.
2016-11-20 14:30:56 +00:00
`clap` will pin the minimum required version of Rust to the CI builds. Bumping the minimum version of Rust is considered a minor breaking change, meaning *at a minimum* the minor version of `clap` will be bumped.
2017-04-22 21:11:53 +00:00
In order to keep from being surprised of breaking changes, it is **highly** recommended to use the `~major.minor.patch` style in your `Cargo.toml` only if you wish to target a version of Rust that is *older* than current stable minus two releases:
2016-11-20 14:30:56 +00:00
```toml
[dependencies]
2018-11-09 01:34:13 +00:00
clap = "~3.0.0-beta.1"
2016-11-20 14:30:56 +00:00
```
This will cause *only* the patch version to be updated upon a `cargo update` call, and therefore cannot break due to new features, or bumped minimum versions of Rust.
2020-03-05 03:06:06 +00:00
#### Minimum Supported Version of Rust (MSRV)
2016-11-20 14:30:56 +00:00
2019-10-30 20:56:27 +00:00
`clap` will officially support current stable Rust, minus two releases, but may work with prior releases as well. For example, current stable Rust at the time of this writing is 1.38.0, meaning `clap` is guaranteed to compile with 1.36.0 and beyond.
2017-10-24 22:31:33 +00:00
2019-10-30 20:56:27 +00:00
At the 1.39.0 stable release, `clap` will be guaranteed to compile with 1.37.0 and beyond, etc.
2016-11-20 14:30:56 +00:00
2020-03-02 08:52:15 +00:00
The following is a list of the minimum required version of Rust to compile `clap` by our `MAJOR.MINOR` version number:
| clap | MSRV |
| :----: | :----: |
| >=3.0 | 1.40.0 |
| >=2.21 | 1.24.0 |
| >=2.2 | 1.12.0 |
| >=2.1 | 1.6.0 |
| >=1.5 | 1.4.0 |
| >=1.4 | 1.2.0 |
| >=1.2 | 1.1.0 |
| >=1.0 | 1.0.0 |
2020-03-05 03:06:06 +00:00
2016-12-06 19:52:24 +00:00
#### Breaking Changes
2017-04-22 21:11:53 +00:00
`clap` takes a similar policy to Rust and will bump the major version number upon breaking changes with only the following exceptions:
2016-12-06 19:52:24 +00:00
* The breaking change is to fix a security concern
* The breaking change is to be fixing a bug (i.e. relying on a bug as a feature)
* The breaking change is a feature isn't used in the wild, or all users of said feature have given approval *prior* to the change
2015-04-08 21:08:59 +00:00
## License
2018-07-23 18:25:18 +00:00
`clap` is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See the [LICENSE-APACHE ](LICENSE-APACHE ) and [LICENSE-MIT ](LICENSE-MIT ) files in this repository for more information.
2015-04-30 23:58:34 +00:00
2017-11-03 03:15:02 +00:00
## Related Crates
There are several excellent crates which can be used with `clap` , I recommend checking them all out! If you've got a crate that would be a good fit to be used with `clap` open an issue and let me know, I'd love to add it!
2019-10-30 02:25:22 +00:00
* [`assert_cmd` ](https://github.com/assert-rs/assert_cmd ) - This crate allows you test your CLIs in a very intuitive and functional way!
2017-11-03 03:15:02 +00:00
2020-03-02 08:52:15 +00:00
[docs]: https://docs.rs/clap
[examples]: examples