-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 mandatory arguments not used #546
Comments
You could try testing commander.args in addition to process.argv. Bear in mind that it may contain a circular reference to commander. I'm seeing inconsistent behaviour with the
So I updated test/test.arguments.js to match this scenario and it passes. Unless I add a |
The original poster asked about mandatory non-option arguments. There is not currently automatic support for this. In the simple example with an argument to the program you can make a simple test for whether anything is left after the options are consumed by testing program.args.
|
(Note: deleted a couple of comments in this thread that were not contributing to improving this package.) |
This issue has not seen much activity in the passing years. Feel free to open a new issue if it comes up again, with new information and renewed interest. Closing in favour of #230. Thank you for your contributions. |
Hi guys, I am working on simple cli tool, and encountered the same error but I feel like it was never really addressed. I there a proper way to handle that missing argument if we want to display an error message or the help (without having to write our own argv parser)? If not, I am okay to work on that. |
There is not currently automatic support for displaying the help if no arguments are supplied, in the example program of original poster. I gave one approach for detecting in: #546 (comment) And there is an open issue about detecting including more complicated cases, with some suggestions, in: #1088 |
Oh indeed your first approach could solve my issue. |
If interested, see also #1062 for some discussion and digging into current state of affairs, and multiple ways |
As of today, #546 (comment) does not work anymore ([email protected]) C:\code\lib>node index.js
error: missing required argument 'engine'
C:\code\lib>node index.js aaa
Action called with aaa
C:\code\lib>node index.js -u bbb
error: missing required argument 'engine' Finally, I cannot get this sample to display "no command given!"
my ugly workaroundlet args = process.argv.slice(2); let arg, foundArg = false; do { arg = args.shift(); if (arg) { arg.replace(/^--/, '-'); switch (arg[1]) { case 'c': case 'n': // c and n are 2 optional parameters that specify a single, assuming the user did things correctly here :/ args.shift(); break; case 'd': // optional boolean parameter break; default: foundArg = true; break; } } } while (!foundArg && args.length > 0); if (!foundArg) { program.help(); } program.parse(process.argv); Slightly uglier workaround but more reliableprogram.exitOverride(); try { program.parse(process.argv); } catch (err) { program._exitCallback = null; program.help(); } |
The behaviour changed in 5.0.0:
Looks like I missed that entry in the README! I opened #1311 to update that. |
I think you could use |
@shadowspawn thank you for your input. program.exitOverride(() => {
// program.args is not defined when asking for the version -V
let args = program.args || [];
// this prevents duplicating the help when asking for it, not ideal but it seems to work
if (!(args.includes('-h') || args.includes('--help'))) {
program.outputHelp();
}
});
program.parse(process.argv); The only minor issue that remains is that the help is now displayed when asking for the version since it doesn't show up in the Lines 1587 to 1596 in c5a5e7b
|
I had to remind myself what
|
And here is a more specific implementation of
|
@shadowspawn Thank you, this implementation you gave does exactly what I wanted. |
I have the following code:
I was trying to have the program output the help text if the argument engine is not specified. Is there any built-in way to do this? At the moment the code works perfectly if I specify the engine. but if I just execute with any arguments then nothing is shown.
Thanks for taking the time to read this.
The text was updated successfully, but these errors were encountered: