Skip to content

Commit

Permalink
fix!: Only apply num_args to user values
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 3, 2022
1 parent 6dddf11 commit ed3aa30
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Replace `Arg::number_of_values` (average across occurrences) with `Arg::num_args` (per occurrence, raw CLI args, not parsed values)
- `num_args(0)` no longer implies `takes_value(true).multiple_values(true)`
- `num_args(1)` no longer implies `multiple_values(true)`
- Does not check default values, only what the user explicitly passes in
- Sometimes `Arg::default_missing_value` didn't require `num_args(0..=1)`, now it does
- Replace `Arg::min_values` (across all occurrences) with `Arg::num_args(N..)` (per occurrence)
- Replace `Arg::max_values` (across all occurrences) with `Arg::num_args(1..=M)` (per occurrence)
- Replace `Arg::multiple_values(true)` with `Arg::num_args(1..)` and `Arg::multiple_values(false)` with `Arg::num_args(0)`
Expand Down
6 changes: 4 additions & 2 deletions src/parser/parser.rs
Expand Up @@ -1127,6 +1127,10 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
ident,
source
);

// `default_missing_values` does not count (ie to avoid problems with flags)
self.verify_num_args(arg, &raw_vals)?;

if raw_vals.is_empty() {
// We assume this case is valid: require equals, but min_vals == 0.
if !arg.default_missing_vals.is_empty() {
Expand All @@ -1148,8 +1152,6 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
};
}

self.verify_num_args(arg, &raw_vals)?;

match arg.get_action() {
ArgAction::Set => {
if source == ValueSource::CommandLine
Expand Down
1 change: 1 addition & 0 deletions tests/builder/default_missing_vals.rs
Expand Up @@ -171,6 +171,7 @@ fn default_missing_value_flag_value() {
Arg::new("flag")
.long("flag")
.action(ArgAction::Set)
.num_args(0..=1)
.default_value("false")
.default_missing_value("true"),
);
Expand Down
1 change: 1 addition & 0 deletions tests/builder/default_vals.rs
Expand Up @@ -874,6 +874,7 @@ fn missing_with_value_delimiter() {
Arg::new("option")
.long("option")
.value_delimiter(';')
.num_args(0..=1)
.default_missing_values(&["value1;value2;value3", "value4;value5"]),
);

Expand Down

0 comments on commit ed3aa30

Please sign in to comment.