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
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
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
yeah sorry I was thinking of program.args that ends up being the excess args after parsing the options
ahh ok that makes more sense - thx
This can be closed:
console.log( program.helpInformation() );
In case this helps anyone else out, here's an example of how to do this (with [email protected]):
// ...
// 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();
}
@mikermcneil thank you!
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
Docs PR would be fantastic - pull away!
PR sent, sorry for the delay
What if I want the argument missing messages and not the help?
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.
+1 for this
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);
});
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();
}
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
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.
Most helpful comment
In case this helps anyone else out, here's an example of how to do this (with
[email protected]):