Repository navigation
parseArgs doesn't use default value when the option is passed without value #54396
Description
Activity
- 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 - addedutilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
on Aug 15, 2024 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.
Reacted by Aviv Keller- addedduplicateIssues and PRs that are duplicates of other issues or PRs.Issues and PRs that are duplicates of other issues or PRs.
on Aug 15, 2024 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).
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
hostwhere its value is''What do you see instead?
An error
Additional information
No response