|
| 1 | +use clap::{builder::PossibleValue, Arg, ArgAction, Command}; |
| 2 | + |
1 | 3 | fn main() {
|
2 | 4 | #[allow(unused_mut)]
|
3 |
| - let mut cmd = clap::Command::new("stdio-fixture") |
| 5 | + let mut cmd = Command::new("stdio-fixture") |
4 | 6 | .version("1.0")
|
5 | 7 | .long_version("1.0 - a2132c")
|
6 | 8 | .arg_required_else_help(true)
|
7 |
| - .subcommand(clap::Command::new("more")) |
| 9 | + .subcommand(Command::new("more")) |
| 10 | + .subcommand( |
| 11 | + Command::new("test") |
| 12 | + .visible_alias("do-stuff") |
| 13 | + .long_about("Subcommand with one visible alias"), |
| 14 | + ) |
| 15 | + .subcommand( |
| 16 | + Command::new("test_2") |
| 17 | + .visible_aliases(["do-other-stuff", "tests"]) |
| 18 | + .about("several visible aliases") |
| 19 | + .long_about("Subcommand with multiple visible aliases"), |
| 20 | + ) |
| 21 | + .subcommand( |
| 22 | + Command::new("test_3") |
| 23 | + .long_flag("test") |
| 24 | + .about("several visible long flag aliases") |
| 25 | + .visible_long_flag_aliases(["testing", "testall", "test_all"]), |
| 26 | + ) |
| 27 | + .subcommand( |
| 28 | + Command::new("test_4") |
| 29 | + .short_flag('t') |
| 30 | + .about("several visible short flag aliases") |
| 31 | + .visible_short_flag_aliases(['q', 'w']), |
| 32 | + ) |
| 33 | + .subcommand( |
| 34 | + Command::new("test_5") |
| 35 | + .short_flag('e') |
| 36 | + .long_flag("test-hdr") |
| 37 | + .about("all kinds of visible aliases") |
| 38 | + .visible_aliases(["tests_4k"]) |
| 39 | + .visible_long_flag_aliases(["thetests", "t4k"]) |
| 40 | + .visible_short_flag_aliases(['r', 'y']), |
| 41 | + ) |
8 | 42 | .arg(
|
9 |
| - clap::Arg::new("verbose") |
| 43 | + Arg::new("verbose") |
10 | 44 | .long("verbose")
|
11 | 45 | .help("log")
|
12 |
| - .action(clap::ArgAction::SetTrue) |
| 46 | + .action(ArgAction::SetTrue) |
13 | 47 | .long_help("more log"),
|
| 48 | + ) |
| 49 | + .arg( |
| 50 | + Arg::new("config") |
| 51 | + .action(ArgAction::Set) |
| 52 | + .help("Speed configuration") |
| 53 | + .short('c') |
| 54 | + .long("config") |
| 55 | + .value_name("MODE") |
| 56 | + .value_parser([ |
| 57 | + PossibleValue::new("fast"), |
| 58 | + PossibleValue::new("slow").help("slower than fast"), |
| 59 | + PossibleValue::new("secret speed").hide(true), |
| 60 | + ]) |
| 61 | + .default_value("fast"), |
| 62 | + ) |
| 63 | + .arg( |
| 64 | + Arg::new("name") |
| 65 | + .action(ArgAction::Set) |
| 66 | + .help("App name") |
| 67 | + .long_help("Set the instance app name") |
| 68 | + .value_name("NAME") |
| 69 | + .visible_alias("app-name") |
| 70 | + .default_value("clap"), |
| 71 | + ) |
| 72 | + .arg( |
| 73 | + Arg::new("fruits") |
| 74 | + .short('f') |
| 75 | + .visible_short_alias('b') |
| 76 | + .action(ArgAction::Append) |
| 77 | + .value_name("FRUITS") |
| 78 | + .help("List of fruits") |
| 79 | + .default_values(["apple", "banane", "orange"]), |
14 | 80 | );
|
| 81 | + #[cfg(feature = "env")] |
| 82 | + { |
| 83 | + cmd = cmd.arg( |
| 84 | + Arg::new("env_arg") |
| 85 | + .help("Read from env var when arg is not present.") |
| 86 | + .value_name("ENV") |
| 87 | + .env("ENV_ARG"), |
| 88 | + ) |
| 89 | + } |
15 | 90 | #[cfg(feature = "color")]
|
16 | 91 | {
|
17 |
| - use clap::builder::styling; |
18 |
| - const STYLES: styling::Styles = styling::Styles::styled() |
19 |
| - .header(styling::AnsiColor::Green.on_default().bold()) |
20 |
| - .usage(styling::AnsiColor::Green.on_default().bold()) |
21 |
| - .literal(styling::AnsiColor::Blue.on_default().bold()) |
22 |
| - .placeholder(styling::AnsiColor::Cyan.on_default()); |
| 92 | + use clap::builder::styling::{AnsiColor, Styles}; |
| 93 | + const STYLES: Styles = Styles::styled() |
| 94 | + .header(AnsiColor::Green.on_default().bold()) |
| 95 | + .error(AnsiColor::Red.on_default().bold()) |
| 96 | + .usage(AnsiColor::Green.on_default().bold().underline()) |
| 97 | + .literal(AnsiColor::Blue.on_default().bold()) |
| 98 | + .placeholder(AnsiColor::Cyan.on_default()) |
| 99 | + .valid(AnsiColor::Green.on_default()) |
| 100 | + .invalid(AnsiColor::Magenta.on_default().bold()) |
| 101 | + .context(AnsiColor::Yellow.on_default().dimmed()) |
| 102 | + .context_value(AnsiColor::Yellow.on_default().italic()); |
23 | 103 | cmd = cmd.styles(STYLES);
|
24 | 104 | }
|
25 | 105 | cmd.get_matches();
|
|
0 commit comments