Search code examples
javascriptnode.jsyargs

getting message for "Invalid values:" for yargs


I am using yargs to develop a cli tool . Here is simple yargs code to process the cli option

let argv = require('yargs')
    .usage('$0 <command> [option]')
    .command(
        'validate_zip',
        'validate the directory structure for the zip to be uploaded',
        {
            'validate_zip': {
                alias: 'vz'
            }
        }

    )
    .option('s', {
        alias: 'stage',
        describe: 'stage',
        type: 'string',
        choices: ['dev', 'qa', 'uat', 'prod'],
        count: true
    })
    .demandCommand(1, 'You need at least one command before moving on!')
    .help('h')
    .alias('h', 'help')
    .example('$0 validate_zip -s dev', 'testing yargs')
    .showHelpOnFail(false, "Specify --help || -h for available options")
    .argv;

Here is the cli command node testYargs.js vz -s dev . I tried passing "dev", but having the same issue.

and it displaying the following message

Invalid values:
  Argument: s, Given: 1, Choices: "dev", "qa", "uat", "prod"

Specify --help || -h for available options

Solution

  • count option should not be set to true. Count has a special meaning, indicating that the number of occurrences of a flag should be counted:

    details about count. can be found in original yargs api documentation.

    So -v -v -v would setv = 3, this will break the Choices logic.

    credit to original answer.