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

Implicit value for command options #1595

Closed
darrenrahnemoon opened this issue Sep 5, 2021 · 3 comments
Closed

Implicit value for command options #1595

darrenrahnemoon opened this issue Sep 5, 2021 · 3 comments

Comments

@darrenrahnemoon
Copy link

Say I have the following command option:

program.addOption(new Option('--watch', 'some description...'));

(The style in which the option doesn't take an argument itself)

If I don't specify the --watch option when running the command, it returns option: false whereas a better value IMO would've been watch: undefined which is falsy enough but also gives us a hint as to whether or not this option was explicitly false or implicitly false.

I understand that this philosophy wouldn't work for negatable options since in that case undefined === implicit true which would be odd since undefined is falsy.

Is there a way to tell whether or not an option was explicitly set or implicitly?


Bit of context:

I have a command that takes in the option mentioned in the example above, but also can check for the same "flag" in the process.env.

Now if the option flag has been explicitly set, I'd ignore the environment variables, since it's a direct override from the shell. However if the option is not explicitly set I'd like to use the environment variables as backup.

Problem is: by not explicitly adding watch returns false and there's nowhere to find out whether it was explicitly set or implicitly other than maybe traversing through the process args by hand and seeing if the flag is present.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Sep 5, 2021

If I don't specify the --watch option when running the command, it returns option: false whereas a better value IMO would've been watch: undefined

I think you have something else in your code, as watch: false is not the expected behaviour or what I am seeing.

program.addOption(new Option('--watch', 'some description...'));
program.parse();
console.log(program.opts());
% node index.js
{}
% node index.js --watch
{ watch: true }

Is there a way to tell whether or not an option was explicitly set or implicitly?

No, not currently.


I have a command that takes in the option mentioned in the example above, but also can check for the same "flag" in the process.env.

This might be what you are waiting for: #1587

@darrenrahnemoon
Copy link
Author

darrenrahnemoon commented Sep 5, 2021

This might be what you are waiting for: #1587

Wow either the universe has aligned or that feature was really high in demand :D
Closing this issue then.

Side note: What's the ETA on that entire round getting merged to master?

@shadowspawn
Copy link
Collaborator

Side note: What's the ETA on that entire round getting merged to master?

#1587 will go in a feature release and does not require a major release. Probably fairly soon. Like weeks, not months.

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

No branches or pull requests

2 participants