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

parseArgs doesn't use default value when the option is passed without value #54396

Closed
ematipico opened this issue Aug 15, 2024 · 3 comments
Closed
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. util Issues and PRs related to the built-in util module.

Comments

@ematipico
Copy link

ematipico commented Aug 15, 2024

Version

v20.11.1

Platform

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.

Here's a simple case:

const args = ['--host'];
const options = {
	host: {
		type: 'string',
		default: ''
	}
};

parseArgs({ args, options })

However, the utility throws an error.

How often does it reproduce? Is there a required condition?

Consistently with the snippet given

const args = ['--host'];
const options = {
	host: {
		type: 'string',
		default: ''
	}
};

parseArgs({ args, options })

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

Uncaught:
TypeError [ERR_PARSE_ARGS_INVALID_OPTION_VALUE]: Option '--host <value>' argument missing
    at checkOptionUsage (node:internal/util/parse_args/parse_args:106:11)
    at node:internal/util/parse_args/parse_args:360:9
    at Array.forEach (<anonymous>)
    at parseArgs (node:internal/util/parse_args/parse_args:357:3) {
  code: 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'

Additional information

No response

@ematipico ematipico 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 Aug 15, 2024
@RedYetiDev RedYetiDev added util Issues and PRs related to the built-in util module. repro-exists labels Aug 15, 2024
@RedYetiDev
Copy link
Member

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

@bakkot
Copy link

bakkot commented Aug 15, 2024

This is a duplicate of #53427. I agree with the comments there that this would not be good to include.

@RedYetiDev RedYetiDev added the duplicate Issues and PRs that are duplicates of other issues or PRs. label Aug 15, 2024
@RedYetiDev RedYetiDev closed this as not planned Won't fix, can't repro, duplicate, stale Aug 15, 2024
@shadowspawn
Copy link
Member

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

4 participants