Search code examples
node.jsyargs

yargs warning: Too many arguments provided. Expected max 1 but received 2


I have an issue with my yargs configuration:

const argv = require('yargs')
    .boolean('reset', {
        alias: 'rs'
    })
    .boolean('refreshConfig', {
        alias: 'rc'
    })
    .option('harvest', {
        alias: 'h'
    })
    .option('lang', {
        alias: 'l',
        default: 'fr'
    })
    .help().argv;

I executed the script as below:

$ node ./srcjobs/cli.js --refreshConfig --harvest=facebook

and I received this error:

Too many arguments provided. Expected max 1 but received 2.

Do you know why ? Thank you for your help.


Solution

  • .boolean receive only 1 argument, from source code

    boolean<K extends string>(key: K | ReadonlyArray<K>): Argv<T & { [key in K]: boolean | undefined }>;
    

    Proper way

    const argv = require('yargs')
      .boolean('reset')
      .alias('rs', 'reset')
      .boolean('refreshConfig')
      .alias('rc', 'refreshConfig')
      .option('harvest', {
        alias: 'h'
      })
      .option('lang', {
        alias: 'l',
        default: 'fr'
      })
      .help().argv;