Any way to set an option value programatically using an arg string ?
See original GitHub issueHi,
My use case is that I want a TOML config file with key/value matching the CLI options names :
CLI : myapp --option-one="super value"
TOML : option-one="super value"
In the end I could have myapp --config=my-toml-file.toml
Then once the CLI is already parsed and initialised, that I’m running into the right command, I analyse if my app have a config file and if so, I want the config file values to override options default values or CLI passed value.
Then I get my option with OptionSpec option = commandLine.getCommandSpec().findOption(optionNameWhichIsTOMLKey);
and I would like to be able to set the new value of my option from the TOML value without having to convert it but simply by passing the String arg “super value” and let Picoli convert the value to the right type itself as it does when passing the arg on command line using converters:
option.setValue(optionValueWhichIsTOMLValue);
But the setValue() is typed.
I don’t think parsing all args again is the way as I would like to keep all that was entered on CLI. The thing I think about is having a setter with a string type for options that would be able to parse the parameter as it does during command line parsing but individually for the defined option. Also do you see any other way to do what I want to do ? Thanks.
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (8 by maintainers)
Glad to hear that!
I was thinking to add
IDefaultValueProvider
getter and setter methods toCommandSpec
. In your application, all you would need to do is implement theIDefaultValueProvider
interface to return values from a config file, and call thecommandSpec.defaultValueProvider
setter method with your implementation.When the
parse
method is called, picocli will first set the defaults, and then parse the actual input. The exact initialization sequence will be:So in your application you won’t need to get values from the default provider for each ArgSpec; this happens automatically. All you have to do is provide a
IDefaultValueProvider
implementation.Will communicate for this issue in #321