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
48 lines
1.3 KiB
Rust
48 lines
1.3 KiB
Rust
//! A somewhat comprehensive example of a typical `clap_derive` usage.
|
|
|
|
use clap::{Parser, ValueHint};
|
|
use std::path::PathBuf;
|
|
|
|
/// A basic example
|
|
#[derive(Parser, Debug)]
|
|
#[clap(name = "basic")]
|
|
struct Opt {
|
|
// A flag, true if used in the command line. Note doc comment will
|
|
// be used for the help message of the flag. The name of the
|
|
// argument will be, by default, based on the name of the field.
|
|
/// Activate debug mode
|
|
#[clap(short, long)]
|
|
debug: bool,
|
|
|
|
// The number of occurrences of the `v/verbose` flag
|
|
/// Verbose mode (-v, -vv, -vvv, etc.)
|
|
#[clap(short, long, parse(from_occurrences))]
|
|
verbose: u8,
|
|
|
|
/// Set speed
|
|
#[clap(short, long, default_value = "42")]
|
|
speed: f64,
|
|
|
|
/// Output file
|
|
#[clap(short, long, parse(from_os_str), value_hint = ValueHint::FilePath)]
|
|
output: PathBuf,
|
|
|
|
// the long option will be translated by default to kebab case,
|
|
// i.e. `--nb-cars`.
|
|
/// Number of cars
|
|
#[clap(short = 'c', long)]
|
|
nb_cars: Option<i32>,
|
|
|
|
/// admin_level to consider
|
|
#[clap(short, long)]
|
|
level: Vec<String>,
|
|
|
|
/// Files to process
|
|
#[clap(name = "FILE", parse(from_os_str), value_hint = ValueHint::AnyPath)]
|
|
files: Vec<PathBuf>,
|
|
}
|
|
|
|
fn main() {
|
|
let opt = Opt::parse();
|
|
println!("{:#?}", opt);
|
|
}
|