question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any way to set an option value programatically using an arg string ?

See original GitHub issue

Hi, 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:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
remkopcommented, Aug 30, 2018

Glad to hear that!

I was thinking to add IDefaultValueProvider getter and setter methods to CommandSpec. In your application, all you would need to do is implement the IDefaultValueProvider interface to return values from a config file, and call the commandSpec.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:

  1. clear the argSpec value
  2. if initial value exists, set this value (field value in the case of annotated fields)
  3. if default value was specified, set this value
  4. if default value provider has non-null value for this argSpec, set this value // <- this needs to be added
  5. set value from user input

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.

0reactions
NicolasMassartcommented, Aug 30, 2018

Will communicate for this issue in #321

Read more comments on GitHub >

github_iconTop Results From Across the Web

Value of <select><option> coming back as String instead of ...
option 's property value is always a string. Try to use [ngValue] instead. ... option element is overridden by angular 's NgSelectOption directive ......
Read more >
How do I set an active value on select dropdown control ...
This FAQ explains the topic "How do I set an active value on select dropdown control programmatically?"
Read more >
Frequently Asked Questions — Ansible Documentation
How can I set the PATH or any other environment variable for a task or entire play? . Setting environment variables can...
Read more >
Function.prototype.call() - JavaScript - MDN Web Docs - Mozilla
The call() method calls the function with a given this value and arguments provided individually. Try it.
Read more >
Set Simulink parameter value - MATLAB set_param - MathWorks
Setting multiple parameters with one function call is efficient because one call evaluates the parameters only once. If any parameter names or values...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found