Skip to content

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

Closed as not planned
@ematipico

Description

@ematipico

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateIssues and PRs that are duplicates of other issues or PRs.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions