Skip to content

Commit

Permalink
docs(cookbook): Clarify intent of fake flags
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 8, 2024
1 parent 3eaf1af commit 02f8214
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions examples/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ fn cli() -> Command {
.group(ArgGroup::new("tests").multiple(true))
.next_help_heading("TESTS")
.args([
Arg::new("empty")
position_sensitive_flag(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"),
Expand All @@ -31,29 +27,30 @@ fn cli() -> Command {
.group(ArgGroup::new("operators").multiple(true))
.next_help_heading("OPERATORS")
.args([
Arg::new("or")
position_sensitive_flag(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")
position_sensitive_flag(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"),
])
}

fn position_sensitive_flag(arg: Arg) -> Arg {
// Flags don't track the position of each occurrence, so we need to emulate flags with
// value-less options to get the same result
arg.num_args(0)
.value_parser(value_parser!(bool))
.default_missing_value("true")
.default_value("false")
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum Value {
Bool(bool),
Expand Down

0 comments on commit 02f8214

Please sign in to comment.