UNPKG

4.31 kBMarkdownView Raw
1Optimist
2========
3
4Optimist is a node.js library for option parsing for people who hate option
5parsing. More specifically, this module is for people who like all the --bells
6and -whistlz of program usage but think optstrings are a waste of time.
7
8With optimist, option parsing doesn't have to suck (as much).
9
10With Optimist, the options are just a hash! No optstrings attached.
11-------------------------------------------------------------------
12
13xup.js:
14
15 #!/usr/bin/env node
16 var argv = require('optimist').argv;
17
18 if (argv.rif - 5 * argv.xup > 7.138) {
19 console.log('Buy more riffiwobbles');
20 }
21 else {
22 console.log('Sell the xupptumblers');
23 }
24
25***
26
27 $ ./xup.js --rif=55 --xup=9.52
28 Buy more riffiwobbles
29
30 $ ./xup.js --rif 12 --xup 8.1
31 Sell the xupptumblers
32
33But wait! There's more! You can do short options:
34-------------------------------------------------
35
36short.js:
37
38 #!/usr/bin/env node
39 var argv = require('optimist').argv;
40 console.log('(%d,%d)', argv.x, argv.y);
41
42***
43
44 $ ./short.js -x 10 -y 21
45 (10,21)
46
47And booleans, both long and short (and grouped):
48----------------------------------
49
50bool.js:
51
52 #!/usr/bin/env node
53 var sys = require('sys');
54 var argv = require('optimist').argv;
55
56 if (argv.s) {
57 sys.print(argv.fr ? 'Le chat dit: ' : 'The cat says: ');
58 }
59 console.log(
60 (argv.fr ? 'miaou' : 'meow') + (argv.p ? '.' : '')
61 );
62
63***
64 $ ./bool.js -s
65 The cat says: meow
66
67 $ ./bool.js -sp
68 The cat says: meow.
69
70 $ ./bool.js -sp --fr
71 Le chat dit: miaou.
72
73And non-hypenated options too! Just use `argv._`!
74-------------------------------------------------
75
76nonopt.js:
77
78 #!/usr/bin/env node
79 var argv = require('optimist').argv;
80 console.log('(%d,%d)', argv.x, argv.y);
81 console.log(argv._);
82
83***
84
85 $ ./nonopt.js -x 6.82 -y 3.35 moo
86 (6.82,3.35)
87 [ 'moo' ]
88
89 $ ./nonopt.js foo -x 0.54 bar -y 1.12 baz
90 (0.54,1.12)
91 [ 'foo', 'bar', 'baz' ]
92
93Plus, Optimist comes with .usage() and .demand()!
94-------------------------------------------------
95
96divide.js:
97 #!/usr/bin/env node
98 var argv = require('optimist')
99 .usage('Usage: $0 -x [num] -y [num]')
100 .demand(['x','y'])
101 .argv;
102
103 console.log(argv.x / argv.y);
104
105***
106
107 $ ./divide.js -x 55 -y 11
108 5
109
110 $ ./divide.js -x 4.91 -z 2.51
111 Usage: ./divide.js -x [num] -y [num]
112 Missing arguments: y
113
114EVEN MORE HOLY COW
115------------------
116
117default_singles.js:
118 #!/usr/bin/env node
119 var argv = require('optimist')
120 .default('x', 10)
121 .default('y', 10)
122 .argv
123 ;
124 console.log(argv.x + argv.y);
125
126***
127
128 $ ./default_singles.js -x 5
129 15
130
131default_hash.js:
132 #!/usr/bin/env node
133 var argv = require('optimist')
134 .default({ x : 10, y : 10 })
135 .argv
136 ;
137 console.log(argv.x + argv.y);
138
139***
140
141 $ ./default_hash.js -y 7
142 17
143
144And if you really want to get all descriptive about it...
145---------------------------------------------------------
146
147boolean_single.js
148 #!/usr/bin/env node
149 var argv = require('optimist')
150 .boolean('v')
151 .argv
152 ;
153 console.dir(argv);
154
155***
156 $ ./boolean_single.js -v foo bar baz
157 true
158 [ 'bar', 'baz', 'foo' ]
159
160boolean_double.js
161
162 #!/usr/bin/env node
163 var argv = require('optimist')
164 .boolean(['x','y','z'])
165 .argv
166 ;
167 console.dir([ argv.x, argv.y, argv.z ]);
168 console.dir(argv._);
169
170***
171 $ ./boolean_double.js -x -z one two three
172 [ true, false, true ]
173 [ 'one', 'two', 'three' ]
174
175Notes
176=====
177
178Every argument that looks like a number (`!isNaN(Number(arg))`) is converted to
179one. This way you can just `net.createConnection(argv.port)` and you can add
180numbers out of `argv` with `+` without having that mean concatenation,
181which is super frustrating.
182
183Installation
184============
185
186With [npm](http://github.com/isaacs/npm), just do:
187 npm install optimist
188
189or clone this project on github:
190
191 git clone http://github.com/substack/node-optimist.git
192
193To run the tests with [expresso](http://github.com/visionmedia/expresso),
194just do:
195
196 expresso
197
198Inspired By
199===========
200
201This module is loosely inspired by Perl's
202[Getopt::Casual](http://search.cpan.org/~photo/Getopt-Casual-0.13.1/Casual.pm).