yargs / yargs

yargs the modern, pirate-themed successor to optimist.
https://yargs.js.org/
MIT License
11.09k stars 992 forks source link

Yargs exports.builder not correctly modeling a long option preceded by positional arguments #1193

Open Tarasaur opened 6 years ago

Tarasaur commented 6 years ago

Hi!

I am unable to successfully model a complex command using a sequence of positional arguments followed by a long option flag.

Example usage I want to achieve:

myScript fancyEntry 3 fancyChoice --entryFlag

I have something like the following:

exports.command = 'myScript <entryName> <entryNumber> [entryChoice] [entryFlag]';
exports.builder = (yargs) => yargs
  .positional('entryName', {
    alias: 'eName',
    type: 'string'
  })
  .positional('entryNumber', {
    alias: 'eNumber',
    type: 'number'
  })
  .positional('entryChoice', {
    alias: 'eChoice',
    choices: ['plainChoice', 'fancyChoice'],
    default: 'plainChoice',
    type: 'string'
  })
  .option('entryFlag', {
    type: 'boolean'
  });
exports.handler = (argv) => {
  if (argv.entryFlag) {
    // Do something
  } else {
    // Do something else
  }
}

However, when running my command, argv.entryFlag always seems to be false, even when it is present in my command.

I tried explicitly assigning arguments and triggering the value in roundabout ways, such as:

myScript fancyEntry 3 "fancyChoice" --entryFlag
myScript fancyEntry 3 entryChoice="fancyChoice" --entryFlag
myScript fancyEntry 3 entryChoice="fancyChoice" --entryFlag=true
myScript fancyEntry 3 entryChoice="fancyChoice" true

No luck, though.

I would love for --entryFlag to behave like --verbose in that it is an optional argument, and if present, its value is true.

Have I grossly misconfigured something?

hudovisk commented 6 years ago

Same problem here