No description
Find a file
2021-12-06 11:04:41 -06:00
.github chore(gh): Update labels 2021-12-06 10:24:03 -06:00
benches fix: Be consistent on hide/hidden 2021-11-29 10:58:00 -06:00
clap_derive chore: Remove stale boiletplate 2021-12-06 11:04:41 -06:00
clap_generate chore: Remove stale boiletplate 2021-12-06 11:04:41 -06:00
clap_generate_fig chore: Remove stale boiletplate 2021-12-06 11:04:41 -06:00
docs docs: Focus top-level README 2021-11-30 09:53:25 -06:00
examples docs: Add Derive API reference 2021-12-01 21:23:06 -06:00
src chore: Remove stale boiletplate 2021-12-06 11:04:41 -06:00
tests docs: Update changelog 2021-12-02 20:26:21 -06:00
.gitignore chore: Align on boilerplate 2021-11-17 15:24:50 -06:00
.pre-commit-config.yaml chore: Align on boilerplate 2021-11-17 15:24:50 -06:00
Cargo.toml docs: Re-work examples 2021-11-30 21:33:52 -06:00
CHANGELOG.md docs: Update changelog 2021-12-04 12:07:37 -06:00
clap.schema.json feat(doc): Fix many typos in docs, comments and codes found by typos-cli 2021-10-19 10:38:22 +09:00
committed.toml chore: Align on boilerplate 2021-11-17 15:24:50 -06:00
CONTRIBUTING.md docs: Remove references to just (removed by #2923) 2021-11-14 21:46:31 -05:00
LICENSE-APACHE chore: relicense under MIT and APACHE 2.0 2018-07-23 14:25:18 -04:00
LICENSE-MIT chore: updates additional files for 2x release 2016-01-28 11:46:12 -05:00
Makefile docs: Fix stale examples 2021-11-30 12:05:30 -06:00
README.md docs: Add Derive API reference 2021-12-01 21:23:06 -06:00
typos.toml docs: Fix typos 2021-11-17 15:21:58 -06:00

clap

Command Line Argument Parser for Rust

Crates.io Crates.io License License Build Status Coverage Status Contributors

Dual-licensed under Apache 2.0 or MIT.

  1. About
  2. Tutorial: Builder API, Derive API
  3. Examples
  4. API Reference
  5. CHANGELOG
  6. FAQ
  7. Questions & Discussions
  8. Contributing

About

Create your command-line parser, with all of the bells and whistles, declaratively or procedurally.

Example

use clap::Parser;

#[derive(Parser)]
#[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
    #[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
    input: String,
    /// A level of verbosity, and can be used multiple times
    #[clap(short, long, parse(from_occurrences))]
    verbose: i32,
}

fn main() {
    let args = Cli::parse();

    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'
    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...
}
$ demo --help
clap [..]

Kevin K. <kbknapp@gmail.com>, Clap Maintainers

A simple to use, efficient, and full-featured Command Line Argument Parser

USAGE:
    demo[EXE] [OPTIONS] <INPUT>

ARGS:
    <INPUT>    Some input. Because this isn't an Option<T> it's required to be used

OPTIONS:
    -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
    -v, --verbose          A level of verbosity, and can be used multiple times
    -V, --version          Print version information

(version number and .exe extension on windows replaced by placeholders)

Aspirations

  • Out of the box, users get a polished CLI experience
    • Including common argument behavior, help generation, suggested fixes for users, colored output, shell completions, 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

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 for CLI parsers optimized for other use cases.

Feature Flags

Default Features

  • std: Not Currently Used. Placeholder for supporting no_std environments in a backwards compatible manner.
  • color: Turns on colored error messages.
  • suggestions: Turns on the Did you mean '--myoption'? feature for when users make typos.

Optional features

  • 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.
  • 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

Warning: These may contain breaking changes between minor releases.