Search

8/12/2011

node-optimist /

node-optimist

optimist is a node.js library for option parsing for people who hate option parsing. More specifically, this module is for people who like all the --bells and -whistlz of program usage but think optstrings are a waste of time.

With optimist, option parsing doesn't have to suck (as much).


var argv = require('optimist').argv;

if (argv.rif - 5 * argv.xup > 7.138) {
console.log('Buy more riffiwobbles');
}
else {
console.log('Sell the xupptumblers');
}


$ ./xup.js --rif=55 --xup=9.52
Buy more riffiwobbles

$ ./xup.js --rif 12 --xup 8.1
Sell the xupptumblers


And non-hypenated options too! Just use argv._!

var argv = require('optimist').argv;
console.log('(%d,%d)', argv.x, argv.y);
console.log(argv._);


$ ./nonopt.js -x 6.82 -y 3.35 moo
(6.82,3.35)
[ 'moo' ]

$ ./nonopt.js foo -x 0.54 bar -y 1.12 baz
(0.54,1.12)
[ 'foo', 'bar', 'baz' ]


example: https://github.com/substack/node-browserify/blob/master/bin/cli.js


var argv = require('optimist').usage('Usage: $0 [entry files] {OPTIONS}').wrap(80).option('outfile', {
alias: 'o',
desc: 'Write the browserify bundle to this file.\n' + 'If unspecified, browserify prints to stdout.',
}).option('require', {
alias: 'r',
desc: 'A module name or file to bundle.require()\n' + 'Optionally use a colon separator to set the target.',
}).option('entry', {
alias: 'e',
desc: 'An entry point of your app'
}).option('alias', {
alias: 'a',
desc: 'Register an alias with a colon separator: "to:from"\n' + "Example: --alias 'jquery:jquery-browserify'",
}).option('plugin', {
alias: 'p',
desc: 'Use a plugin. Use a colon separator to specify additional ' + 'plugin arguments as a JSON string.\n' + 'Example: --plugin \'fileify:["files","."]\''
}).option('help', {
alias: 'h',
desc: 'Show this message'
}).check(function (argv) {
if (argv.help) throw ''
if (process.argv.length <= 2) throw 'Specify a parameter.'
}).argv;

沒有留言: