I'm new with optimist and I've done a bit of googling and try outs, but I still can't find an elegant way to add a --help
option.
I see a help() option on the documentation. With this I would expect the following to work:
var argv = require('optimist')
.usage('Some usage')
.alias('l', 'local')
.describe('l', 'uses local repo')
.help()
.argv
So on the shell if I typed ./myScript --help
it would show the usage. I know that I can inspect the argv
for a -h
or --help
option and do console(argv.usage)
to print the usage, but I was trying to use the API instead of hacking it.
Is this a valid question? Thanks for the help.
bitoiu
When you want to be able to display a usage, you want to keep a pointer to the object that is returned from the require(). This is because the object returned from the .argv is just a plain object, so there is no way to access the help() or showHelp() functions. Below is a contrived example that I think will point you in the right direction for what you're trying to do.
var optimist = require('optimist')
.usage('$0: This is an example on how to use optimist')
.describe('h', 'Display the usage')
.describe('l', 'uses local repo')
.alias('h', 'help')
.alias('l', 'local');
var argv = optimist.argv;
if (argv.help) {
optimist.showHelp();
process.exit(0);
}
if (argv.local) {
// Do stuff based on local repo
console.info('Got the -l/--local flag!');
}
Then when you run the code, and pass in either -h or --help, you would get the following output:
node ./ex-optimist.js: This is an example on how to use optimist
Options:
-h, --help Display the usage
-l, --local uses local repo
Also note that using the .help function will return the usage as a string, and trying to use .argv after it will make your argv variable 'undefined'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With