UNPKG

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