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

321 add support for default value provider #458

Merged

Conversation

NicolasMassart
Copy link
Contributor

Adding support for a default value provider that could enable me to have those defaults loaded from a configuration file.

See issue #321

NicolasMassart and others added 3 commits August 31, 2018 00:00
Following what was done for Command version provider I added a default value
provider.
Following what was done for Command version provider I added a default value
provider as proposed in remkop#321.
@codecov-io
Copy link

codecov-io commented Aug 30, 2018

Codecov Report

Merging #458 into master will decrease coverage by 0.1%.
The diff coverage is 80%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #458      +/-   ##
============================================
- Coverage     88.63%   88.53%   -0.11%     
- Complexity      274      275       +1     
============================================
  Files             4        4              
  Lines          3767     3785      +18     
  Branches        892      897       +5     
============================================
+ Hits           3339     3351      +12     
- Misses          218      222       +4     
- Partials        210      212       +2
Impacted Files Coverage Δ Complexity Δ
src/main/java/picocli/CommandLine.java 88.43% <80%> (-0.12%) 147 <1> (+1)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0732f0f...08bf1ed. Read the comment docs.

@NicolasMassart
Copy link
Contributor Author

As said in #321, I didn't integrate unit tests for the moment, it's just a preview PR so be sure it's ok with @remkop

* Returns default value for a command, option or parameter.
* @return default value
* @throws Exception an exception detailing what went wrong when obtaining default value
*/
Copy link
Owner

Choose a reason for hiding this comment

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

#321 has some javadoc for this method that I would like to use.

* @return default value
* @throws Exception an exception detailing what went wrong when obtaining default value
*/
String defaultValue(ArgSpec argSpec) throws Exception;
Copy link
Owner

Choose a reason for hiding this comment

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

The API in #321 did not have throws Exception in the signature, but I'm okay with this. I hadn't thought about this, but I guess it is convenient for implementors if this method is declared as throws Exception. I believe it is used in methods that currently also throw Exception so there is no additional error handling required there, which is nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I admit I mimicked the way it worked for version provider

for (OptionSpec option : commandSpec.options()) { applyDefault(option, required); }
for (PositionalParamSpec positional : commandSpec.positionalParameters()) { applyDefault(positional, required); }
for (OptionSpec option : commandSpec.options()) { applyDefault(commandSpec.defaultValueProvider, option, required); }
for (PositionalParamSpec positional : commandSpec.positionalParameters()) { applyDefault(commandSpec.defaultValueProvider, positional, required); }
Copy link
Owner

Choose a reason for hiding this comment

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

Small nit, but I'd prefer to call the commandSpec.defaultValueProvider() method instead of referencing the commandSpec.defaultValueProvider field where possible.

@@ -3650,6 +3677,15 @@ public CommandSpec aliases(String... aliases) {
return this;
}

/** Returns the default value provider for this command.
* @return the default value provider or {@code null}*/
public IDefaultValueProvider defaultValueProvider() { return defaultValueProvider; }
Copy link
Owner

Choose a reason for hiding this comment

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

Please add @since 3.6 to the javadoc

/** Sets default value provider for this command.
* @param defaultValueProvider the default value provider to use, or {@code null}.
* @return this CommandSpec for method chaining */
public CommandSpec defaultValueProvider(IDefaultValueProvider defaultValueProvider) { this.defaultValueProvider = defaultValueProvider; return this; }
Copy link
Owner

Choose a reason for hiding this comment

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

Here also, please @since 3.6

* @param defaultValueProvider the default value provider to use, or {@code null}.
* @return this CommandSpec for method chaining */
public CommandSpec defaultValueProvider(IDefaultValueProvider defaultValueProvider) { this.defaultValueProvider = defaultValueProvider; return this; }

Copy link
Owner

Choose a reason for hiding this comment

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

I am wondering whether it would be a good idea to also add programmatic API for setting/getting the IDefaultProvider on the CommandLine facade class.

If we think this is a good idea, it should work like the other CommandLine.getX and setX methods: apply the new X value to all subcommands that are registered at the time of the call. (See for example setStopAtPositional, setToggleBooleanFlags etc).

According to PR remkop#458 review:
 - fixed javadoc and since version
 - added a setter in Command facade for default provider
 - replaced use of defaultValueProvider field by its getter
@remkop
Copy link
Owner

remkop commented Sep 1, 2018

PR looks good and tests look good. I may add some more tests but this covers a lot.
Thank you very much!

Can I ask you to rebase on master? That will give a cleaner git history when I merge the branch.

@NicolasMassart
Copy link
Contributor Author

@remkop remkop merged commit c6049f9 into remkop:master Sep 2, 2018
@remkop
Copy link
Owner

remkop commented Sep 2, 2018

Merged. Thank you very much!

@remkop remkop added this to the 3.6 milestone Sep 2, 2018
NicolasMassart added a commit to NicolasMassart/picocli that referenced this pull request Sep 4, 2018
Fixed misbehaviour that was introduced in PR remkop#458, see remkop#321
Now default provider overrides default or initial values only if the return
value of the provider is not null.
remkop pushed a commit that referenced this pull request Sep 4, 2018
Fixed misbehaviour that was introduced in PR #458, see #321
Now default provider overrides default or initial values only if the return
value of the provider is not null.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants