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
getOptionValueSource() in a command context returns undefined
for global options
#1713
Comments
I am not sure of best approach for your case. You could possibly write your own If you have global options and local options, and whatever you are doing with them you need to tell where they came from using
For interest, can you give an insight into why you need to distinguish?
My first impression is it is not a common enough case to build into Commander. But happy to leave issue open and see if other people have similar need. |
I'll add that |
I'm working on a CLI that can use options from a config file, that I load when the CLI is invoked. But I need to be able to override any options from the config file using CLI options, hence this need. I would have thought that the use case is pretty common 😄 At the command level, I need both global and command options to execute the command, and I process all options by merging the options that comes from the config file, then from the CLI. As a lot of options have default values, I need to only keep CLI options that were explicitly set by the user to override the ones from the config file. BTW, I found a simple workaround to implement
This works as |
im having this exact same issue. @sinedied can you give a bit more context on what option and command are in your little code snippet? is option a string? |
Yes, in that snippet |
Opened a PR to implement |
Added in Commander v9.5.0 |
Hello,
I need to distinguish in my code which options comes from the user (via CLI options), and which one are just the default values.
For that, I use
getOptionValueSource()
but I have an issue with that, as it always return undefined when trying to get the source of a global option in a command context.I retrieve all options using
command.optsWithGlobals()
, but thencommand.getOptionValueSource()
only works for command-level options, and if I useprogram.getOptionValueSource()
it only works with global-level options.Should I merge all options manually instead of using
optsWithGlobals()
to separate the two contexts when trying to determine the source of the options? It would seems a bit complicated for a simple use case.Would you consider adding a
getOptionValueSourceWithGlobals
to match the behavior ofoptsWithGlobals
, so that we could retrieve the option's source if it comes either from command or global level?The text was updated successfully, but these errors were encountered: