logo

Crate clap[][src]

Expand description

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

use clap::Parser;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[clap(about, version, author)]
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.0.2", features = ["derive"] }
$ demo --help
clap [..]

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

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.54.0)

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.

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.

Sponsors

Gold

Silver

Bronze

Backer

https://github.com/clap-rs/clap

Macros

Allows you to build the App instance from your Cargo.toml at compile time.

Create an Arg from a usage string.

arg_enumDeprecated

Deprecated, replaced with ArgEnum

clap_appDeprecated

Deprecated, replaced with clap::Parser and clap::arg! (Issue clap-rs/clap#2835)

Allows you to pull the authors for the app from your Cargo.toml at compile time in the form: "author1 lastname <author1@example.com>:author2 lastname <author2@example.com>"

Allows you to pull the description from your Cargo.toml at compile time.

Allows you to pull the name from your Cargo.toml at compile time.

Allows you to pull the version from your Cargo.toml at compile time as MAJOR.MINOR.PATCH_PKGVERSION_PRE

load_yamlDeprecatedyaml

Deprecated in Issue #3087, maybe clap::Parser would fit your use case?

value_tDeprecated

Deprecated, replaced with ArgMatches::value_of_t

value_t_or_exitDeprecated

Deprecated, replaced with ArgMatches::value_of_t_or_exit

values_tDeprecated

Deprecated, replaced with ArgMatches::values_of_t

Deprecated, replaced with ArgMatches::values_of_t_or_exit

Structs

Build a command-line interface.

The abstract representation of a command line argument. Used to set all the options and relationships that define a valid argument for the program.

Family of related arguments.

Container for parse results.

Command Line Argument Parser Error

Iterate over indices for where an argument appeared when parsing, via ArgMatches::indices_of

Iterate over multiple values for an argument via [ArgMatches::values_of_os].

A possible value of an argument.

SubCommandDeprecated

Deprecated, replaced with App::new, unless you were looking for Subcommand

Iterate over multiple values for an argument via ArgMatches::values_of.

Enums

Application level settings, which affect how App operates

Various settings that apply to arguments and may be set, unset, and checked via getter/setter methods Arg::setting, Arg::unset_setting, and Arg::is_set. This is what the Arg methods which accept a bool use internally.

Represents the color preferences for program output

Command line argument parser kind of error

Contains either a regular expression or a set of them or a reference to one.

Provide shell with hint on how to complete an argument.

Traits

Parse arguments into enums.

Parse a set of arguments into a user-defined container.

Converts an instance of ArgMatches to a user-defined container.

Create an App relevant for a user-defined container.

Parse command-line arguments into Self.

Parse command-line arguments into Self.

Parse a sub-command into a user-defined enum.

Type Definitions

Short hand for Result type

Derive Macros

Deprecated, replaced with Parser