Yargs: Default command: help on error should display other available commands

Created on 4 Dec 2019  路  3Comments  路  Source: yargs/yargs

See https://github.com/yargs/yargs/issues/1331#issuecomment-559247460

If you have a default command, and the user makes an error in the args, then yargs should not output usage for the default command (current behaviour). The problem is, the user may want to use another command, not the default, and the user does not know the app knows other commands. yargs should output general help node index.js --help instead, it contains list of commands and args for the default command, too.

Example:

const options = yargs
  .command(['cmd1', '*'], 'Command 1', { 'value': { demandOption: true } })
  .command('cmd2', 'Command 2', { 'value': { demandOption: true } })
  .help().strict(true).demandCommand(1)
  .parse();

Then run node index.js foo - an error argument -- and output is (shortened):

Command 1

Options:
  --version  Show version number                                      [boolean]
  --help     Show help                                                [boolean]
  --value                                                             [required]

But I would expect same similar to node index.js --help, i.e.

Command 1                                                                           

Commands:                                                                           
  index.js cmd1  Command 1                                             [default]    
  index.js cmd2  Command 2                                                          

Options:                                                                            
  --version  Show version number                                      [boolean]    
  --help     Show help                                                [boolean]    
  --value                                                             [required]                                

So, that the user knows, the app has more commands.

feature request

Most helpful comment

@xmedeko @mleguen I ran into this issue as well and discovered a workaround:

const { _:[cmd] } = yargs
    .command('foo', ...)
    .command('bar', ...)
    .command('baz', ...)
    .demandCommand()
    .help()
    .argv

// Check which command was executed
switch (cmd) {
    // Skip valid commands
    case 'foo':
    case 'bar':
    case 'baz': break
    // Otherwise, show full help output
    default: yargs.showHelp()
}

All 3 comments

@xmedeko @mleguen I ran into this issue as well and discovered a workaround:

const { _:[cmd] } = yargs
    .command('foo', ...)
    .command('bar', ...)
    .command('baz', ...)
    .demandCommand()
    .help()
    .argv

// Check which command was executed
switch (cmd) {
    // Skip valid commands
    case 'foo':
    case 'bar':
    case 'baz': break
    // Otherwise, show full help output
    default: yargs.showHelp()
}

Is there a work in progress over this issue?

This has been fixed on the main branch, and you can experiment with it here:

npm i yargs@next

It will probably be a few more weeks before yargs@v17 is released.

Was this page helpful?
0 / 5 - 0 ratings