You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
parseArgs doesn't use default value when the option is passed without value #54396
Darwin *** 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 20:39:46 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T6020 arm64
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.
changed the title [-]`parseArgs` doesn't default value when the option is passed without value[/-][+]`parseArgs` doesn't use default value when the option is passed without value[/+]on Aug 15, 2024
└─$ node repro.jsnode: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
We have a case where an option can be a string or a boolean. However, `parseArgs' can't handle this
Correct, that is not supported.
so we try to mark it as a string and provide an empty string as a default value.
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).
Activity
[-]`parseArgs` doesn't default value when the option is passed without value[/-][+]`parseArgs` doesn't use default value when the option is passed without value[/+]avivkeller commentedon Aug 15, 2024
bakkot commentedon Aug 15, 2024
This is a duplicate of #53427. I agree with the comments there that this would not be good to include.
shadowspawn commentedon Aug 18, 2024
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).