mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
d840d5650e
Before #2005, `Clap` was a special trait that derived all clap traits it detected were relevant (including an enum getting both `ArgEnum`, `Clap`, and `Subcommand`). Now, we have elevated `Clap`, `Args`, `Subcommand`, and `ArgEnum` to be user facing but the name `Clap` isn't very descriptive. This also helps further clarify the relationships so a crate providing an item to be `#[clap(flatten)]` or `#[clap(subcommand)]` is more likely to choose the needed trait to derive. Also, my proposed fix fo #2785 includes making `App` attributes almost exclusively for `Clap`. Clarifying the names/roles will help communicate this. For prior discussion, see #2583
60 lines
1.2 KiB
Markdown
60 lines
1.2 KiB
Markdown
+++
|
|
title = "Fast & Modern CLI Framework for Rust"
|
|
+++
|
|
|
|
**Clap** is a simple-to-use, efficient, and full-featured library for parsing command line arguments and subcommands when writing console/terminal applications.
|
|
|
|
Here is an example of a simple program:
|
|
|
|
```rust
|
|
use clap::Parser;
|
|
|
|
/// Simple program to greet a person
|
|
#[derive(Parser, Debug)]
|
|
#[clap(name = "hello")]
|
|
struct Hello {
|
|
/// Name of the person to greet
|
|
#[clap(short, long)]
|
|
name: String,
|
|
|
|
/// Number of times to greet
|
|
#[clap(short, long, default_value = "1")]
|
|
count: u8,
|
|
}
|
|
|
|
fn main() {
|
|
let hello = Hello::parse();
|
|
|
|
for _ in 0..hello.count {
|
|
println!("Hello {}!", hello.name)
|
|
}
|
|
}
|
|
```
|
|
|
|
The above example program can be run as shown below:
|
|
|
|
```
|
|
$ hello --name John --count 3
|
|
Hello John!
|
|
Hello John!
|
|
Hello John!
|
|
```
|
|
|
|
The program also has automatically generated help message:
|
|
|
|
```
|
|
hello
|
|
|
|
Simple program to greet a person
|
|
|
|
USAGE:
|
|
hello [OPTIONS] --name <name>
|
|
|
|
FLAGS:
|
|
-h, --help Print help information
|
|
-V, --version Print version information
|
|
|
|
OPTIONS:
|
|
-c, --count <count> Number of times to greet [default: 1]
|
|
-n, --name <name> Name of the person to greet
|
|
```
|