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

add 'default_missing_value' configuration option #1587

Merged
merged 3 commits into from
May 21, 2020

Conversation

rivy
Copy link
Contributor

@rivy rivy commented Oct 30, 2019

v3-compatible version of #1468.
Re-opened from #1507 (after upstream branch rename auto-close).

RFC/WIP ~ this PR is a quick proof-of-concept, inviting commentary before being updated and fleshed out (documentation, tests, etc).

For an OPTION taking optional values (ie, .min_values(0).require_equals(true)), currently any time that option is encountered multiple times, occurs is incremented, but no entry is made into the indices and vals arrays for options with no/missing value. "None" would seem like a good entry but the vals array contains just that, values, not Options, so that would break compatibility without any great upside. However, adding a default for missing values can be done without breaking anything else...

This PR adds support to supply a value for the "None"/missing-value case (eg, for "--OPTION") via a default_missing_value() method. Notably, the supplied value to default_missing_value() may differ from the OPTION's default_value().

clap (v3) compiles and tests without error after this patch, but there is no specific testing of the feature as of yet (WIP).

I'm happy to put some more effort towards this (adding tests, documentation, etc) if it is a desired feature addition.

@kbknapp
Copy link
Member

kbknapp commented Oct 30, 2019

As mentioned in the other comments, I'd like to see the most work put towards how we make it clear the difference between default value and default missing value, as well as thinking through all the possible edge cases. Perhaps naming it default_empty_value or such would help.

The one other area of concern is how this interacts with the conditional default values, or should it? I'm perfectly OK saying it doesn't.

@rivy
Copy link
Contributor Author

rivy commented Oct 31, 2019

toml v0.5.4 is breaking the Travis build ...
Alex doesn't believe that changing the minimum rust version is a "semantic" change?
Maybe lock toml to v0.5.3?

@rivy
Copy link
Contributor Author

rivy commented Oct 31, 2019

I can easily change the name from default_missing_value to default_empty_value.

I'm concerned a bit about the semantics...

Is --color="" (or --color=) an "empty" value? a null value?

I do think that (1) --color="" (aka --color=) and (2) --color are semantically distinct and should be treated as (1) color with an empty string value (ie, "") and (2) color with no value (which pulls in the default_missing_value in the PR). But I'm not really sure of the best terminology for differentiating the two cases.

@kbknapp
Copy link
Member

kbknapp commented Oct 31, 2019

Is --color="" (or --color=) an "empty" value? a null value?

I do think that (1) --color="" (aka --color=) and (2) --color are semantically distinct and should be treated as (1) color with an empty string value (ie, "") and (2) color with no value (which pulls in the default_missing_value in the PR).

I agree and you make a great point. The best terminology I can think of is --color= or --color="" is explicitly an empty value. Whereas --color is a null value, I like your terminology. So having said that I'd retract my default_empty_value idea, being that --color= and --color="" are actually what clap calls empty values.

@tshepang
Copy link
Contributor

I'd say --color= should mean exactly the same as --color, because a value is not given at all, and that's what @rivy seems to favor also.

@rivy
Copy link
Contributor Author

rivy commented Dec 23, 2019

@tshepang , no, this PR is actually exactly the opposite. This PR and IMO ...

  • --color="" == --color='' == --color=, and means "use the empty string ('') as the value for the color option"
  • --color is semantically different, meaning "use the (author specified) default value for the color option"; eg --color would generally be equivalent to --color=always

@tshepang
Copy link
Contributor

Hm, maybe --color= should be disallowed then (runtime error). It's not obvious to me it should be an empty string, instead of null.

@rivy
Copy link
Contributor Author

rivy commented Dec 25, 2019

@tshepang , most shells parse and return --foo= as the resultant argument for all of --foo=, --foo='', and --foo="". These are supplied, and there is usually no reasonable way to differentiate at the level of the executable. So, generally, --foo= is / has to be interpreted as foo with an empty string argument.

Notably, windows CMD is, as usual, an exception, but most option libraries for windows emulate the *nix standard.

@pksunkara
Copy link
Member

@rivy Please rebase and add some tests and docs.

@rivy
Copy link
Contributor Author

rivy commented Apr 10, 2020

Will do ... in the middle of landing a large PR to coreutils, so it'll have to be next week or so ...

@rivy rivy changed the title [RFC/WIP] fix missing optional values (v3-compatible version; feature?) add 'default_missing_value' configuration option May 20, 2020
@rivy
Copy link
Contributor Author

rivy commented May 20, 2020

@pksunkara , @kbknapp , the code is now rebased onto 'master' and updated to use the newer clap-v3 internals. I've also added a few tests. I should have some documentation comments done within the next day or so...

Please review and let me know what revisions you'd like and whatever else needs to be done as prep for merge.

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.

Looks good. Have a few minor comments. You probably need to make sure cargo fmt doesn't fail.

src/build/arg/mod.rs Outdated Show resolved Hide resolved
src/build/arg/mod.rs Show resolved Hide resolved
tests/default_missing_vals.rs Show resolved Hide resolved
tests/default_missing_vals.rs Outdated Show resolved Hide resolved
src/parse/parser.rs Outdated Show resolved Hide resolved
@pksunkara
Copy link
Member

Can you also add a test case which gets empty value for --color= even with default_missing_value specified. (You probably need to say TODO and comment out the test case since empty values don't work yet correctly)

@rivy
Copy link
Contributor Author

rivy commented May 20, 2020

@pksunkara , @kbknapp , @CreepySkeleton , docs added; some polish done based on initial feedback.

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.

Looks good.

src/build/arg/mod.rs Outdated Show resolved Hide resolved
src/parse/parser.rs Outdated Show resolved Hide resolved
src/parse/parser.rs Outdated Show resolved Hide resolved
tests/default_missing_vals.rs Outdated Show resolved Hide resolved
@rivy rivy requested a review from pksunkara May 20, 2020 21:32
@pksunkara
Copy link
Member

Tests are still failing and you haven't addressed all my comments.

@rivy rivy force-pushed the fix.v3.missing-opt-val branch 2 times, most recently from 8489bf3 to f2b590c Compare May 21, 2020 01:50
@rivy
Copy link
Contributor Author

rivy commented May 21, 2020

Doc and lint tests are now passing.
All comments addressed, if not completely resolved.

tests/default_missing_vals.rs Outdated Show resolved Hide resolved
tests/default_missing_vals.rs Outdated Show resolved Hide resolved
src/build/arg/mod.rs Outdated Show resolved Hide resolved
@rivy
Copy link
Contributor Author

rivy commented May 21, 2020

Changes made based on feedback.
I'll squash to 3 commits (add, tests, docs) after initial tests pass.

@pksunkara
Copy link
Member

Looks good. I don't have any other comments.

@rivy
Copy link
Contributor Author

rivy commented May 21, 2020

Rebased onto current master, squashed to 3 commits, missing trailing comma added.

Tests are all passing.

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 May 21, 2020

Build succeeded:

@bors bors bot merged commit 31ebd7e into clap-rs:master May 21, 2020
@rivy rivy deleted the fix.v3.missing-opt-val branch May 21, 2020 21:45
@twe4ked
Copy link

twe4ked commented May 21, 2020

Thanks everyone 🙏

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.

6 participants