-
Notifications
You must be signed in to change notification settings - Fork 418
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
Go spf13/viper like way of configuring commandline behaviour #1566
Comments
Hi @bpasson, yes, absolutely! 😄 Picocli already has built-in support for a lot of viper-like functionality and has hooks for building your own. |
@remkop I'm looking for resolving options following the order as below from most important to least important
The properties default provider doesn't cover this or am I wrong? |
I see, for that use case also, I don't think you would need to do much work to get that behaviour. You can define an option as follows: @Option(names = "--option", defaultValue = "${mydefault:-123}"
int optionValue; That way, the value will be searched in the following places:
Does that answer your question? |
Almost. What I am looking for is probably more in line of: @CommandLine.Command(valueProvider = CustomValueProvider.class,
resolveOrder={CommandLine.Resolve.FLAG,CommandLine.Resolve.SYSTEM_PROPERTY,CommandLine.Resolve.ENV,
CommandLine.Resolve.VALUE_PROVIDER,CommandLine.Resolve.DEFAULT} )
public class SampleCommand implements Runnable {
@CommandLine.Option(names="--option", defaultValue="123",lookup="optionValue")
int optionValue;
}
public class CustomValueProvider implements ValueProvider {
String defaultValue(ArgSpec argSpec) throws Exception;
} Which would then search in the following places:
The The addition of the resolve order gives flexibility on which sources are considered and in what order of precedence. Not specifying the resolveOrder would give the default order: I used the Naming of attributes, types and values in my example are probably not the best, but I hope they make clear what I would like to achieve. What do you think? |
Users currently cannot combine About introducing a resolveOrder mechanism, please note that users can already specify the order in the variable by nesting multiple lookups, and use different keys for different sources (system props, env vars, resource bundle). 👍
One thing though, I just realized that the default values from the command's If that were implemented, I believe that would meet your requirements, would you agree? |
The addition of variable interpolation to the A couple of remarks from my side.
|
I am still not convinced that specifying default values in both the I did notice that the user manual does not document this fact, that the There are pros and cons to the The default order for resolving variables is exactly as you specified ( That said, I just released 4.6.3, and it may take a while until the next release. |
FYI my previous statement was incorrect; if the default value provider does not have a default value for a specific option, then the default value in the code ( |
So if I understand correctly. The default value provided on the annotation is used when the defaultValueProvider doesn't provide a value? I will create a custom default provider to read and write configuration and possibly to system property and env variable resolution. Is there a way to disable the default mechanism of system property, env and resource bundle lookup as I'm thinking of handling that in the defaultProvider? |
Yes, that is correct. Sorry for the confusion.
As long as the value that is returned from the If the application does need default values that look like variables, then the dollar needs to be escaped with an extra dollar: |
I added this section to the user manual: https://picocli.info/#_variables_in_default_values |
I think it's good you clarified it for default values. It is more or less mentioned in chapter 20 Variable Interpolation, but you it's in a spot where you easily overlook the power of it. Happy to see this conversation lead to improvements for both of us. Keep up the good work. I need to get back to my own community contribution https://github.com/quarkiverse/quarkus-rabbitmq-client to finish some issues 😁 Feel free to close the issue as resolved. |
Cool, thanks for the confirmation. Good luck with your project, it looks nice (you have my 🌟 star). |
Is there a way to extend PicoCLI to support functionality like https://github.com/spf13/viper does for https://github.com/spf13/cobra?
The text was updated successfully, but these errors were encountered: