1 | Optimist
|
2 | ========
|
3 |
|
4 | Optimist is a node.js library for option parsing for people who hate option
|
5 | parsing. More specifically, this module is for people who like all the --bells
|
6 | and -whistlz of program usage but think optstrings are a waste of time.
|
7 |
|
8 | With optimist, option parsing doesn't have to suck (as much).
|
9 |
|
10 | With Optimist, the options are just a hash! No optstrings attached.
|
11 | -------------------------------------------------------------------
|
12 |
|
13 | xup.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 |
|
33 | But wait! There's more! You can do short options:
|
34 | -------------------------------------------------
|
35 |
|
36 | short.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 |
|
47 | And booleans, both long and short (and grouped):
|
48 | ----------------------------------
|
49 |
|
50 | bool.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 |
|
73 | And non-hypenated options too! Just use `argv._`!
|
74 | -------------------------------------------------
|
75 |
|
76 | nonopt.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 |
|
93 | Plus, Optimist comes with .usage() and .demand()!
|
94 | -------------------------------------------------
|
95 |
|
96 | divide.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 |
|
114 | EVEN MORE HOLY COW
|
115 | ------------------
|
116 |
|
117 | default_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 |
|
131 | default_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 |
|
144 | And if you really want to get all descriptive about it...
|
145 | ---------------------------------------------------------
|
146 |
|
147 | boolean_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 |
|
160 | boolean_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 |
|
175 | Notes
|
176 | =====
|
177 |
|
178 | Every argument that looks like a number (`!isNaN(Number(arg))`) is converted to
|
179 | one. This way you can just `net.createConnection(argv.port)` and you can add
|
180 | numbers out of `argv` with `+` without having that mean concatenation,
|
181 | which is super frustrating.
|
182 |
|
183 | Installation
|
184 | ============
|
185 |
|
186 | With [npm](http://github.com/isaacs/npm), just do:
|
187 | npm install optimist
|
188 |
|
189 | or clone this project on github:
|
190 |
|
191 | git clone http://github.com/substack/node-optimist.git
|
192 |
|
193 | To run the tests with [expresso](http://github.com/visionmedia/expresso),
|
194 | just do:
|
195 |
|
196 | expresso
|
197 |
|
198 | Inspired By
|
199 | ===========
|
200 |
|
201 | This module is loosely inspired by Perl's
|
202 | [Getopt::Casual](http://search.cpan.org/~photo/Getopt-Casual-0.13.1/Casual.pm).
|