You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # yargs-parser
  2. [![Build Status](https://travis-ci.org/yargs/yargs-parser.png)](https://travis-ci.org/yargs/yargs-parser)
  3. [![Coverage Status](https://coveralls.io/repos/yargs/yargs-parser/badge.svg?branch=)](https://coveralls.io/r/yargs/yargs-parser?branch=master)
  4. [![NPM version](https://img.shields.io/npm/v/yargs-parser.svg)](https://www.npmjs.com/package/yargs-parser)
  5. [![Windows Tests](https://img.shields.io/appveyor/ci/bcoe/yargs-parser/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/bcoe/yargs-parser)
  6. [![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
  7. The mighty option parser used by [yargs](https://github.com/yargs/yargs).
  8. visit the [yargs website](http://yargs.js.org/) for more examples, and thorough usage instructions.
  9. <img width="250" src="https://github.com/yargs/yargs-parser/blob/master/yargs-logo.png">
  10. ## Example
  11. ```sh
  12. npm i yargs-parser --save
  13. ```
  14. ```js
  15. var argv = require('yargs-parser')(process.argv.slice(2))
  16. console.log(argv)
  17. ```
  18. ```sh
  19. node example.js --foo=33 --bar hello
  20. { _: [], foo: 33, bar: 'hello' }
  21. ```
  22. _or parse a string!_
  23. ```js
  24. var argv = require('./')('--foo=99 --bar=33')
  25. console.log(argv)
  26. ```
  27. ```sh
  28. { _: [], foo: 99, bar: 33 }
  29. ```
  30. Convert an array of mixed types before passing to `yargs-parser`:
  31. ```js
  32. var parse = require('yargs-parser')
  33. parse(['-f', 11, '--zoom', 55].join(' ')) // <-- array to string
  34. parse(['-f', 11, '--zoom', 55].map(String)) // <-- array of strings
  35. ```
  36. ## API
  37. ### require('yargs-parser')(args, opts={})
  38. Parses command line arguments returning a simple mapping of keys and values.
  39. **expects:**
  40. * `args`: a string or array of strings representing the options to parse.
  41. * `opts`: provide a set of hints indicating how `args` should be parsed:
  42. * `opts.alias`: an object representing the set of aliases for a key: `{alias: {foo: ['f']}}`.
  43. * `opts.array`: indicate that keys should be parsed as an array: `{array: ['foo', 'bar']}`.
  44. * `opts.boolean`: arguments should be parsed as booleans: `{boolean: ['x', 'y']}`.
  45. * `opts.config`: indicate a key that represents a path to a configuration file (this file will be loaded and parsed).
  46. * `opts.coerce`: provide a custom synchronous function that returns a coerced value from the argument provided
  47. (or throws an error), e.g. `{coerce: {foo: function (arg) {return modifiedArg}}}`.
  48. * `opts.count`: indicate a key that should be used as a counter, e.g., `-vvv` = `{v: 3}`.
  49. * `opts.default`: provide default values for keys: `{default: {x: 33, y: 'hello world!'}}`.
  50. * `opts.envPrefix`: environment variables (`process.env`) with the prefix provided should be parsed.
  51. * `opts.narg`: specify that a key requires `n` arguments: `{narg: {x: 2}}`.
  52. * `opts.normalize`: `path.normalize()` will be applied to values set to this key.
  53. * `opts.string`: keys should be treated as strings (even if they resemble a number `-x 33`).
  54. * `opts.configuration`: provide configuration options to the yargs-parser (see: [configuration](#configuration)).
  55. * `opts.number`: keys should be treated as numbers.
  56. **returns:**
  57. * `obj`: an object representing the parsed value of `args`
  58. * `key/value`: key value pairs for each argument and their aliases.
  59. * `_`: an array representing the positional arguments.
  60. ### require('yargs-parser').detailed(args, opts={})
  61. Parses a command line string, returning detailed information required by the
  62. yargs engine.
  63. **expects:**
  64. * `args`: a string or array of strings representing options to parse.
  65. * `opts`: provide a set of hints indicating how `args`, inputs are identical to `require('yargs-parser')(args, opts={})`.
  66. **returns:**
  67. * `argv`: an object representing the parsed value of `args`
  68. * `key/value`: key value pairs for each argument and their aliases.
  69. * `_`: an array representing the positional arguments.
  70. * `error`: populated with an error object if an exception occurred during parsing.
  71. * `aliases`: the inferred list of aliases built by combining lists in `opts.alias`.
  72. * `newAliases`: any new aliases added via camel-case expansion.
  73. * `configuration`: the configuration loaded from the `yargs` stanza in package.json.
  74. <a name="configuration"></a>
  75. ### Configuration
  76. The yargs-parser applies several automated transformations on the keys provided
  77. in `args`. These features can be turned on and off using the `configuration` field
  78. of `opts`.
  79. ```js
  80. var parsed = parser(['--no-dice'], {
  81. configuration: {
  82. 'boolean-negation': false
  83. }
  84. })
  85. ```
  86. ### short option groups
  87. * default: `true`.
  88. * key: `short-option-groups`.
  89. Should a group of short-options be treated as boolean flags?
  90. ```sh
  91. node example.js -abc
  92. { _: [], a: true, b: true, c: true }
  93. ```
  94. _if disabled:_
  95. ```sh
  96. node example.js -abc
  97. { _: [], abc: true }
  98. ```
  99. ### camel-case expansion
  100. * default: `true`.
  101. * key: `camel-case-expansion`.
  102. Should hyphenated arguments be expanded into camel-case aliases?
  103. ```sh
  104. node example.js --foo-bar
  105. { _: [], 'foo-bar': true, fooBar: true }
  106. ```
  107. _if disabled:_
  108. ```sh
  109. node example.js --foo-bar
  110. { _: [], 'foo-bar': true }
  111. ```
  112. ### dot-notation
  113. * default: `true`
  114. * key: `dot-notation`
  115. Should keys that contain `.` be treated as objects?
  116. ```sh
  117. node example.js --foo.bar
  118. { _: [], foo: { bar: true } }
  119. ```
  120. _if disabled:_
  121. ```sh
  122. node example.js --foo.bar
  123. { _: [], "foo.bar": true }
  124. ```
  125. ### parse numbers
  126. * default: `true`
  127. * key: `parse-numbers`
  128. Should keys that look like numbers be treated as such?
  129. ```sh
  130. node example.js --foo=99.3
  131. { _: [], foo: 99.3 }
  132. ```
  133. _if disabled:_
  134. ```sh
  135. node example.js --foo=99.3
  136. { _: [], foo: "99.3" }
  137. ```
  138. ### boolean negation
  139. * default: `true`
  140. * key: `boolean-negation`
  141. Should variables prefixed with `--no` be treated as negations?
  142. ```sh
  143. node example.js --no-foo
  144. { _: [], foo: false }
  145. ```
  146. _if disabled:_
  147. ```sh
  148. node example.js --no-foo
  149. { _: [], "no-foo": true }
  150. ```
  151. ### duplicate arguments array
  152. * default: `true`
  153. * key: `duplicate-arguments-array`
  154. Should arguments be coerced into an array when duplicated:
  155. ```sh
  156. node example.js -x 1 -x 2
  157. { _: [], x: [1, 2] }
  158. ```
  159. _if disabled:_
  160. ```sh
  161. node example.js -x 1 -x 2
  162. { _: [], x: 2 }
  163. ```
  164. ### flatten duplicate arrays
  165. * default: `true`
  166. * key: `flatten-duplicate-arrays`
  167. Should array arguments be coerced into a single array when duplicated:
  168. ```sh
  169. node example.js -x 1 2 -x 3 4
  170. { _: [], x: [1, 2, 3, 4] }
  171. ```
  172. _if disabled:_
  173. ```sh
  174. node example.js -x 1 2 -x 3 4
  175. { _: [], x: [[1, 2], [3, 4]] }
  176. ```
  177. ## Special Thanks
  178. The yargs project evolves from optimist and minimist. It owes its
  179. existence to a lot of James Halliday's hard work. Thanks [substack](https://github.com/substack) **beep** **boop** \o/
  180. ## License
  181. ISC