commander.js: Show help if mandatory arguments not used
I have the following code:
const version = JSON.parse(fs.readFileSync(path.join(__dirname, './package.json')) + '').version;
program
.version(version)
.arguments('<engine>')
.option('-u, --url <link>','indicates that the address parameter is to be interpreted as an url (value by default)')
.option('-f, --file <link>','indicates that the address parameter is to be interpreted as a file')
.action(main)
program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}
function main(engine){
console.log(engine);
if(program.file) {
renderer.execute(program.file,engine, {file: true}).then(console.log);
} else {
renderer.execute(program.url, engine).then(console.log).done();
}
};
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.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 15 (4 by maintainers)
And here is a more specific implementation of
.exitOverrideto look for the error of interest:I had to remind myself what
.parseOptionsreturns. This is the pre-flighting I had in mind to check for no arguments after top-level parse.