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 if no command was executed #7

Closed
ghost opened this issue Aug 24, 2011 · 20 comments
Closed

Show help if no command was executed #7

ghost opened this issue Aug 24, 2011 · 20 comments

Comments

@ghost
Copy link

ghost commented Aug 24, 2011

Hi

I wanted my utility to show help if no command was executed.

This was how I monkey patched my code to solve it.

parentAction = commander.Command::action
commander.Command::action = (fn) ->
    newFn = ->
        fn.apply this, arguments
        commander.commandExecuted = this
    parentAction.apply this, [newFn]

# later after parse statement
if !commander.commandExecuted?
    process.stdout.write commander.helpInformation()

Is this an acceptable solution, or is there something I'm missing in the API?

Chris

@tj
Copy link
Owner

tj commented Aug 24, 2011

sometimes you want to accept stdin etc by default, but I'd like some kind of nice option for sure. the best would be probably just to add .outputHelpInformation() and then in your script at the top do:

 if (!process.argv.length) program.outputHelpInformation();

or similar

@ghost
Copy link
Author

ghost commented Aug 24, 2011

true about stdin so it would be better off as an option, but aside from that, I think my code also allows for the fact that if people type in spurious commands.

BTW, I think your code meant if (process.argv.length == 2) ....

Chris

@tj
Copy link
Owner

tj commented Aug 24, 2011

yeah sorry I was thinking of program.args that ends up being the excess args after parsing the options

@ghost
Copy link
Author

ghost commented Aug 24, 2011

ahh ok that makes more sense - thx

@ghost
Copy link

ghost commented Sep 20, 2011

This can be closed:

console.log( program.helpInformation() );

@ghost ghost closed this as completed Sep 22, 2011
@mikermcneil
Copy link

In case this helps anyone else out, here's an example of how to do this (with commander@2.1.0):

// ...
// Do all the normal things
// ...

// After parsing your commander program....
program.parse(process.argv);

// Check the program.args obj
var NO_COMMAND_SPECIFIED = program.args.length === 0;

// Handle it however you like
if (NO_COMMAND_SPECIFIED) {
  // e.g. display usage
  program.help();
}

@heff
Copy link

heff commented Jan 26, 2014

@mikermcneil thank you!

@foxx
Copy link
Contributor

foxx commented Jul 13, 2014

Might be worth adding some docs to the README, as it took me a while to figure this out. Would a docs patch PR be accepted?

You cannot use program.args because then it won't detect things like --option, only the unnamed arguments. Although admittedly that may be a desired use case.

Anyway, to get this working, I had to use;

  if (!process.argv.slice(2).length) {
    program.outputHelp();
    return;
  }

This allows me to do;

$ cmd
Help displayed

$ cmd -o
Help not displayed

$ cmd lala
Help not displayed

@SomeKittens
Copy link
Collaborator

Docs PR would be fantastic - pull away!

@foxx
Copy link
Contributor

foxx commented Jan 11, 2015

PR sent, sorry for the delay

zhiyelee pushed a commit that referenced this issue Jan 15, 2015
@mgenereu
Copy link

What if I want the argument missing messages and not the help?

@mgenereu
Copy link

I should leave an example. Given this code:

program
  .version('1.0.0')
  .arguments('<abc> <def> <ghi>')
  .parse(process.argv)

my experience is:

$ program
$ program first
  error: missing required argument `def'
$ program first second
  error: missing required argument `ghi'

I was expecting:

$ program
  error: missing required argument `abc'
  error: missing required argument `def'
  error: missing required argument `ghi'
$ program
  error: missing required argument `def'
  error: missing required argument `ghi'
$ program first second
  error: missing required argument `ghi'

Happy to make a pull request if this is anyone else's expected behavior. It could also be activated by a flag.

@pedro93
Copy link

pedro93 commented Mar 8, 2016

+1 for this

@lancedolan
Copy link

For posterity wondering specifically about the case where a wrong command is given, such as a case where a user gives a bunch of arguments but forgets the command itself, this still isn't handled in the latest commander and the user will get blank unhelpful output.

However, the command docs provide a very easy to handle the case explicitly yourself:

// error on unknown commands
program.on('command:*', function () {
  console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));
  process.exit(1);
});

@anantanandgupta
Copy link

here is what i have written

program.allowUnknownOption(false);

const parsed = program.parse(process.argv);
if (!(parsed.args && parsed.args.length > 0 && (typeof (parsed.args[0] === 'object')))) {
  program.outputHelp();
}

@antonkulaga
Copy link

It is really annoying that there is not flag to automatically display help after parse errors when it crashes. If it will not be added I will just switch to another CLI lib in my next project

@shadowspawn
Copy link
Collaborator

Open a new issue with more details if you want visibility on this @antonkulaga

There have been lots of improvements to the error messages and help since this issue was opened and closed in 2011.

@jcbdev
Copy link

jcbdev commented May 6, 2021

I know this is closed but if anyone finds themselves back here then the best solution is:

program
    .exitOverride(err => {
      if (err.code == 'commander.missingArgument') program.help();
      // add other error codes if you have different scenarios (options etc)
    });

  await program.parseAsync(process.argv);

@shadowspawn
Copy link
Collaborator

FYI as recent commenters @jcbdev @antonkulaga : PR opened #1534

@shadowspawn
Copy link
Collaborator

Related: .showHelpAfterError() added in prerelease v8.0.0-2 by PR #1534

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

No branches or pull requests