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).
examples
With Optimist, the options are just a hash! No optstrings attached.
xup.js:
#!/usr/bin/env nodevar 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.52Buy more riffiwobbles$ ./xup.js --rif 12 --xup 8.1Sell the xupptumblers
The rest of these methods below come in just before the terminating .argv.
.alias(key, alias)
Set key names as equivalent such that updates to a key will propagate to aliases
and vice-versa.
Optionally .alias() can take an object that maps keys to aliases.
.default(key, value)
Set argv[key] to value if no option was specified on process.argv.
Optionally .default() can take an object that maps keys to default values.
.demand(key)
If key is a string, show the usage information and exit if key wasn't
specified in process.argv.
If key is a number, demand at least as many non-option arguments, which show
up in argv._.
If key is an Array, demand each element.
.describe(key, desc)
Describe a key for the generated usage information.
Optionally .describe() can take an object that maps keys to descriptions.
.options(key, opt)
Instead of chaining together .alias().demand().default(), you can specify
keys in opt for each of the chainable methods.
For example:
var argv = require('optimist') .options('f', { alias : 'file', default : '/etc/passwd', }) .argv;
is the same as
var argv = require('optimist') .alias('f', 'file') .default('f', '/etc/passwd') .argv;
Optionally .options() can take an object that maps keys to opt parameters.
.usage(message)
Set a usage message to show which commands to use. Inside message, the string
$0 will get interpolated to the current script name or node command for the
present script similar to how $0 works in bash or perl.
.check(fn)
Check that certain conditions are met in the provided arguments.
If fn throws or returns false, show the thrown error, usage information, and
exit.
.boolean(key)
Interpret key as a boolean. If a non-flag option follows key in
process.argv, that string won't get set as the value of key.
If key never shows up as a flag in process.arguments, argv[key] will be
false.
If key is an Array, interpret all the elements as booleans.
.string(key)
Tell the parser logic not to interpret key as a number or boolean.
This can be useful if you need to preserve leading zeros in an input.
If key is an Array, interpret all the elements as strings.
.wrap(columns)
Format usage output to wrap at columns many columns.
.help()
Return the generated usage string.
.showHelp(fn=console.error)
Print the usage data using fn for printing.
.parse(args)
Parse args instead of process.argv. Returns the argv object.
.argv
Get the arguments as a plain old object.
Arguments without a corresponding flag show up in the argv._ array.
The script name or node command is available at argv.$0 similarly to how $0
works in bash or perl.
parsing tricks
stop parsing
Use -- to stop parsing flags and stuff the remainder into argv._.
Every argument that looks like a number (!isNaN(Number(arg))) is converted to
one. This way you can just net.createConnection(argv.port) and you can add
numbers out of argv with + without having that mean concatenation,
which is super frustrating.
duplicates
If you specify a flag multiple times it will get turned into an array containing
all the values in order.