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,
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.
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 --flagis identical to the output to the output for--flag arg.That is:
invoked with
and with
both give exactly the same output,
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
defaultvalue in the output array (ornull, if not specified), which I could check for.