Skip to content

Impossible to validate missing arguments for a flag when multiple is set #120

Description

@bakkot

As in #112, I was surprised to find that parsing a flag with a type when the argument is omitted does not error, instead returning null. That issue points to the wiki, which says I should do the validation myself. Fair enough.

Unfortunately, it is impossible to do the validation myself when the flag is specified as multiple, because the output for --flag arg --flag is identical to the output to the output for --flag arg.

That is:

const commandLineArgs = require('command-line-args');

const options = [{
  name: 'flag',
  type: String,
  multiple: true,
  defaultValue: 'default',
}];

console.log(commandLineArgs(options));

invoked with

node cli-args.js --flag arg --flag

and with

node cli-args.js --flag arg

both give exactly the same output,

{ flag: [ 'arg' ] }

So it is not actually possible to check whether the user has passed a flag without specifying its (required) argument.

I was hoping that it would at least put the default value in the output array (or null, if not specified), which I could check for.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions