0

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?

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117

3 Answers3

3

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
Richie Bendall
  • 7,738
  • 4
  • 38
  • 58
2

You just need to define a command on everything else (* wilcard) that shows the help.

yargs
  .command('test-all','',handler)
  .command('test-file','',handler)
  .command('*','', () => { y.showHelp(); })
  .argv
0

Best way to do this now is with:

.demandCommand().recommendCommands().strict()

This also solves the issue wherein you don't enter any command.

Clarence Liu
  • 3,874
  • 2
  • 25
  • 29