No description
Find a file
Daniel Saxton c4612e4c17
docs: Fix typo in derive API README (#3896)
Co-authored-by: Daniel Saxton <dsaxton@pm.me>
2022-07-05 09:49:55 -05:00
.github chore(deps): bump pre-commit/action from 2.0.3 to 3.0.0 2022-07-01 03:05:59 +00:00
assets docs: Add back in logo 2021-12-07 17:45:57 -06:00
clap_bench chore: Bump MSRV from 1.56.0 to 1.56.1 2022-06-17 09:01:45 -05:00
clap_complete chore: Release 2022-06-28 08:04:18 -05:00
clap_complete_fig chore: Release 2022-07-01 19:48:03 -05:00
clap_derive chore: Release 2022-06-28 08:04:18 -05:00
clap_lex chore: Release 2022-06-28 08:04:18 -05:00
clap_mangen chore: Release 2022-06-28 08:04:18 -05:00
docs docs: Update App references to Command 2022-02-14 15:33:49 -06:00
examples docs: Fix typo in derive API README (#3896) 2022-07-05 09:49:55 -05:00
src doc: Explain why examples are missing 2022-06-30 08:29:16 -05:00
tests feat: Add mut_subcommand method to Command 2022-06-29 20:42:58 -07:00
.clippy.toml chore: Bump MSRV from 1.56.0 to 1.56.1 2022-06-17 09:01:45 -05:00
.gitignore chore: Align on boilerplate 2021-11-17 15:24:50 -06:00
.pre-commit-config.yaml chore: Upgrade boilerplate 2022-02-15 12:36:19 -06:00
Cargo.toml chore: Release 2022-06-30 08:25:54 -05:00
CHANGELOG.md chore: Release 2022-06-30 08:25:54 -05: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 fix: Allow people to opt-in to deprecations 2022-06-14 10:50:05 -05:00
LICENSE-APACHE chore: relicense under MIT and APACHE 2.0 2018-07-23 14:25:18 -04:00
LICENSE-MIT docs(license): Update years/holders 2022-06-27 12:55:09 -05:00
Makefile chore: Bump MSRV from 1.56.0 to 1.56.1 2022-06-17 09:01:45 -05:00
README.md docs: Reorder dependency addition and example 2022-07-03 09:49:52 -04:00
release.toml chore: Configure release process 2021-12-07 21:36:00 -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
  9. Sponsors

About

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

Example

Add clap as a dependency with the derive feature enabled:

cargo add clap -F derive

This allows using the 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, value_parser)]
    name: String,

    /// Number of times to greet
    #[clap(short, long, value_parser, default_value_t = 1)]
    count: u8,
}

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

    for _ in 0..args.count {
        println!("Hello {}!", args.name)
    }
}
$ 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)

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
  • Resilient maintainership, including
    • Willing to break compatibility rather than batching up breaking changes in large releases
    • Leverage feature flags to keep to one active branch
    • Being under WG-CLI to increase the bus factor
  • We follow semver and will wait about 6-9 months between major breaking changes
  • We will support the last two minor Rust releases (MSRV, currently 1.56.1)

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.

Selecting an API

Why use the declarative Derive API:

  • Easier to read, write, and modify
  • Easier to keep the argument declaration and reading of argument in sync
  • Easier to reuse, e.g. clap-verbosity-flag

Why use the procedural Builder API:

  • Faster compile times if you aren't already using other procedural macros
  • More flexible, e.g. you can look up how many times an argument showed up, what its values were, and what were the indexes of those values. The Derive API can only report presence, number of occurrences, or values but no indices or combinations of data.

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

  • deprecated: Guided experience to prepare for next breaking release (at different stages of development, this may become default)
  • 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.

Sponsors

Gold

Silver

Bronze

Backer