javascript - Trying to use Optimist API help() to print usage() -
i'm new optimist , i've done bit of googling , try outs, still can't find elegant way add --help option.
i see help() option on documentation. expect following work:
var argv = require('optimist') .usage('some usage') .alias('l', 'local') .describe('l', 'uses local repo') .help() .argv so on shell if typed ./myscript --help show usage. know can inspect argvfor -h or --help option , console(argv.usage) print usage, trying use api instead of hacking it.
is valid question? help.
bitoiu
when want able display usage, want keep pointer object returned require(). because object returned .argv plain object, there no way access help() or showhelp() functions. below contrived example think point in right direction you're trying do.
var optimist = require('optimist') .usage('$0: example on how use optimist') .describe('h', 'display 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) { // stuff based on local repo console.info('got -l/--local flag!'); } then when run code, , pass in either -h or --help, following output:
node ./ex-optimist.js: example on how use optimist options: -h, --help display usage -l, --local uses local repo also note using .help function return usage string, , trying use .argv after make argv variable 'undefined'.
Comments
Post a Comment