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 9The mighty option parser used by [yargs](https://github.com/yargs/yargs). 10 11visit 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 18npm i yargs-parser --save 19``` 20 21```js 22var argv = require('yargs-parser')(process.argv.slice(2)) 23console.log(argv) 24``` 25 26```sh 27node example.js --foo=33 --bar hello 28{ _: [], foo: 33, bar: 'hello' } 29``` 30 31_or parse a string!_ 32 33```js 34var argv = require('yargs-parser')('--foo=99 --bar=33') 35console.log(argv) 36``` 37 38```sh 39{ _: [], foo: 99, bar: 33 } 40``` 41 42Convert an array of mixed types before passing to `yargs-parser`: 43 44```js 45var parse = require('yargs-parser') 46parse(['-f', 11, '--zoom', 55].join(' ')) // <-- array to string 47parse(['-f', 11, '--zoom', 55].map(String)) // <-- array of strings 48``` 49 50## API 51 52### require('yargs-parser')(args, opts={}) 53 54Parses 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 89Parses a command line string, returning detailed information required by the 90yargs 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 111The yargs-parser applies several automated transformations on the keys provided 112in `args`. These features can be turned on and off using the `configuration` field 113of `opts`. 114 115```js 116var 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 128Should a group of short-options be treated as boolean flags? 129 130```sh 131node example.js -abc 132{ _: [], a: true, b: true, c: true } 133``` 134 135_if disabled:_ 136 137```sh 138node example.js -abc 139{ _: [], abc: true } 140``` 141 142### camel-case expansion 143 144* default: `true`. 145* key: `camel-case-expansion`. 146 147Should hyphenated arguments be expanded into camel-case aliases? 148 149```sh 150node example.js --foo-bar 151{ _: [], 'foo-bar': true, fooBar: true } 152``` 153 154_if disabled:_ 155 156```sh 157node example.js --foo-bar 158{ _: [], 'foo-bar': true } 159``` 160 161### dot-notation 162 163* default: `true` 164* key: `dot-notation` 165 166Should keys that contain `.` be treated as objects? 167 168```sh 169node example.js --foo.bar 170{ _: [], foo: { bar: true } } 171``` 172 173_if disabled:_ 174 175```sh 176node example.js --foo.bar 177{ _: [], "foo.bar": true } 178``` 179 180### parse numbers 181 182* default: `true` 183* key: `parse-numbers` 184 185Should keys that look like numbers be treated as such? 186 187```sh 188node example.js --foo=99.3 189{ _: [], foo: 99.3 } 190``` 191 192_if disabled:_ 193 194```sh 195node example.js --foo=99.3 196{ _: [], foo: "99.3" } 197``` 198 199### boolean negation 200 201* default: `true` 202* key: `boolean-negation` 203 204Should variables prefixed with `--no` be treated as negations? 205 206```sh 207node example.js --no-foo 208{ _: [], foo: false } 209``` 210 211_if disabled:_ 212 213```sh 214node example.js --no-foo 215{ _: [], "no-foo": true } 216``` 217 218### combine arrays 219 220* default: `false` 221* key: `combine-arrays` 222 223Should arrays be combined when provided by both command line arguments and 224a configuration file. 225 226### duplicate arguments array 227 228* default: `true` 229* key: `duplicate-arguments-array` 230 231Should arguments be coerced into an array when duplicated: 232 233```sh 234node example.js -x 1 -x 2 235{ _: [], x: [1, 2] } 236``` 237 238_if disabled:_ 239 240```sh 241node 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 250Should array arguments be coerced into a single array when duplicated: 251 252```sh 253node example.js -x 1 2 -x 3 4 254{ _: [], x: [1, 2, 3, 4] } 255``` 256 257_if disabled:_ 258 259```sh 260node 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 269The prefix to use for negated boolean variables. 270 271```sh 272node example.js --no-foo 273{ _: [], foo: false } 274``` 275 276_if set to `quux`:_ 277 278```sh 279node example.js --quuxfoo 280{ _: [], foo: false } 281``` 282 283### populate -- 284 285* default: `false`. 286* key: `populate--` 287 288Should unparsed flags be stored in `--` or `_`. 289 290_If disabled:_ 291 292```sh 293node example.js a -b -- x y 294{ _: [ 'a', 'x', 'y' ], b: true } 295``` 296 297_If enabled:_ 298 299```sh 300node 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 309Should a placeholder be added for keys not set via the corresponding CLI argument? 310 311_If disabled:_ 312 313```sh 314node example.js -a 1 -c 2 315{ _: [], a: 1, c: 2 } 316``` 317 318_If enabled:_ 319 320```sh 321node 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 330Should 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 335node example.js -a run b -x y 336{ _: [ 'b' ], a: 'run', x: 'y' } 337``` 338 339_If enabled:_ 340 341```sh 342node 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 351Should aliases be removed before returning results? 352 353_If disabled:_ 354 355```sh 356node example.js --test-field 1 357{ _: [], 'test-field': 1, testField: 1, 'test-alias': 1, testAlias: 1 } 358``` 359 360_If enabled:_ 361 362```sh 363node 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 372Should dashed keys be removed before returning results? This option has no effect if 373`camel-case-expansion` is disabled. 374 375_If disabled:_ 376 377```sh 378node example.js --test-field 1 379{ _: [], 'test-field': 1, testField: 1 } 380``` 381 382_If enabled:_ 383 384```sh 385node example.js --test-field 1 386{ _: [], testField: 1 } 387``` 388 389### unknown options as args 390 391* default: `false` 392* key: `unknown-options-as-args` 393 394Should unknown options be treated like regular arguments? An unknown option is one that is not 395configured in `opts`. 396 397_If disabled_ 398 399```sh 400node example.js --unknown-option --known-option 2 --string-option --unknown-option2 401{ _: [], unknownOption: true, knownOption: 2, stringOption: '', unknownOption2: true } 402``` 403 404_If enabled_ 405 406```sh 407node example.js --unknown-option --known-option 2 --string-option --unknown-option2 408{ _: ['--unknown-option'], knownOption: 2, stringOption: '--unknown-option2' } 409``` 410 411## Special Thanks 412 413The yargs project evolves from optimist and minimist. It owes its 414existence to a lot of James Halliday's hard work. Thanks [substack](https://github.com/substack) **beep** **boop** \o/ 415 416## License 417 418ISC 419