Skip to content

Commit

Permalink
feat(builder): Allow flags to take num_args=0..=1
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 17, 2025
1 parent c395d02 commit 389fbe8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions clap_builder/src/builder/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ impl ArgAction {
match self {
Self::Set => ValueRange::FULL,
Self::Append => ValueRange::FULL,
Self::SetTrue => ValueRange::EMPTY,
Self::SetFalse => ValueRange::EMPTY,
Self::SetTrue => ValueRange::OPTIONAL,
Self::SetFalse => ValueRange::OPTIONAL,
Self::Count => ValueRange::EMPTY,
Self::Help => ValueRange::EMPTY,
Self::HelpShort => ValueRange::EMPTY,
Expand Down
6 changes: 6 additions & 0 deletions clap_builder/src/builder/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ impl ValueRange {
end_inclusive: 1,
};

#[cfg(debug_assertions)]
pub(crate) const OPTIONAL: Self = Self {
start_inclusive: 0,
end_inclusive: 1,
};

pub(crate) const FULL: Self = Self {
start_inclusive: 0,
end_inclusive: usize::MAX,
Expand Down
20 changes: 13 additions & 7 deletions tests/builder/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,19 +225,25 @@ fn leading_dash_stripped() {

#[test]
fn optional_value() {
let cmd = Command::new("flag").args([arg!(-f --flag "some flag").action(ArgAction::SetTrue)]);
let cmd = Command::new("flag").args([arg!(-f --flag "some flag")
.action(ArgAction::SetTrue)
.num_args(0..=1)]);

let m = cmd.clone().try_get_matches_from(vec![""]).unwrap();
assert!(!*m.get_one::<bool>("flag").expect("defaulted by clap"));

let m = cmd.clone().try_get_matches_from(vec!["", "-f"]).unwrap();
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));

cmd.clone()
.try_get_matches_from(vec!["", "-f", "true"])
.unwrap_err();

cmd.clone()
let m = cmd
.clone()
.try_get_matches_from(vec!["", "-f", "false"])
.unwrap_err();
.unwrap();
assert!(!*m.get_one::<bool>("flag").expect("defaulted by clap"));

let m = cmd
.clone()
.try_get_matches_from(vec!["", "-f", "true"])
.unwrap();
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
}
1 change: 0 additions & 1 deletion tests/derive/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ fn unit_for_negation() {
}

#[test]
#[should_panic = "Argument `alice`'s action SetTrue is incompatible with `num_args(0..=1)`"]
fn optional_value_flag() {
#[derive(Parser, PartialEq, Eq, Debug)]
struct Opt {
Expand Down

0 comments on commit 389fbe8

Please sign in to comment.