optimist / 0.6.1 last updated a year ago created on Dec 21st 2010
Install
npm install --save optimist
Clone
git clone git@github.com:substack/node-optimist.git
maintainers
versions
39 total
| 0.6.1 | 2 years ago | substack |
| 0.6.0 | 3 years ago | substack |
| 0.5.2 | 3 years ago | substack |
| 0.5.1 | 3 years ago | substack |
| 0.5.0 | 3 years ago | substack |
| 0.4.0 | 3 years ago | substack |
| 0.3.7 | 3 years ago | substack |
| 0.3.6 | 3 years ago | substack |
| 0.3.5 | 4 years ago | substack |
| 0.3.4 | 4 years ago | substack |
| 0.3.3 | 4 years ago | substack |
| 0.3.1 | 4 years ago | substack |
| 0.3.0 | 4 years ago | substack |
| 0.2.8 | 5 years ago | substack |
| 0.2.7 | 5 years ago | substack |
| 0.2.6 | 5 years ago | anonymous |
| 0.2.5 | 5 years ago | anonymous |
| 0.2.4 | 5 years ago | anonymous |
| 0.2.3 | 5 years ago | anonymous |
| 0.2.2 | 5 years ago | anonymous |
| 0.2.1 | 5 years ago | anonymous |
| 0.2.0 | 5 years ago | anonymous |
| 0.1.9 | 5 years ago | anonymous |
| 0.1.8 | 5 years ago | anonymous |
| 0.1.7 | 5 years ago | anonymous |
| 0.1.6 | 5 years ago | anonymous |
| 0.1.5 | 5 years ago | anonymous |
| 0.1.4 | 5 years ago | anonymous |
| 0.1.3 | 5 years ago | anonymous |
| 0.1.2 | 5 years ago | anonymous |
| 0.1.1 | 5 years ago | anonymous |
| 0.1.0 | 5 years ago | anonymous |
| 0.0.7 | 5 years ago | anonymous |
| 0.0.6 | 5 years ago | anonymous |
| 0.0.5 | 5 years ago | anonymous |
| 0.0.4 | 5 years ago | anonymous |
| 0.0.3 | 5 years ago | anonymous |
| 0.0.2 | 5 years ago | anonymous |
| 0.0.1 | 5 years ago | anonymous |
readme
DEPRECATION NOTICE
I don't want to maintain this module anymore since I just use minimist, the argument parsing engine, directly instead nowadays.
See yargs for the modern, pirate-themed successor to optimist.
You should also consider nomnom.
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).
examples
With Optimist, the options are just a hash! No optstrings attached.
xup.js:

But wait! There's more! You can do short options:
short.js:
And booleans, both long and short (and grouped):
bool.js:
And non-hypenated options too! Just use argv._!
nonopt.js:
Plus, Optimist comes with .usage() and .demand()!
divide.js:
EVEN MORE HOLY COW
default_singles.js:
default_hash.js:
And if you really want to get all descriptive about it...
boolean_single.js
boolean_double.js
Optimist is here to help...
You can describe parameters for help messages and set aliases. Optimist figures out how to format a handy help string automatically.
line_count.js
methods
By itself,
will use process.argv array to construct the argv object.
You can pass in the process.argv yourself:
or use .parse() to do the same thing:
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:
is the same as
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._.
negate fields
If you want to explicity set a field to false instead of just leaving it
undefined or to override a default you can do --no-key.
numbers
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.
dot notation
When you use dots (.s) in argument names, an implicit object path is assumed.
This lets you organize arguments into nested objects.
short numbers
Short numeric head -n5 style argument work too:
installation
With npm, just do: npm install optimist
or clone this project on github:
To run the tests with expresso, just do:
inspired By
This module is loosely inspired by Perl's Getopt::Casual.

