clap - Rust (original) (raw)

Expand description

Command Line Argument Parser for Rust

Quick Links:

§Aspirations

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

§Example

Run

$ cargo add clap --features derive

(See also feature flag reference)

Then define your CLI in main.rs:

use clap::Parser;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    /// Name of the person to greet
    #[arg(short, long)]
    name: String,

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

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

    for _ in 0..args.count {
        println!("Hello {}!", args.name);
    }
}

And try it out:

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

Usage: demo[EXE] [OPTIONS] --name <NAME>

Options:
  -n, --name <NAME>    Name of the person to greet
  -c, --count <COUNT>  Number of times to greet [default: 1]
  -h, --help           Print help
  -V, --version        Print version

$ demo --name Me
Hello Me!

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

See also the derive tutorial and reference

Augment clap:

CLI Helpers

Testing

Documentation:

_cookbookunstable-doc

Documentation: Cookbook

_deriveunstable-doc

Documentation: Derive Reference

_faqunstable-doc

Documentation: FAQ

_featuresunstable-doc

Documentation: Feature Flags

_tutorialunstable-doc

Tutorial for the Builder API

builder

Define Command line arguments

error

Error reporting

parser

Command line argument parser

arg

Create an Arg from a usage string.

command

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

crate_authors

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

crate_description

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

crate_name

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

crate_version

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

value_parser

Select a ValueParser implementation from the intended type

Arg

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

ArgGroup

Family of related arguments.

ArgMatches

Container for parse results.

Command

Build a command-line interface.

Id

Arg or ArgGroup identifier

ArgAction

Behavior of arguments when they are encountered while parsing

ColorChoice

Represents the color preferences for program output

ValueHint

Provide shell with hint on how to complete an argument.

Args

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

CommandFactory

Create a Command relevant for a user-defined container.

FromArgMatches

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

Parser

Parse command-line arguments into Self.

Subcommand

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

ValueEnum

Parse arguments into enums.

Error

Command Line Argument Parser Error