1 | # yargs-parser
|
---|
2 |
|
---|
3 | [![Build Status](https://travis-ci.org/yargs/yargs-parser.svg)](https://travis-ci.org/yargs/yargs-parser)
|
---|
4 | [![Coverage Status](https://coveralls.io/repos/yargs/yargs-parser/badge.svg?branch=)](https://coveralls.io/r/yargs/yargs-parser?branch=master)
|
---|
5 | [![NPM version](https://img.shields.io/npm/v/yargs-parser.svg)](https://www.npmjs.com/package/yargs-parser)
|
---|
6 | [![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
|
---|
7 |
|
---|
8 |
|
---|
9 | The mighty option parser used by [yargs](https://github.com/yargs/yargs).
|
---|
10 |
|
---|
11 | visit the [yargs website](http://yargs.js.org/) for more examples, and thorough usage instructions.
|
---|
12 |
|
---|
13 | <img width="250" src="https://raw.githubusercontent.com/yargs/yargs-parser/master/yargs-logo.png">
|
---|
14 |
|
---|
15 | ## Example
|
---|
16 |
|
---|
17 | ```sh
|
---|
18 | npm i yargs-parser --save
|
---|
19 | ```
|
---|
20 |
|
---|
21 | ```js
|
---|
22 | var argv = require('yargs-parser')(process.argv.slice(2))
|
---|
23 | console.log(argv)
|
---|
24 | ```
|
---|
25 |
|
---|
26 | ```sh
|
---|
27 | node example.js --foo=33 --bar hello
|
---|
28 | { _: [], foo: 33, bar: 'hello' }
|
---|
29 | ```
|
---|
30 |
|
---|
31 | _or parse a string!_
|
---|
32 |
|
---|
33 | ```js
|
---|
34 | var argv = require('./')('--foo=99 --bar=33')
|
---|
35 | console.log(argv)
|
---|
36 | ```
|
---|
37 |
|
---|
38 | ```sh
|
---|
39 | { _: [], foo: 99, bar: 33 }
|
---|
40 | ```
|
---|
41 |
|
---|
42 | Convert an array of mixed types before passing to `yargs-parser`:
|
---|
43 |
|
---|
44 | ```js
|
---|
45 | var parse = require('yargs-parser')
|
---|
46 | parse(['-f', 11, '--zoom', 55].join(' ')) // <-- array to string
|
---|
47 | parse(['-f', 11, '--zoom', 55].map(String)) // <-- array of strings
|
---|
48 | ```
|
---|
49 |
|
---|
50 | ## API
|
---|
51 |
|
---|
52 | ### require('yargs-parser')(args, opts={})
|
---|
53 |
|
---|
54 | Parses command line arguments returning a simple mapping of keys and values.
|
---|
55 |
|
---|
56 | **expects:**
|
---|
57 |
|
---|
58 | * `args`: a string or array of strings representing the options to parse.
|
---|
59 | * `opts`: provide a set of hints indicating how `args` should be parsed:
|
---|
60 | * `opts.alias`: an object representing the set of aliases for a key: `{alias: {foo: ['f']}}`.
|
---|
61 | * `opts.array`: indicate that keys should be parsed as an array: `{array: ['foo', 'bar']}`.<br>
|
---|
62 | Indicate that keys should be parsed as an array and coerced to booleans / numbers:<br>
|
---|
63 | `{array: [{ key: 'foo', boolean: true }, {key: 'bar', number: true}]}`.
|
---|
64 | * `opts.boolean`: arguments should be parsed as booleans: `{boolean: ['x', 'y']}`.
|
---|
65 | * `opts.coerce`: provide a custom synchronous function that returns a coerced value from the argument provided
|
---|
66 | (or throws an error). For arrays the function is called only once for the entire array:<br>
|
---|
67 | `{coerce: {foo: function (arg) {return modifiedArg}}}`.
|
---|
68 | * `opts.config`: indicate a key that represents a path to a configuration file (this file will be loaded and parsed).
|
---|
69 | * `opts.configObjects`: configuration objects to parse, their properties will be set as arguments:<br>
|
---|
70 | `{configObjects: [{'x': 5, 'y': 33}, {'z': 44}]}`.
|
---|
71 | * `opts.configuration`: provide configuration options to the yargs-parser (see: [configuration](#configuration)).
|
---|
72 | * `opts.count`: indicate a key that should be used as a counter, e.g., `-vvv` = `{v: 3}`.
|
---|
73 | * `opts.default`: provide default values for keys: `{default: {x: 33, y: 'hello world!'}}`.
|
---|
74 | * `opts.envPrefix`: environment variables (`process.env`) with the prefix provided should be parsed.
|
---|
75 | * `opts.narg`: specify that a key requires `n` arguments: `{narg: {x: 2}}`.
|
---|
76 | * `opts.normalize`: `path.normalize()` will be applied to values set to this key.
|
---|
77 | * `opts.number`: keys should be treated as numbers.
|
---|
78 | * `opts.string`: keys should be treated as strings (even if they resemble a number `-x 33`).
|
---|
79 |
|
---|
80 | **returns:**
|
---|
81 |
|
---|
82 | * `obj`: an object representing the parsed value of `args`
|
---|
83 | * `key/value`: key value pairs for each argument and their aliases.
|
---|
84 | * `_`: an array representing the positional arguments.
|
---|
85 | * [optional] `--`: an array with arguments after the end-of-options flag `--`.
|
---|
86 |
|
---|
87 | ### require('yargs-parser').detailed(args, opts={})
|
---|
88 |
|
---|
89 | Parses a command line string, returning detailed information required by the
|
---|
90 | yargs engine.
|
---|
91 |
|
---|
92 | **expects:**
|
---|
93 |
|
---|
94 | * `args`: a string or array of strings representing options to parse.
|
---|
95 | * `opts`: provide a set of hints indicating how `args`, inputs are identical to `require('yargs-parser')(args, opts={})`.
|
---|
96 |
|
---|
97 | **returns:**
|
---|
98 |
|
---|
99 | * `argv`: an object representing the parsed value of `args`
|
---|
100 | * `key/value`: key value pairs for each argument and their aliases.
|
---|
101 | * `_`: an array representing the positional arguments.
|
---|
102 | * `error`: populated with an error object if an exception occurred during parsing.
|
---|
103 | * `aliases`: the inferred list of aliases built by combining lists in `opts.alias`.
|
---|
104 | * `newAliases`: any new aliases added via camel-case expansion.
|
---|
105 | * `configuration`: the configuration loaded from the `yargs` stanza in package.json.
|
---|
106 |
|
---|
107 | <a name="configuration"></a>
|
---|
108 |
|
---|
109 | ### Configuration
|
---|
110 |
|
---|
111 | The yargs-parser applies several automated transformations on the keys provided
|
---|
112 | in `args`. These features can be turned on and off using the `configuration` field
|
---|
113 | of `opts`.
|
---|
114 |
|
---|
115 | ```js
|
---|
116 | var parsed = parser(['--no-dice'], {
|
---|
117 | configuration: {
|
---|
118 | 'boolean-negation': false
|
---|
119 | }
|
---|
120 | })
|
---|
121 | ```
|
---|
122 |
|
---|
123 | ### short option groups
|
---|
124 |
|
---|
125 | * default: `true`.
|
---|
126 | * key: `short-option-groups`.
|
---|
127 |
|
---|
128 | Should a group of short-options be treated as boolean flags?
|
---|
129 |
|
---|
130 | ```sh
|
---|
131 | node example.js -abc
|
---|
132 | { _: [], a: true, b: true, c: true }
|
---|
133 | ```
|
---|
134 |
|
---|
135 | _if disabled:_
|
---|
136 |
|
---|
137 | ```sh
|
---|
138 | node example.js -abc
|
---|
139 | { _: [], abc: true }
|
---|
140 | ```
|
---|
141 |
|
---|
142 | ### camel-case expansion
|
---|
143 |
|
---|
144 | * default: `true`.
|
---|
145 | * key: `camel-case-expansion`.
|
---|
146 |
|
---|
147 | Should hyphenated arguments be expanded into camel-case aliases?
|
---|
148 |
|
---|
149 | ```sh
|
---|
150 | node example.js --foo-bar
|
---|
151 | { _: [], 'foo-bar': true, fooBar: true }
|
---|
152 | ```
|
---|
153 |
|
---|
154 | _if disabled:_
|
---|
155 |
|
---|
156 | ```sh
|
---|
157 | node example.js --foo-bar
|
---|
158 | { _: [], 'foo-bar': true }
|
---|
159 | ```
|
---|
160 |
|
---|
161 | ### dot-notation
|
---|
162 |
|
---|
163 | * default: `true`
|
---|
164 | * key: `dot-notation`
|
---|
165 |
|
---|
166 | Should keys that contain `.` be treated as objects?
|
---|
167 |
|
---|
168 | ```sh
|
---|
169 | node example.js --foo.bar
|
---|
170 | { _: [], foo: { bar: true } }
|
---|
171 | ```
|
---|
172 |
|
---|
173 | _if disabled:_
|
---|
174 |
|
---|
175 | ```sh
|
---|
176 | node example.js --foo.bar
|
---|
177 | { _: [], "foo.bar": true }
|
---|
178 | ```
|
---|
179 |
|
---|
180 | ### parse numbers
|
---|
181 |
|
---|
182 | * default: `true`
|
---|
183 | * key: `parse-numbers`
|
---|
184 |
|
---|
185 | Should keys that look like numbers be treated as such?
|
---|
186 |
|
---|
187 | ```sh
|
---|
188 | node example.js --foo=99.3
|
---|
189 | { _: [], foo: 99.3 }
|
---|
190 | ```
|
---|
191 |
|
---|
192 | _if disabled:_
|
---|
193 |
|
---|
194 | ```sh
|
---|
195 | node example.js --foo=99.3
|
---|
196 | { _: [], foo: "99.3" }
|
---|
197 | ```
|
---|
198 |
|
---|
199 | ### boolean negation
|
---|
200 |
|
---|
201 | * default: `true`
|
---|
202 | * key: `boolean-negation`
|
---|
203 |
|
---|
204 | Should variables prefixed with `--no` be treated as negations?
|
---|
205 |
|
---|
206 | ```sh
|
---|
207 | node example.js --no-foo
|
---|
208 | { _: [], foo: false }
|
---|
209 | ```
|
---|
210 |
|
---|
211 | _if disabled:_
|
---|
212 |
|
---|
213 | ```sh
|
---|
214 | node example.js --no-foo
|
---|
215 | { _: [], "no-foo": true }
|
---|
216 | ```
|
---|
217 |
|
---|
218 | ### combine arrays
|
---|
219 |
|
---|
220 | * default: `false`
|
---|
221 | * key: `combine-arrays`
|
---|
222 |
|
---|
223 | Should arrays be combined when provided by both command line arguments and
|
---|
224 | a configuration file.
|
---|
225 |
|
---|
226 | ### duplicate arguments array
|
---|
227 |
|
---|
228 | * default: `true`
|
---|
229 | * key: `duplicate-arguments-array`
|
---|
230 |
|
---|
231 | Should arguments be coerced into an array when duplicated:
|
---|
232 |
|
---|
233 | ```sh
|
---|
234 | node example.js -x 1 -x 2
|
---|
235 | { _: [], x: [1, 2] }
|
---|
236 | ```
|
---|
237 |
|
---|
238 | _if disabled:_
|
---|
239 |
|
---|
240 | ```sh
|
---|
241 | node example.js -x 1 -x 2
|
---|
242 | { _: [], x: 2 }
|
---|
243 | ```
|
---|
244 |
|
---|
245 | ### flatten duplicate arrays
|
---|
246 |
|
---|
247 | * default: `true`
|
---|
248 | * key: `flatten-duplicate-arrays`
|
---|
249 |
|
---|
250 | Should array arguments be coerced into a single array when duplicated:
|
---|
251 |
|
---|
252 | ```sh
|
---|
253 | node example.js -x 1 2 -x 3 4
|
---|
254 | { _: [], x: [1, 2, 3, 4] }
|
---|
255 | ```
|
---|
256 |
|
---|
257 | _if disabled:_
|
---|
258 |
|
---|
259 | ```sh
|
---|
260 | node example.js -x 1 2 -x 3 4
|
---|
261 | { _: [], x: [[1, 2], [3, 4]] }
|
---|
262 | ```
|
---|
263 |
|
---|
264 | ### negation prefix
|
---|
265 |
|
---|
266 | * default: `no-`
|
---|
267 | * key: `negation-prefix`
|
---|
268 |
|
---|
269 | The prefix to use for negated boolean variables.
|
---|
270 |
|
---|
271 | ```sh
|
---|
272 | node example.js --no-foo
|
---|
273 | { _: [], foo: false }
|
---|
274 | ```
|
---|
275 |
|
---|
276 | _if set to `quux`:_
|
---|
277 |
|
---|
278 | ```sh
|
---|
279 | node example.js --quuxfoo
|
---|
280 | { _: [], foo: false }
|
---|
281 | ```
|
---|
282 |
|
---|
283 | ### populate --
|
---|
284 |
|
---|
285 | * default: `false`.
|
---|
286 | * key: `populate--`
|
---|
287 |
|
---|
288 | Should unparsed flags be stored in `--` or `_`.
|
---|
289 |
|
---|
290 | _If disabled:_
|
---|
291 |
|
---|
292 | ```sh
|
---|
293 | node example.js a -b -- x y
|
---|
294 | { _: [ 'a', 'x', 'y' ], b: true }
|
---|
295 | ```
|
---|
296 |
|
---|
297 | _If enabled:_
|
---|
298 |
|
---|
299 | ```sh
|
---|
300 | node example.js a -b -- x y
|
---|
301 | { _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
|
---|
302 | ```
|
---|
303 |
|
---|
304 | ### set placeholder key
|
---|
305 |
|
---|
306 | * default: `false`.
|
---|
307 | * key: `set-placeholder-key`.
|
---|
308 |
|
---|
309 | Should a placeholder be added for keys not set via the corresponding CLI argument?
|
---|
310 |
|
---|
311 | _If disabled:_
|
---|
312 |
|
---|
313 | ```sh
|
---|
314 | node example.js -a 1 -c 2
|
---|
315 | { _: [], a: 1, c: 2 }
|
---|
316 | ```
|
---|
317 |
|
---|
318 | _If enabled:_
|
---|
319 |
|
---|
320 | ```sh
|
---|
321 | node example.js -a 1 -c 2
|
---|
322 | { _: [], a: 1, b: undefined, c: 2 }
|
---|
323 | ```
|
---|
324 |
|
---|
325 | ### halt at non-option
|
---|
326 |
|
---|
327 | * default: `false`.
|
---|
328 | * key: `halt-at-non-option`.
|
---|
329 |
|
---|
330 | Should parsing stop at the first positional argument? This is similar to how e.g. `ssh` parses its command line.
|
---|
331 |
|
---|
332 | _If disabled:_
|
---|
333 |
|
---|
334 | ```sh
|
---|
335 | node example.js -a run b -x y
|
---|
336 | { _: [ 'b' ], a: 'run', x: 'y' }
|
---|
337 | ```
|
---|
338 |
|
---|
339 | _If enabled:_
|
---|
340 |
|
---|
341 | ```sh
|
---|
342 | node example.js -a run b -x y
|
---|
343 | { _: [ 'b', '-x', 'y' ], a: 'run' }
|
---|
344 | ```
|
---|
345 |
|
---|
346 | ### strip aliased
|
---|
347 |
|
---|
348 | * default: `false`
|
---|
349 | * key: `strip-aliased`
|
---|
350 |
|
---|
351 | Should aliases be removed before returning results?
|
---|
352 |
|
---|
353 | _If disabled:_
|
---|
354 |
|
---|
355 | ```sh
|
---|
356 | node example.js --test-field 1
|
---|
357 | { _: [], 'test-field': 1, testField: 1, 'test-alias': 1, testAlias: 1 }
|
---|
358 | ```
|
---|
359 |
|
---|
360 | _If enabled:_
|
---|
361 |
|
---|
362 | ```sh
|
---|
363 | node example.js --test-field 1
|
---|
364 | { _: [], 'test-field': 1, testField: 1 }
|
---|
365 | ```
|
---|
366 |
|
---|
367 | ### strip dashed
|
---|
368 |
|
---|
369 | * default: `false`
|
---|
370 | * key: `strip-dashed`
|
---|
371 |
|
---|
372 | Should dashed keys be removed before returning results? This option has no effect if
|
---|
373 | `camel-case-exansion` is disabled.
|
---|
374 |
|
---|
375 | _If disabled:_
|
---|
376 |
|
---|
377 | ```sh
|
---|
378 | node example.js --test-field 1
|
---|
379 | { _: [], 'test-field': 1, testField: 1 }
|
---|
380 | ```
|
---|
381 |
|
---|
382 | _If enabled:_
|
---|
383 |
|
---|
384 | ```sh
|
---|
385 | node example.js --test-field 1
|
---|
386 | { _: [], testField: 1 }
|
---|
387 | ```
|
---|
388 |
|
---|
389 | ## Special Thanks
|
---|
390 |
|
---|
391 | The yargs project evolves from optimist and minimist. It owes its
|
---|
392 | existence to a lot of James Halliday's hard work. Thanks [substack](https://github.com/substack) **beep** **boop** \o/
|
---|
393 |
|
---|
394 | ## License
|
---|
395 |
|
---|
396 | ISC
|
---|