From d63106b9f698df0d2241662ab1459c823890dec4 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 8 Jan 2024 09:50:40 -0600 Subject: [PATCH] docs(cookbook): Allow repeated operators --- examples/find.md | 50 +++++++++++++++++++++++++++++++++++++----------- examples/find.rs | 40 +++++++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/examples/find.md b/examples/find.md index b2da1607ef9..75bc3a4f13b 100644 --- a/examples/find.md +++ b/examples/find.md @@ -11,13 +11,15 @@ Options: -V, --version Print version TESTS: - --empty File is empty and is either a regular file or a directory - --name Base of file name (the path with the leading directories removed) matches shell - pattern pattern + --empty [] File is empty and is either a regular file or a directory [default: false] + [possible values: true, false] + --name Base of file name (the path with the leading directories removed) matches + shell pattern pattern OPERATORS: - -o, --or expr2 is not evaluate if exp1 is true - -a, --and Same as `expr1 expr1` + -o, --or [] expr2 is not evaluate if exp1 is true [default: false] [possible values: true, + false] + -a, --and [] Same as `expr1 expr1` [default: false] [possible values: true, false] $ find --empty -o --name .keep [ @@ -42,12 +44,38 @@ $ find --empty -o --name .keep ] $ find --empty -o --name .keep -o --name foo -? 2 -error: the argument '--or' cannot be used multiple times - -Usage: find [OPTIONS] - -For more information, try '--help'. +[ + ( + "empty", + Bool( + true, + ), + ), + ( + "or", + Bool( + true, + ), + ), + ( + "name", + String( + ".keep", + ), + ), + ( + "or", + Bool( + true, + ), + ), + ( + "name", + String( + "foo", + ), + ), +] ``` diff --git a/examples/find.rs b/examples/find.rs index 0b7f2c70000..8a0b55d528d 100644 --- a/examples/find.rs +++ b/examples/find.rs @@ -1,6 +1,6 @@ use std::collections::BTreeMap; -use clap::{arg, command, ArgGroup, ArgMatches, Command}; +use clap::{command, value_parser, Arg, ArgAction, ArgGroup, ArgMatches, Command}; fn main() { let matches = cli().get_matches(); @@ -13,14 +13,44 @@ fn cli() -> Command { .group(ArgGroup::new("tests").multiple(true)) .next_help_heading("TESTS") .args([ - arg!(--empty "File is empty and is either a regular file or a directory").group("tests"), - arg!(--name "Base of file name (the path with the leading directories removed) matches shell pattern pattern").group("tests"), + Arg::new("empty") + .long("empty") + .num_args(0) + .value_parser(value_parser!(bool)) + .default_missing_value("true") + .default_value("false") + .action(ArgAction::Append) + .help("File is empty and is either a regular file or a directory") + .group("tests"), + Arg::new("name") + .long("name") + .action(ArgAction::Append) + .help("Base of file name (the path with the leading directories removed) matches shell pattern pattern") + .group("tests") ]) .group(ArgGroup::new("operators").multiple(true)) .next_help_heading("OPERATORS") .args([ - arg!(-o - -or "expr2 is not evaluate if exp1 is true").group("operators"), - arg!(-a - -and "Same as `expr1 expr1`").group("operators"), + Arg::new("or") + .short('o') + .long("or") + .num_args(0) + .value_parser(value_parser!(bool)) + .default_missing_value("true") + .default_value("false") + .action(ArgAction::Append) + .help("expr2 is not evaluate if exp1 is true") + .group("operators"), + Arg::new("and") + .short('a') + .long("and") + .num_args(0) + .value_parser(value_parser!(bool)) + .default_missing_value("true") + .default_value("false") + .action(ArgAction::Append) + .help("Same as `expr1 expr1`") + .group("operators"), ]) }