Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
The optimist npm package is a command-line option parser for Node.js. It allows developers to parse arguments passed to their Node.js applications and use them as options within the application. It provides a simple and fluent API to define and access command-line arguments.
Option Parsing
This feature allows the parsing of command-line arguments. In the code sample, optimist is used to parse the 'name' argument from the command line and print a greeting message.
const argv = require('optimist').argv;
console.log('Hello, ' + argv.name + '!');
Default Values
This feature allows setting default values for command-line arguments. In the code sample, if the 'name' argument is not provided, it defaults to 'World'.
const argv = require('optimist').default('name', 'World').argv;
console.log('Hello, ' + argv.name + '!');
Demand Options
This feature makes certain command-line arguments mandatory. In the code sample, the 'name' argument is required, and the script will not run without it.
const argv = require('optimist').demand(['name']).argv;
console.log('Hello, ' + argv.name + '!');
Usage Messages
This feature allows developers to provide usage messages for their command-line applications. In the code sample, a usage message is defined to guide the user on how to run the script.
const argv = require('optimist').usage('Usage: $0 --name [name]').argv;
console.log('Hello, ' + argv.name + '!');
Boolean Flags
This feature allows the definition of boolean flags. In the code sample, a 'verbose' flag is defined, which can be turned on or off.
const argv = require('optimist').boolean('verbose').argv;
console.log('Verbose mode is ' + (argv.verbose ? 'on' : 'off'));
Yargs is a modern and feature-rich command-line argument parser that has succeeded optimist. It offers similar functionalities but with additional features like command chaining, advanced parsing, and automatic help generation.
Commander is another popular npm package for command-line interfaces. It provides a high-level API for defining commands, options, and subcommands, making it more suitable for complex CLI applications compared to optimist.
Minimist is a minimalistic command-line argument parser. It is more lightweight than optimist and focuses on simplicity and performance, with fewer features and a smaller footprint.
Argparse is a node.js version of the Python argument parsing library. It offers a rich set of features for argument parsing, including subcommands and nested parsers, making it a powerful alternative to 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).
xup.js:
#!/usr/bin/env node
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
short.js:
#!/usr/bin/env node
var argv = require('optimist').argv;
console.log('(%d,%d)', argv.x, argv.y);
$ ./short.js -x 10 -y 21
(10,21)
bool.js:
#!/usr/bin/env node
var sys = require('sys');
var argv = require('optimist').argv;
if (argv.s) {
sys.print(argv.fr ? 'Le chat dit: ' : 'The cat says: ');
}
console.log(
(argv.fr ? 'miaou' : 'meow') + (argv.p ? '.' : '')
);
$ ./bool.js -s
The cat says: meow
$ ./bool.js -sp
The cat says: meow.
$ ./bool.js -sp --fr
Le chat dit: miaou.
argv._
!nonopt.js:
#!/usr/bin/env node
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' ]
divide.js: #!/usr/bin/env node var argv = require('optimist') .usage('Usage: $0 -x [num] -y [num]') .demand(['x','y']) .argv;
console.log(argv.x / argv.y);
$ ./divide.js -x 55 -y 11
5
$ ./divide.js -x 4.91 -z 2.51
Usage: ./divide.js -x [num] -y [num]
Missing arguments: y
default_singles.js: #!/usr/bin/env node var argv = require('optimist') .default('x', 10) .default('y', 10) .argv ; console.log(argv.x + argv.y);
$ ./default_singles.js -x 5
15
default_hash.js: #!/usr/bin/env node var argv = require('optimist') .default({ x : 10, y : 10 }) .argv ; console.log(argv.x + argv.y);
$ ./default_hash.js -y 7
17
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.
With npm, just do: npm install optimist
or clone this project on github:
git clone http://github.com/substack/node-optimist.git
To run the tests with expresso, just do:
expresso
This module is loosely inspired by Perl's Getopt::Casual.
FAQs
Light-weight option parsing with an argv hash. No optstrings attached.
The npm package optimist receives a total of 1,448,830 weekly downloads. As such, optimist popularity was classified as popular.
We found that optimist demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.