Giter VIP home page Giter VIP logo

node-optimist's Introduction

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.

But all hope is not lost, dear reader, because there is Optimist, proving that option parsing doesn't have to suck (as much).

With Optimist, the options are just a hash! No optstrings attached.

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

But wait! There's more! You can do short options:

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)

And booleans, both long and short (and grouped):

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.

And non-hypenated options too! Just use 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' ]

Plus, Optimist comes with .usage() and .demand()!

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

Installation

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

Inspired By

This module is loosely inspired by Perl's Getopt::Casual.

node-optimist's People

Contributors

marshall avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.