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)
9 The mighty option parser used by [yargs](https://github.com/yargs/yargs).
11 visit the [yargs website](http://yargs.js.org/) for more examples, and thorough usage instructions.
13 <img width="250" src="https://raw.githubusercontent.com/yargs/yargs-parser/master/yargs-logo.png">
18 npm i yargs-parser --save
22 var argv = require('yargs-parser')(process.argv.slice(2))
27 node example.js --foo=33 --bar hello
28 { _: [], foo: 33, bar: 'hello' }
34 var argv = require('./')('--foo=99 --bar=33')
39 { _: [], foo: 99, bar: 33 }
42 Convert an array of mixed types before passing to `yargs-parser`:
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
52 ### require('yargs-parser')(args, opts={})
54 Parses command line arguments returning a simple mapping of keys and values.
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`).
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 `--`.
87 ### require('yargs-parser').detailed(args, opts={})
89 Parses a command line string, returning detailed information required by the
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={})`.
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.
107 <a name="configuration"></a>
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
116 var parsed = parser(['--no-dice'], {
118 'boolean-negation': false
123 ### short option groups
126 * key: `short-option-groups`.
128 Should a group of short-options be treated as boolean flags?
132 { _: [], a: true, b: true, c: true }
142 ### camel-case expansion
145 * key: `camel-case-expansion`.
147 Should hyphenated arguments be expanded into camel-case aliases?
150 node example.js --foo-bar
151 { _: [], 'foo-bar': true, fooBar: true }
157 node example.js --foo-bar
158 { _: [], 'foo-bar': true }
164 * key: `dot-notation`
166 Should keys that contain `.` be treated as objects?
169 node example.js --foo.bar
170 { _: [], foo: { bar: true } }
176 node example.js --foo.bar
177 { _: [], "foo.bar": true }
183 * key: `parse-numbers`
185 Should keys that look like numbers be treated as such?
188 node example.js --foo=99.3
195 node example.js --foo=99.3
196 { _: [], foo: "99.3" }
202 * key: `boolean-negation`
204 Should variables prefixed with `--no` be treated as negations?
207 node example.js --no-foo
208 { _: [], foo: false }
214 node example.js --no-foo
215 { _: [], "no-foo": true }
221 * key: `combine-arrays`
223 Should arrays be combined when provided by both command line arguments and
224 a configuration file.
226 ### duplicate arguments array
229 * key: `duplicate-arguments-array`
231 Should arguments be coerced into an array when duplicated:
234 node example.js -x 1 -x 2
241 node example.js -x 1 -x 2
245 ### flatten duplicate arrays
248 * key: `flatten-duplicate-arrays`
250 Should array arguments be coerced into a single array when duplicated:
253 node example.js -x 1 2 -x 3 4
254 { _: [], x: [1, 2, 3, 4] }
260 node example.js -x 1 2 -x 3 4
261 { _: [], x: [[1, 2], [3, 4]] }
267 * key: `negation-prefix`
269 The prefix to use for negated boolean variables.
272 node example.js --no-foo
273 { _: [], foo: false }
279 node example.js --quuxfoo
280 { _: [], foo: false }
288 Should unparsed flags be stored in `--` or `_`.
293 node example.js a -b -- x y
294 { _: [ 'a', 'x', 'y' ], b: true }
300 node example.js a -b -- x y
301 { _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
304 ### set placeholder key
307 * key: `set-placeholder-key`.
309 Should a placeholder be added for keys not set via the corresponding CLI argument?
314 node example.js -a 1 -c 2
315 { _: [], a: 1, c: 2 }
321 node example.js -a 1 -c 2
322 { _: [], a: 1, b: undefined, c: 2 }
325 ### halt at non-option
328 * key: `halt-at-non-option`.
330 Should parsing stop at the first positional argument? This is similar to how e.g. `ssh` parses its command line.
335 node example.js -a run b -x y
336 { _: [ 'b' ], a: 'run', x: 'y' }
342 node example.js -a run b -x y
343 { _: [ 'b', '-x', 'y' ], a: 'run' }
349 * key: `strip-aliased`
351 Should aliases be removed before returning results?
356 node example.js --test-field 1
357 { _: [], 'test-field': 1, testField: 1, 'test-alias': 1, testAlias: 1 }
363 node example.js --test-field 1
364 { _: [], 'test-field': 1, testField: 1 }
370 * key: `strip-dashed`
372 Should dashed keys be removed before returning results? This option has no effect if
373 `camel-case-exansion` is disabled.
378 node example.js --test-field 1
379 { _: [], 'test-field': 1, testField: 1 }
385 node example.js --test-field 1
386 { _: [], testField: 1 }
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/