Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show help for no command and handle unknown command #883

Closed
wclr opened this issue May 25, 2017 · 7 comments · Fixed by #1826
Closed

Show help for no command and handle unknown command #883

wclr opened this issue May 25, 2017 · 7 comments · Fixed by #1826
Labels

Comments

@wclr
Copy link

wclr commented May 25, 2017

I trouble to implement folliwng case:

  1. Show message unknown command if some unknnown command supplied
  2. Show help if no command supplied.

To handle unknown command I use default command *, it works ok

  .command({
    command: '*',
    handler: (argv) => {      
      if (argv._[0]) {
        console.log('Unknown commmand', argv._[0])        
      }
    }
  })
 .demand(1)

But demand(1) here doesn't work in this case so If I supply no command nothing happens at all (it goes to * handler)

I tried to use yargs.showHelp() somewhere in '*' handler or just checking main argv

const argv = yargs...

if (!argv._[0]) {
  yargs.showHelp()
}

but it doesn't show help. All it shows:

Options:
  --help  Show help                                                    [boolean]
@wclr wclr changed the title Show help and handle d Show help for no command and handle unknown command May 25, 2017
@the-spyke
Copy link

Same issue here. I don't what to use strict mode as I need to parse unspecified options. My temporary solution is to use a flag in handlers.

@fgarcia
Copy link

fgarcia commented Dec 20, 2017

for those waiting for this feature, here is how I do it

var yargs = require('yargs')

var argv = yargs
  .command('all', 'All projects', ya => {
    console.log('all')
  })
  .command('watch', 'watch')
  .command('short', 'short, just print spec file and console', () => {})
  .command('full', 'long, naming each test', () => {})

  .strict().argv

if (!argv._[0]) {
  yargs.showHelp()
}

@gempain
Copy link

gempain commented Mar 4, 2020

Another, shorter way:

yargs
  .demandCommand(1)
  .strict()

This will throw an error if you don't pass a command, and will print the help at the same time.

@Zelgadis87
Copy link

Zelgadis87 commented May 20, 2020

@gempain @fgarcia That unfortunately also throws on valid commands with unrecognized arguments, which is not exactly the same.
Unfortunately I have dynamic arguments. I would only need to check if the command is recognized with arbitrary arguments.

@gempain
Copy link

gempain commented May 21, 2020

@Zelgadis87 then I guess you have to use * and remove the .strict()

@OsmanAltun
Copy link
Contributor

Specifying default command still removes all commands from yargs.showHelp(callback).

+1

@bcoe
Copy link
Member

bcoe commented Feb 28, 2021

Cleaning up some stale issues, apologize this one was open for so long.

@wclr @Zelgadis87 @gempain there is now the method available .strictCommands(), which I believe provides exactly the behavior you want -- failing on unknown commands, including the default, but allowing any flags.

@OsmanAltun is working on a fix for .showHelp() listing commands in #1826, but I think `.strictCommands()`` is closer to what you're looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants