Search code examples
javascriptnode.jsyargs

Using yargs (npm package), how to give an error if command not found?


I have yargs with 2 commands?

yargs
.command('test-all','',handler)
.command('test-file','',handler)
.argv

The user inputs:

node myapp.js other-command

But yargs don't throw an error. What should I do?


Solution

  • You can use .demandCommand() to do this:

    yargs
      .command('test-all','',handler)
      .command('test-file','',handler)
      .demandCommand(1, 'You need at least one command before moving on')
      .argv