-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
It's not possible to customize environment variables with booleans. #1787
Comments
I am interested in understanding the use case a bit better. Are you using Commander to generate a metadata file, or to consume environment variables produced from a metadata file? In the metadata file I think the "booleans" are per YAML which has a wider range: https://yaml.org/type/bool.html I don't know what environment variable value will be produced from a "boolean" but expect it will be just a pair of possible values (like say TRUE/FALSE). |
You do get custom parsing of the environment variable by declaring the option as taking a value. Say:
|
Both. Consuming:
That's version 1.1.
Yaml boolean are string that needs a parser, they are injected to the environment without been parsed and parsed later at
Actually I am targeting a generic alternative. program.js: export function buildCommand(command = new Command()) {
command.option(...)....
} cli.js (executable): import { buildCommand } from './program.js';
const options = buildCommand().parse(); action.js (GitHub action entrypoint): import { Command } from 'commander-github-actions';
import { buildCommand } from './program.js';
const options = buildCommand(new Command()).parse(); A commander application can become a GitHub Action almost out of the box. |
(Not likely to add a parameter directly to |
(Thanks for the detailed info.) |
I think you can custom process env for a boolean option by adding a listener for the (Let me know if you would like a proof of concept. I can imagine it working, but have not tried it!) |
There is seldom a need to hook the events these days, but they are available. The order of the events matters. By adding the listener after the option my listener will be called after the default processing, and can overwrite the value with the custom value. program
.addOption(new Option('-d, --debug').env('DEBUG'))
.on('optionEnv:debug', function () {
// The event does not pass the value for a boolean option, so get it ourselves.
const rawValue = process.env.DEBUG;
const value = !['false', 'False', 'FALSE'].includes(rawValue);
this.setOptionValueWithSource('debug', value, 'env');
});
program.parse();
console.log(program.opts()); $ DEBUG=FALSE node index.js
{ debug: false }
$ DEBUG=TRUE node index.js
{ debug: true } Note to future readers: if this was used in practice, I expect a subclass of Command would add the listener after adding a boolean option with an envVar. |
That won’t work for my use case.
and breaks default
|
That is an extra problem, separate from your original issue with boolean options, as affects non-boolean options too. My first thought is to groom |
After the discussion, reusing the environment support commander provides is not possible without complicated changes. |
The new code in your project treating the GitHub env as a separate parse stage looks nicer than trying to wrangle it into Commander's treatment of environment variables.
Rather than discussing here? Yes. I'll make a couple of comments since I looked anyway. It looks like you are emitting an event from a particular point in the parsing. I don't think this is a general solution for custom config parsing. Different approaches get inserted at different points in the parsing. There was considerable discussion of config files in a previous issue, which revealed how varied the approaches are: |
It's not possible to customize environment variables parsing like:
FOO=false command
Because the variable value is not forwarded:
commander.js/lib/command.js
Lines 1565 to 1568 in f2aec26
GitHub Actions exposes inputs using the pattern:
Where allowed boolean values are ['true', 'True', 'TRUE', 'false', 'False, 'FALSE'];
The proposal is to drop the condition
commander.js/lib/command.js
Line 1562 in f2aec26
And add source argument to
argParser()
changing:commander.js/lib/command.js
Line 539 in f2aec26
to:
This would allow something like:
The text was updated successfully, but these errors were encountered: