Feature request: option to show usage when no args given (and/or on-demand)
jrr opened this issue · 3 comments
In addition to showing usage behind a help flag like -h
, I'd like to show it when the user hasn't supplied any arguments at all.
I managed to achieve it like this:
help: {
type: Boolean,
optional: true,
alias: "h",
description: "Prints this usage guide",
defaultValue: process.argv.length <= 2, // show help when the user supplies no args
},
..but it'd be nice to have this as a first-class feature.
Idea 1: an option to show help when given no arguments
I guess this would belong in ParseOptions. Maybe something like showHelpWhenNoArguments
?
{
helpArg: 'help',
headerContentSections: [{ header: 'My Example Config', content: 'Thanks for using Our Awesome Library' }],
footerContentSections: [{ header: 'Footer', content: `Copyright: Big Faceless Corp. inc.` }],
showHelpWhenNoArguments: true // <--
},
Idea 2: the ability to display help on-demand at any time
A more general solution would be a way to programmatically invoke the help screen, so I can choose to display it based on the parsed result:
const args = parse<CLIArguments>(...)
// ...
if (_.isEmpty(args)) {
// printHelp()
}
It might be tricky to design the API for that, though, since it requires access to information that is consumed by parse()
. One way could be to expose more things on args
:
const args = parse<CLIArguments>(...)
// ...
if(args.noArgumentsGiven){
args.printHelp();
}
Are either of these already possible? Do they sound useful?
I think the fixes I have submitted support either of your requested use cases. Take a look and let me know if you have any issues.
Wow, that was fast, thanks!
The thing is that I don't have any required arguments, they're all optional. (so I don't think the new options hideMissingArgMessages
/ showHelpWhenArgsMissing
/ helpWhenArgMissingHeader
will help with use case 1 above)
The new addCommandLineResults
option is great, though, and covers idea 2.
Here's what I'm doing now, instead of checking process.argv.length
:
if (_.isEqual(Object.keys(args), ["_commandLineResults"])) {
args._commandLineResults.printHelp();
}
(in other words, "when the user didn't supply any arguments at all, print the help")
I think that the easiest way for you to find out if any arguments at all were passed is just to use process.argv
.