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

getOptionValueSource() in a command context returns undefined for global options #1713

Closed
sinedied opened this issue Apr 7, 2022 · 7 comments

Comments

@sinedied
Copy link

sinedied commented Apr 7, 2022

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 then command.getOptionValueSource() only works for command-level options, and if I use program.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 of optsWithGlobals, so that we could retrieve the option's source if it comes either from command or global level?

@shadowspawn
Copy link
Collaborator

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.

I am not sure of best approach for your case. You could possibly write your own getOptionValueSourceWithGlobals which searches up the command chain looking for a hit. (I haven't tried this and not sure if there are complications.)

If you have global options and local options, and whatever you are doing with them you need to tell where they came from using .getOptionValueSource(), I don't think you get to call it simple. 😄

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 interest, can you give an insight into why you need to distinguish?

Would you consider adding a getOptionValueSourceWithGlobals to match the behavior of optsWithGlobals, so that we could retrieve the option's source if it comes either from command or global level?

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.

@shadowspawn
Copy link
Collaborator

I'll add that .optsWithGlobals() was added in 9.0 and .getOptionValueSource() in 8.3, so both fairly new and may take a while to see how they are being used and could be improved. Interesting to hear you are using both. 👍

@sinedied
Copy link
Author

sinedied commented Apr 7, 2022

For interest, can you give an insight into why you need to distinguish?

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 getOptionValueSourceWithGlobals():

const getOptionValueSourceWithGlobals(option, command) => command. getOptionValueSource(option) ?? program.getOptionValueSource(option);

This works as getOptionValueSource returns undefined when the option is not found in the current context.

@michaelAtCoalesce
Copy link

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?

@shadowspawn
Copy link
Collaborator

Yes, in that snippet option is a string and command is the Command object as passed to the action handler (or saved when the command created).

@shadowspawn
Copy link
Collaborator

shadowspawn commented Dec 17, 2022

Opened a PR to implement .getOptionValueSourceWithGlobals(): #1832

@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Dec 19, 2022
@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label Jan 7, 2023
@shadowspawn
Copy link
Collaborator

Added in Commander v9.5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants