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.

396 lines
8.9 KiB

4 years ago
  1. # yargs-parser
  2. [![Build Status](https://travis-ci.org/yargs/yargs-parser.svg)](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. [![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
  6. The mighty option parser used by [yargs](https://github.com/yargs/yargs).
  7. visit the [yargs website](http://yargs.js.org/) for more examples, and thorough usage instructions.
  8. <img width="250" src="https://raw.githubusercontent.com/yargs/yargs-parser/master/yargs-logo.png">
  9. ## Example
  10. ```sh
  11. npm i yargs-parser --save
  12. ```
  13. ```js
  14. var argv = require('yargs-parser')(process.argv.slice(2))
  15. console.log(argv)
  16. ```
  17. ```sh
  18. node example.js --foo=33 --bar hello
  19. { _: [], foo: 33, bar: 'hello' }
  20. ```
  21. _or parse a string!_
  22. ```js
  23. var argv = require('./')('--foo=99 --bar=33')
  24. console.log(argv)
  25. ```
  26. ```sh
  27. { _: [], foo: 99, bar: 33 }
  28. ```
  29. Convert an array of mixed types before passing to `yargs-parser`:
  30. ```js
  31. var parse = require('yargs-parser')
  32. parse(['-f', 11, '--zoom', 55].join(' ')) // <-- array to string
  33. parse(['-f', 11, '--zoom', 55].map(String)) // <-- array of strings
  34. ```
  35. ## API
  36. ### require('yargs-parser')(args, opts={})
  37. Parses command line arguments returning a simple mapping of keys and values.
  38. **expects:**
  39. * `args`: a string or array of strings representing the options to parse.
  40. * `opts`: provide a set of hints indicating how `args` should be parsed:
  41. * `opts.alias`: an object representing the set of aliases for a key: `{alias: {foo: ['f']}}`.
  42. * `opts.array`: indicate that keys should be parsed as an array: `{array: ['foo', 'bar']}`.<br>
  43. Indicate that keys should be parsed as an array and coerced to booleans / numbers:<br>
  44. `{array: [{ key: 'foo', boolean: true }, {key: 'bar', number: true}]}`.
  45. * `opts.boolean`: arguments should be parsed as booleans: `{boolean: ['x', 'y']}`.
  46. * `opts.coerce`: provide a custom synchronous function that returns a coerced value from the argument provided
  47. (or throws an error). For arrays the function is called only once for the entire array:<br>
  48. `{coerce: {foo: function (arg) {return modifiedArg}}}`.
  49. * `opts.config`: indicate a key that represents a path to a configuration file (this file will be loaded and parsed).
  50. * `opts.configObjects`: configuration objects to parse, their properties will be set as arguments:<br>
  51. `{configObjects: [{'x': 5, 'y': 33}, {'z': 44}]}`.
  52. * `opts.configuration`: provide configuration options to the yargs-parser (see: [configuration](#configuration)).
  53. * `opts.count`: indicate a key that should be used as a counter, e.g., `-vvv` = `{v: 3}`.
  54. * `opts.default`: provide default values for keys: `{default: {x: 33, y: 'hello world!'}}`.
  55. * `opts.envPrefix`: environment variables (`process.env`) with the prefix provided should be parsed.
  56. * `opts.narg`: specify that a key requires `n` arguments: `{narg: {x: 2}}`.
  57. * `opts.normalize`: `path.normalize()` will be applied to values set to this key.
  58. * `opts.number`: keys should be treated as numbers.
  59. * `opts.string`: keys should be treated as strings (even if they resemble a number `-x 33`).
  60. **returns:**
  61. * `obj`: an object representing the parsed value of `args`
  62. * `key/value`: key value pairs for each argument and their aliases.
  63. * `_`: an array representing the positional arguments.
  64. * [optional] `--`: an array with arguments after the end-of-options flag `--`.
  65. ### require('yargs-parser').detailed(args, opts={})
  66. Parses a command line string, returning detailed information required by the
  67. yargs engine.
  68. **expects:**
  69. * `args`: a string or array of strings representing options to parse.
  70. * `opts`: provide a set of hints indicating how `args`, inputs are identical to `require('yargs-parser')(args, opts={})`.
  71. **returns:**
  72. * `argv`: an object representing the parsed value of `args`
  73. * `key/value`: key value pairs for each argument and their aliases.
  74. * `_`: an array representing the positional arguments.
  75. * `error`: populated with an error object if an exception occurred during parsing.
  76. * `aliases`: the inferred list of aliases built by combining lists in `opts.alias`.
  77. * `newAliases`: any new aliases added via camel-case expansion.
  78. * `configuration`: the configuration loaded from the `yargs` stanza in package.json.
  79. <a name="configuration"></a>
  80. ### Configuration
  81. The yargs-parser applies several automated transformations on the keys provided
  82. in `args`. These features can be turned on and off using the `configuration` field
  83. of `opts`.
  84. ```js
  85. var parsed = parser(['--no-dice'], {
  86. configuration: {
  87. 'boolean-negation': false
  88. }
  89. })
  90. ```
  91. ### short option groups
  92. * default: `true`.
  93. * key: `short-option-groups`.
  94. Should a group of short-options be treated as boolean flags?
  95. ```sh
  96. node example.js -abc
  97. { _: [], a: true, b: true, c: true }
  98. ```
  99. _if disabled:_
  100. ```sh
  101. node example.js -abc
  102. { _: [], abc: true }
  103. ```
  104. ### camel-case expansion
  105. * default: `true`.
  106. * key: `camel-case-expansion`.
  107. Should hyphenated arguments be expanded into camel-case aliases?
  108. ```sh
  109. node example.js --foo-bar
  110. { _: [], 'foo-bar': true, fooBar: true }
  111. ```
  112. _if disabled:_
  113. ```sh
  114. node example.js --foo-bar
  115. { _: [], 'foo-bar': true }
  116. ```
  117. ### dot-notation
  118. * default: `true`
  119. * key: `dot-notation`
  120. Should keys that contain `.` be treated as objects?
  121. ```sh
  122. node example.js --foo.bar
  123. { _: [], foo: { bar: true } }
  124. ```
  125. _if disabled:_
  126. ```sh
  127. node example.js --foo.bar
  128. { _: [], "foo.bar": true }
  129. ```
  130. ### parse numbers
  131. * default: `true`
  132. * key: `parse-numbers`
  133. Should keys that look like numbers be treated as such?
  134. ```sh
  135. node example.js --foo=99.3
  136. { _: [], foo: 99.3 }
  137. ```
  138. _if disabled:_
  139. ```sh
  140. node example.js --foo=99.3
  141. { _: [], foo: "99.3" }
  142. ```
  143. ### boolean negation
  144. * default: `true`
  145. * key: `boolean-negation`
  146. Should variables prefixed with `--no` be treated as negations?
  147. ```sh
  148. node example.js --no-foo
  149. { _: [], foo: false }
  150. ```
  151. _if disabled:_
  152. ```sh
  153. node example.js --no-foo
  154. { _: [], "no-foo": true }
  155. ```
  156. ### combine arrays
  157. * default: `false`
  158. * key: `combine-arrays`
  159. Should arrays be combined when provided by both command line arguments and
  160. a configuration file.
  161. ### duplicate arguments array
  162. * default: `true`
  163. * key: `duplicate-arguments-array`
  164. Should arguments be coerced into an array when duplicated:
  165. ```sh
  166. node example.js -x 1 -x 2
  167. { _: [], x: [1, 2] }
  168. ```
  169. _if disabled:_
  170. ```sh
  171. node example.js -x 1 -x 2
  172. { _: [], x: 2 }
  173. ```
  174. ### flatten duplicate arrays
  175. * default: `true`
  176. * key: `flatten-duplicate-arrays`
  177. Should array arguments be coerced into a single array when duplicated:
  178. ```sh
  179. node example.js -x 1 2 -x 3 4
  180. { _: [], x: [1, 2, 3, 4] }
  181. ```
  182. _if disabled:_
  183. ```sh
  184. node example.js -x 1 2 -x 3 4
  185. { _: [], x: [[1, 2], [3, 4]] }
  186. ```
  187. ### negation prefix
  188. * default: `no-`
  189. * key: `negation-prefix`
  190. The prefix to use for negated boolean variables.
  191. ```sh
  192. node example.js --no-foo
  193. { _: [], foo: false }
  194. ```
  195. _if set to `quux`:_
  196. ```sh
  197. node example.js --quuxfoo
  198. { _: [], foo: false }
  199. ```
  200. ### populate --
  201. * default: `false`.
  202. * key: `populate--`
  203. Should unparsed flags be stored in `--` or `_`.
  204. _If disabled:_
  205. ```sh
  206. node example.js a -b -- x y
  207. { _: [ 'a', 'x', 'y' ], b: true }
  208. ```
  209. _If enabled:_
  210. ```sh
  211. node example.js a -b -- x y
  212. { _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
  213. ```
  214. ### set placeholder key
  215. * default: `false`.
  216. * key: `set-placeholder-key`.
  217. Should a placeholder be added for keys not set via the corresponding CLI argument?
  218. _If disabled:_
  219. ```sh
  220. node example.js -a 1 -c 2
  221. { _: [], a: 1, c: 2 }
  222. ```
  223. _If enabled:_
  224. ```sh
  225. node example.js -a 1 -c 2
  226. { _: [], a: 1, b: undefined, c: 2 }
  227. ```
  228. ### halt at non-option
  229. * default: `false`.
  230. * key: `halt-at-non-option`.
  231. Should parsing stop at the first positional argument? This is similar to how e.g. `ssh` parses its command line.
  232. _If disabled:_
  233. ```sh
  234. node example.js -a run b -x y
  235. { _: [ 'b' ], a: 'run', x: 'y' }
  236. ```
  237. _If enabled:_
  238. ```sh
  239. node example.js -a run b -x y
  240. { _: [ 'b', '-x', 'y' ], a: 'run' }
  241. ```
  242. ### strip aliased
  243. * default: `false`
  244. * key: `strip-aliased`
  245. Should aliases be removed before returning results?
  246. _If disabled:_
  247. ```sh
  248. node example.js --test-field 1
  249. { _: [], 'test-field': 1, testField: 1, 'test-alias': 1, testAlias: 1 }
  250. ```
  251. _If enabled:_
  252. ```sh
  253. node example.js --test-field 1
  254. { _: [], 'test-field': 1, testField: 1 }
  255. ```
  256. ### strip dashed
  257. * default: `false`
  258. * key: `strip-dashed`
  259. Should dashed keys be removed before returning results? This option has no effect if
  260. `camel-case-exansion` is disabled.
  261. _If disabled:_
  262. ```sh
  263. node example.js --test-field 1
  264. { _: [], 'test-field': 1, testField: 1 }
  265. ```
  266. _If enabled:_
  267. ```sh
  268. node example.js --test-field 1
  269. { _: [], testField: 1 }
  270. ```
  271. ## Special Thanks
  272. The yargs project evolves from optimist and minimist. It owes its
  273. existence to a lot of James Halliday's hard work. Thanks [substack](https://github.com/substack) **beep** **boop** \o/
  274. ## License
  275. ISC