Projekt

Obecné

Profil

Stáhnout (8.18 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
# 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']}`.
62
     Indicate that keys should be parsed as an array and coerced to booleans / numbers:
63
     `{array: [{ key: 'foo', boolean: true }, {key: 'bar', number: true}]}`.
64
  * `opts.boolean`: arguments should be parsed as booleans: `{boolean: ['x', 'y']}`.
65
  * `opts.config`: indicate a key that represents a path to a configuration file (this file will be loaded and parsed).
66
  * `opts.coerce`: provide a custom synchronous function that returns a coerced value from the argument provided
67
    (or throws an error), e.g. `{coerce: {foo: function (arg) {return modifiedArg}}}`.
68
  * `opts.count`: indicate a key that should be used as a counter, e.g., `-vvv` = `{v: 3}`.
69
  * `opts.default`: provide default values for keys: `{default: {x: 33, y: 'hello world!'}}`.
70
  * `opts.envPrefix`: environment variables (`process.env`) with the prefix provided should be parsed.
71
  * `opts.narg`: specify that a key requires `n` arguments: `{narg: {x: 2}}`.
72
  * `opts.normalize`: `path.normalize()` will be applied to values set to this key.
73
  * `opts.string`: keys should be treated as strings (even if they resemble a number `-x 33`).
74
  * `opts.configuration`: provide configuration options to the yargs-parser (see: [configuration](#configuration)).
75
  * `opts.number`: keys should be treated as numbers.
76
  * `opts['--']`: arguments after the end-of-options flag `--` will be set to the `argv.['--']` array instead of being set to the `argv._` array.
77
78
**returns:**
79
80
* `obj`: an object representing the parsed value of `args`
81
  * `key/value`: key value pairs for each argument and their aliases.
82
  * `_`: an array representing the positional arguments.
83
  * [optional] `--`:  an array with arguments after the end-of-options flag `--`.
84
85
### require('yargs-parser').detailed(args, opts={})
86
87
Parses a command line string, returning detailed information required by the
88
yargs engine.
89
90
**expects:**
91
92
* `args`: a string or array of strings representing options to parse.
93
* `opts`: provide a set of hints indicating how `args`, inputs are identical to `require('yargs-parser')(args, opts={})`.
94
95
**returns:**
96
97
* `argv`: an object representing the parsed value of `args`
98
  * `key/value`: key value pairs for each argument and their aliases.
99
  * `_`: an array representing the positional arguments.
100
* `error`: populated with an error object if an exception occurred during parsing.
101
* `aliases`: the inferred list of aliases built by combining lists in `opts.alias`.
102
* `newAliases`: any new aliases added via camel-case expansion.
103
* `configuration`: the configuration loaded from the `yargs` stanza in package.json.
104
105
<a name="configuration"></a>
106
107
### Configuration
108
109
The yargs-parser applies several automated transformations on the keys provided
110
in `args`. These features can be turned on and off using the `configuration` field
111
of `opts`.
112
113
```js
114
var parsed = parser(['--no-dice'], {
115
  configuration: {
116
    'boolean-negation': false
117
  }
118
})
119
```
120
121
### short option groups
122
123
* default: `true`.
124
* key: `short-option-groups`.
125
126
Should a group of short-options be treated as boolean flags?
127
128
```sh
129
node example.js -abc
130
{ _: [], a: true, b: true, c: true }
131
```
132
133
_if disabled:_
134
135
```sh
136
node example.js -abc
137
{ _: [], abc: true }
138
```
139
140
### camel-case expansion
141
142
* default: `true`.
143
* key: `camel-case-expansion`.
144
145
Should hyphenated arguments be expanded into camel-case aliases?
146
147
```sh
148
node example.js --foo-bar
149
{ _: [], 'foo-bar': true, fooBar: true }
150
```
151
152
_if disabled:_
153
154
```sh
155
node example.js --foo-bar
156
{ _: [], 'foo-bar': true }
157
```
158
159
### dot-notation
160
161
* default: `true`
162
* key: `dot-notation`
163
164
Should keys that contain `.` be treated as objects?
165
166
```sh
167
node example.js --foo.bar
168
{ _: [], foo: { bar: true } }
169
```
170
171
_if disabled:_
172
173
```sh
174
node example.js --foo.bar
175
{ _: [], "foo.bar": true }
176
```
177
178
### parse numbers
179
180
* default: `true`
181
* key: `parse-numbers`
182
183
Should keys that look like numbers be treated as such?
184
185
```sh
186
node example.js --foo=99.3
187
{ _: [], foo: 99.3 }
188
```
189
190
_if disabled:_
191
192
```sh
193
node example.js --foo=99.3
194
{ _: [], foo: "99.3" }
195
```
196
197
### boolean negation
198
199
* default: `true`
200
* key: `boolean-negation`
201
202
Should variables prefixed with `--no` be treated as negations?
203
204
```sh
205
node example.js --no-foo
206
{ _: [], foo: false }
207
```
208
209
_if disabled:_
210
211
```sh
212
node example.js --no-foo
213
{ _: [], "no-foo": true }
214
```
215
216
### combine arrays
217
218
* default: `false`
219
* key: `combine-arrays`
220
221
Should arrays be combined when provided by both command line arguments and
222
a configuration file.
223
224
### duplicate arguments array
225
226
* default: `true`
227
* key: `duplicate-arguments-array`
228
229
Should arguments be coerced into an array when duplicated:
230
231
```sh
232
node example.js -x 1 -x 2
233
{ _: [], x: [1, 2] }
234
```
235
236
_if disabled:_
237
238
```sh
239
node example.js -x 1 -x 2
240
{ _: [], x: 2 }
241
```
242
243
### flatten duplicate arrays
244
245
* default: `true`
246
* key: `flatten-duplicate-arrays`
247
248
Should array arguments be coerced into a single array when duplicated:
249
250
```sh
251
node example.js -x 1 2 -x 3 4
252
{ _: [], x: [1, 2, 3, 4] }
253
```
254
255
_if disabled:_
256
257
```sh
258
node example.js -x 1 2 -x 3 4
259
{ _: [], x: [[1, 2], [3, 4]] }
260
```
261
262
### negation prefix
263
264
* default: `no-`
265
* key: `negation-prefix`
266
267
The prefix to use for negated boolean variables.
268
269
```sh
270
node example.js --no-foo
271
{ _: [], foo: false }
272
```
273
274
_if set to `quux`:_
275
276
```sh
277
node example.js --quuxfoo
278
{ _: [], foo: false }
279
```
280
281
### populate --
282
283
* default: `false`.
284
* key: `populate--`
285
286
Should unparsed flags be stored in `--` or `_`.
287
288
_If disabled:_
289
290
```sh
291
node example.js a -b -- x y
292
{ _: [ 'a', 'x', 'y' ], b: true }
293
```
294
295
_If enabled:_
296
297
```sh
298
node example.js a -b -- x y
299
{ _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
300
```
301
302
### set placeholder key
303
304
* default: `false`.
305
* key: `set-placeholder-key`.
306
307
Should a placeholder be added for keys not set via the corresponding CLI argument?
308
309
_If disabled:_
310
311
```sh
312
node example.js -a 1 -c 2
313
{ _: [], a: 1, c: 2 }
314
```
315
316
_If enabled:_
317
318
```sh
319
node example.js -a 1 -c 2
320
{ _: [], a: 1, b: undefined, c: 2 }
321
```
322
323
### halt at non-option
324
325
* default: `false`.
326
* key: `halt-at-non-option`.
327
328
Should parsing stop at the first text argument? This is similar to how e.g. `ssh` parses its command line.
329
330
_If disabled:_
331
332
```sh
333
node example.js -a run b -x y
334
{ _: [ 'run', 'b', 'y' ], a: true, x: true }
335
```
336
337
_If enabled:_
338
339
```sh
340
node example.js -a run b -x y
341
{ _: [ 'run', 'b', '-x', 'y' ], a: true }
342
```
343
344
## Special Thanks
345
346
The yargs project evolves from optimist and minimist. It owes its
347
existence to a lot of James Halliday's hard work. Thanks [substack](https://github.com/substack) **beep** **boop** \o/
348
349
## License
350
351
ISC