Skip to content

util: parseArgs should support required in options[key] #44564

Closed as not planned
@himself65

Description

@himself65

What is the problem this feature will solve?

I'm building a CLI that creates some instances by id and name. But I have to check if all options are filled by the user one by one.

const args = process.argv.slice(2)

const result = parseArgs({
  args,
  options: {
    owner: {
      type: 'string'
    },
    repo: {
      type: 'string'
    }
  }
})

// imaging you will have tens of options in your CLI
if (result.values.repo == null || result.values.owner == null) {
  throw new Error('require parameters')
}
./build.js --owner=himself65 --repo=xxx

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

Add required option.

parseArgs({
  args,
  options: {
    owner: {
      type: 'string',
      required: true
    },
    repo: {
      type: 'string',
      required: true
    },
    force: {
      type: 'boolean',
      // by default, required is false
    },
  }
})

What alternatives have you considered?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.staleutilIssues 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