Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use default value when option is passed #2172

Merged
merged 1 commit into from
Oct 14, 2020
Merged

Don't use default value when option is passed #2172

merged 1 commit into from
Oct 14, 2020

Conversation

davidhewitt
Copy link
Contributor

This PR changes default_value so that it does not apply for an option when the user passes the flag without a corresponding value. This case is now handled by default_missing_value.

(See also my discussion question #2083 where I understood that this is the expected behaviour.)

Consider this sample program:

#[derive(Clap, Debug)]
struct Opts {
    #[clap(long, default_value = "5")]
    batch_size: usize
}

With the current master, I get the following results of execution:

cargo run                    // OK
batch_size = 5

cargo run -- --batch-size    // <---This should not succeed, as default_missing_value is not set
batch_size = 5

cargo run -- --batch-size 6  // OK
batch_size = 6

After this patch, everything works as expected:

cargo run
batch_size = 5

cargo run -- --batch-size
error: The argument '--batch-size <batch-size>' requires a value but none was supplied

cargo run -- --batch-size 6
batch_size = 6

@pksunkara pksunkara linked an issue Oct 14, 2020 that may be closed by this pull request
@pksunkara pksunkara self-requested a review October 14, 2020 05:47
clap_derive/tests/default_value.rs Outdated Show resolved Hide resolved
clap_derive/tests/default_value.rs Outdated Show resolved Hide resolved
tests/default_missing_vals.rs Outdated Show resolved Hide resolved
tests/opts.rs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick review, I will address all comments tonight!

clap_derive/tests/default_value.rs Outdated Show resolved Hide resolved
clap_derive/tests/default_value.rs Outdated Show resolved Hide resolved
tests/default_missing_vals.rs Outdated Show resolved Hide resolved
tests/opts.rs Outdated Show resolved Hide resolved
@davidhewitt
Copy link
Contributor Author

Thanks very much for the feedback - I've addressed it all and CI appears happy. Let me know if there's any other changes you'd like to see!

Copy link
Member

@pksunkara pksunkara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bors r+

@bors
Copy link
Contributor

bors bot commented Oct 14, 2020

Build succeeded:

@bors bors bot merged commit d2dd54b into clap-rs:master Oct 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

arg.take_value conflicts with arg.default_value
2 participants