Expand description
Command Line Argument Parser for Rust
Quick Links:
- Derive [tutorial][_derive::_tutorial] and [reference][_derive]
- Builder [tutorial][_tutorial] and reference
- [Cookbook][_cookbook]
- [FAQ][_faq]
- Discussions
§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.
§Example
Run
$ cargo add clap --features derive
(See also [feature flag reference][_features])
Then define your CLI in main.rs
:
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)
}
}
And try it out:
$ 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
$ demo --name Me
Hello Me!
(version number and .exe
extension on windows replaced by placeholders)
See also the derive [tutorial][_derive::_tutorial] and [reference][_derive]
§Related Projects
Augment clap:
- wild for supporting wildcards (
*
) on Windows like you do Linux - argfile for loading additional arguments from a file (aka response files)
- shadow-rs for generating
Command::long_version
- clap_mangen for generating man page source (roff)
- clap_complete for shell completion support
CLI Helpers
Testing
trycmd
: Bulk snapshot testingsnapbox
: Specialized snapshot testingassert_cmd
andassert_fs
: Customized testing
Documentation:
Re-exports§
pub use crate::builder::ArgAction;
pub use crate::builder::Command;
pub use crate::builder::Arg;
pub use crate::builder::ArgGroup;
pub use crate::error::Error;
pub use crate::parser::ArgMatches;
pub use crate::builder::App;
pub use crate::builder::AppFlags;
pub use crate::builder::AppSettings;
pub use crate::builder::ArgFlags;
pub use crate::builder::ArgSettings;
pub use crate::builder::PossibleValue;
pub use crate::builder::ValueHint;
pub use crate::error::ErrorKind;
pub use crate::error::Result;
pub use crate::parser::Indices;
pub use crate::parser::OsValues;
pub use crate::parser::ValueSource;
pub use crate::parser::Values;
pub use CommandFactory as IntoApp;
pub use ValueEnum as ArgEnum;
Modules§
- Error reporting
Command
line argument parser
Macros§
- Create an
Arg
from a usage string. - Requires
cargo
feature flag to be enabled. - Select a
ValueParser
implementation from the intended type
Enums§
- Represents the color preferences for program output
Traits§
- Parse a set of arguments into a user-defined container.
- Create a
Command
relevant for a user-defined container. - Converts an instance of
ArgMatches
to a user-defined container. - Parse command-line arguments into
Self
. - Parse a sub-command into a user-defined enum.
- Parse arguments into enums.
Derive Macros§
- Deprecated, replaced with
ValueEnum
Generates theValueEnum
impl.