Skip to content

Commit

Permalink
docs(cookbook): Allow repeated operators
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 8, 2024
1 parent 148e102 commit d63106b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 16 deletions.
50 changes: 39 additions & 11 deletions examples/find.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ Options:
-V, --version Print version

TESTS:
--empty File is empty and is either a regular file or a directory
--name <NAME> Base of file name (the path with the leading directories removed) matches shell
pattern pattern
--empty [<empty>] File is empty and is either a regular file or a directory [default: false]
[possible values: true, false]
--name <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 [<or>] expr2 is not evaluate if exp1 is true [default: false] [possible values: true,
false]
-a, --and [<and>] Same as `expr1 expr1` [default: false] [possible values: true, false]

$ find --empty -o --name .keep
[
Expand All @@ -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",
),
),
]

```

40 changes: 35 additions & 5 deletions examples/find.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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 <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"),
])
}

Expand Down

0 comments on commit d63106b

Please sign in to comment.