Skip to content

Allow parseArgs options to have an optional value #53427

Closed as not planned
Closed as not planned
@AtkinsSJ

Description

@AtkinsSJ

What is the problem this feature will solve?

It's not unusual for command-line options to have optional arguments. For example, git has --color[=<when>]: If an argument is provided, it's used, but if --color is given without an argument, that argument defaults to always.

parseArgs() does not support this kind of optional argument. Either the option's type is boolean and any argument throws an error; or it's string and a missing argument throws an error.

What is the feature you are proposing to solve the problem?

Add a field to the option definition so that a string option is allowed to have no argument, and use a default value in that case. For example, the --color[=<when>] case could look like this:

{
	color: {
		type: 'string',
		valueIfNoArgument: 'always',
	}
}

Probably someone can think of a better name for this field.

What alternatives have you considered?

The alternative is to disable strict mode, and manually process the tokens array. However, this is a lot of boilerplate, and requires reimplementing the checks for which options are valid, making it error-prone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.utilIssues and PRs related to the built-in util module.wontfixIssues that will not be fixed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions