-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
parseArgs
doesn't use default value when the option is passed without value
#54396
Comments
parseArgs
doesn't default value when the option is passed without valueparseArgs
doesn't use default value when the option is passed without value
const args = ['--host'];
const options = {
host: {
type: 'string',
default: ''
}
};
require('util').parseArgs({ args, options }) └─$ node repro.js
node:internal/util/parse_args/parse_args:116
throw new ERR_PARSE_ARGS_INVALID_OPTION_VALUE(`Option '${shortAndLong} <value>' argument missing`);
^
TypeError [ERR_PARSE_ARGS_INVALID_OPTION_VALUE]: Option '--host <value>' argument missing
at checkOptionUsage (node:internal/util/parse_args/parse_args:116:11)
at node:internal/util/parse_args/parse_args:381:9
at Array.forEach (<anonymous>)
at Object.parseArgs (node:internal/util/parse_args/parse_args:378:3)
at Object.<anonymous> (/repro.js:9:17)
at Module._compile (node:internal/modules/cjs/loader:1546:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
at Module.load (node:internal/modules/cjs/loader:1317:32)
at Module._load (node:internal/modules/cjs/loader:1127:12)
at TracingChannel.traceSync (node:diagnostics_channel:315:14) {
code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
}
Node.js v22.6.0 |
This is a duplicate of #53427. I agree with the comments there that this would not be good to include. |
Correct, that is not supported.
The default value is only used when the option does not appear in the arguments to be parsed. Were you unclear about the behaviour, or wishing it was different as a work-around? If the default value was used as you tried, it would not be possible to tell the difference between the option unused or the option-value missing. For interest and prior art, Commander used to do what you describe and it was changed because of multiple issues (tj/commander.js#1652). |
Version
v20.11.1
Platform
Subsystem
No response
What steps will reproduce the bug?
We have a case where an option can be a string or a boolean. However, `parseArgs' can't handle this, so we try to mark it as a string and provide an empty string as a default value.
Here's a simple case:
However, the utility throws an error.
How often does it reproduce? Is there a required condition?
Consistently with the snippet given
What is the expected behavior? Why is that the expected behavior?
I would expect to not throw and error, and the utility should give
host
where its value is''
What do you see instead?
An error
Additional information
No response
The text was updated successfully, but these errors were encountered: