UNPKG

5.37 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
65 $ ./bool.js -s
66 The cat says: meow
67
68 $ ./bool.js -sp
69 The cat says: meow.
70
71 $ ./bool.js -sp --fr
72 Le chat dit: miaou.
73
74And non-hypenated options too! Just use `argv._`!
75-------------------------------------------------
76
77nonopt.js:
78
79 #!/usr/bin/env node
80 var argv = require('optimist').argv;
81 console.log('(%d,%d)', argv.x, argv.y);
82 console.log(argv._);
83
84***
85
86 $ ./nonopt.js -x 6.82 -y 3.35 moo
87 (6.82,3.35)
88 [ 'moo' ]
89
90 $ ./nonopt.js foo -x 0.54 bar -y 1.12 baz
91 (0.54,1.12)
92 [ 'foo', 'bar', 'baz' ]
93
94Plus, Optimist comes with .usage() and .demand()!
95-------------------------------------------------
96
97divide.js:
98
99 #!/usr/bin/env node
100 var argv = require('optimist')
101 .usage('Usage: $0 -x [num] -y [num]')
102 .demand(['x','y'])
103 .argv;
104
105 console.log(argv.x / argv.y);
106
107***
108
109 $ ./divide.js -x 55 -y 11
110 5
111
112 $ ./divide.js -x 4.91 -z 2.51
113 Usage: node ./examples/divide.js -x [num] -y [num]
114
115 -x [required]
116
117 -y [required]
118
119 Missing required arguments: y
120
121EVEN MORE HOLY COW
122------------------
123
124default_singles.js:
125
126 #!/usr/bin/env node
127 var argv = require('optimist')
128 .default('x', 10)
129 .default('y', 10)
130 .argv
131 ;
132 console.log(argv.x + argv.y);
133
134***
135
136 $ ./default_singles.js -x 5
137 15
138
139default_hash.js:
140
141 #!/usr/bin/env node
142 var argv = require('optimist')
143 .default({ x : 10, y : 10 })
144 .argv
145 ;
146 console.log(argv.x + argv.y);
147
148***
149
150 $ ./default_hash.js -y 7
151 17
152
153And if you really want to get all descriptive about it...
154---------------------------------------------------------
155
156boolean_single.js
157
158 #!/usr/bin/env node
159 var argv = require('optimist')
160 .boolean('v')
161 .argv
162 ;
163 console.dir(argv);
164
165***
166
167 $ ./boolean_single.js -v foo bar baz
168 true
169 [ 'bar', 'baz', 'foo' ]
170
171boolean_double.js
172
173 #!/usr/bin/env node
174 var argv = require('optimist')
175 .boolean(['x','y','z'])
176 .argv
177 ;
178 console.dir([ argv.x, argv.y, argv.z ]);
179 console.dir(argv._);
180
181***
182
183 $ ./boolean_double.js -x -z one two three
184 [ true, false, true ]
185 [ 'one', 'two', 'three' ]
186
187Optimist is here to help...
188---------------------------
189
190You can describe parameters for help messages and set aliases. Optimist figures
191out how to format a handy help string automatically.
192
193line_count.js
194
195 #!/usr/bin/env node
196 var argv = require('optimist')
197 .usage('Count the lines in a file.\nUsage: $0')
198 .demand('f')
199 .alias('f', 'file')
200 .describe('f', 'Load a file')
201 .argv
202 ;
203
204 var fs = require('fs');
205 var s = fs.createReadStream(argv.file);
206
207 var lines = 0;
208 s.on('data', function (buf) {
209 lines += buf.toString().match(/\n/g).length;
210 });
211
212 s.on('end', function () {
213 console.log(lines);
214 });
215
216***
217
218 $ node line_count.js
219 Count the lines in a file.
220 Usage: node ./line_count.js
221
222 -f, --file [required]
223 Load a file
224
225 Missing required arguments: f
226
227 $ node line_count.js --file line_count.js
228 20
229
230 $ node line_count.js -f line_count.js
231 20
232
233notes
234=====
235
236Every argument that looks like a number (`!isNaN(Number(arg))`) is converted to
237one. This way you can just `net.createConnection(argv.port)` and you can add
238numbers out of `argv` with `+` without having that mean concatenation,
239which is super frustrating.
240
241installation
242============
243
244With [npm](http://github.com/isaacs/npm), just do:
245 npm install optimist
246
247or clone this project on github:
248
249 git clone http://github.com/substack/node-optimist.git
250
251To run the tests with [expresso](http://github.com/visionmedia/expresso),
252just do:
253
254 expresso
255
256inspired By
257===========
258
259This module is loosely inspired by Perl's
260[Getopt::Casual](http://search.cpan.org/~photo/Getopt-Casual-0.13.1/Casual.pm).