Dual-licensed under Apache 2.0 or MIT.
Create your command-line parser, with all of the bells and whistles, declaratively or procedurally.
This uses our Derive API which provides access to the Builder API as attributes on a struct
:
use clap::Parser; /// Simple program to greet a person #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] struct Args { /// Name of the person to greet #[clap(short, long)] name: String, /// Number of times to greet #[clap(short, long, default_value_t = 1)] count: u8, } fn main() { let args = Args::parse(); for _ in 0..args.count { println!("Hello {}!", args.name) } }
Add this to Cargo.toml
:
[dependencies] clap = { version = "3.1.6", features = ["derive"] }
$ demo --help clap [..] Simple program to greet a person USAGE: demo[EXE] [OPTIONS] --name <NAME> OPTIONS: -c, --count <COUNT> Number of times to greet [default: 1] -h, --help Print help information -n, --name <NAME> Name of the person to greet -V, --version Print version information
(version number and .exe
extension on windows replaced by placeholders)
While these aspirations can be at odds with fast build times and low binary size, we will still strive to keep these reasonable for the flexibility you get. Check out the argparse-benchmarks for CLI parsers optimized for other use cases.
Why use the declarative Derive API:
Why use the procedural Builder API:
*
) on Windows like you do LinuxCommand::long_version
trycmd
: Snapshot testingassert_cmd
and assert_fs
no_std
environments in a backwards compatible manner.Did you mean '--myoption'?
feature for when users make typos.#[derive(Parser)]
). Without this you must use one of the other methods of creating a clap
CLI listed above.CARGO_*
environment variables.Warning: These may contain breaking changes between minor releases.
Command::replace
Command::multicall
ArgMatches::grouped_values_of