GSI - Employe Self Service Mobile
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.

518 lines
12 KiB

2 months ago
  1. # yargs-parser
  2. ![ci](https://github.com/yargs/yargs-parser/workflows/ci/badge.svg)
  3. [![NPM version](https://img.shields.io/npm/v/yargs-parser.svg)](https://www.npmjs.com/package/yargs-parser)
  4. [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
  5. ![nycrc config on GitHub](https://img.shields.io/nycrc/yargs/yargs-parser)
  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/main/yargs-logo.png">
  9. ## Example
  10. ```sh
  11. npm i yargs-parser --save
  12. ```
  13. ```js
  14. const argv = require('yargs-parser')(process.argv.slice(2))
  15. console.log(argv)
  16. ```
  17. ```console
  18. $ node example.js --foo=33 --bar hello
  19. { _: [], foo: 33, bar: 'hello' }
  20. ```
  21. _or parse a string!_
  22. ```js
  23. const argv = require('yargs-parser')('--foo=99 --bar=33')
  24. console.log(argv)
  25. ```
  26. ```console
  27. { _: [], foo: 99, bar: 33 }
  28. ```
  29. Convert an array of mixed types before passing to `yargs-parser`:
  30. ```js
  31. const 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. ## Deno Example
  36. As of `v19` `yargs-parser` supports [Deno](https://github.com/denoland/deno):
  37. ```typescript
  38. import parser from "https://deno.land/x/yargs_parser/deno.ts";
  39. const argv = parser('--foo=99 --bar=9987930', {
  40. string: ['bar']
  41. })
  42. console.log(argv)
  43. ```
  44. ## ESM Example
  45. As of `v19` `yargs-parser` supports ESM (_both in Node.js and in the browser_):
  46. **Node.js:**
  47. ```js
  48. import parser from 'yargs-parser'
  49. const argv = parser('--foo=99 --bar=9987930', {
  50. string: ['bar']
  51. })
  52. console.log(argv)
  53. ```
  54. **Browsers:**
  55. ```html
  56. <!doctype html>
  57. <body>
  58. <script type="module">
  59. import parser from "https://unpkg.com/yargs-parser@19.0.0/browser.js";
  60. const argv = parser('--foo=99 --bar=9987930', {
  61. string: ['bar']
  62. })
  63. console.log(argv)
  64. </script>
  65. </body>
  66. ```
  67. ## API
  68. ### parser(args, opts={})
  69. Parses command line arguments returning a simple mapping of keys and values.
  70. **expects:**
  71. * `args`: a string or array of strings representing the options to parse.
  72. * `opts`: provide a set of hints indicating how `args` should be parsed:
  73. * `opts.alias`: an object representing the set of aliases for a key: `{alias: {foo: ['f']}}`.
  74. * `opts.array`: indicate that keys should be parsed as an array: `{array: ['foo', 'bar']}`.<br>
  75. Indicate that keys should be parsed as an array and coerced to booleans / numbers:<br>
  76. `{array: [{ key: 'foo', boolean: true }, {key: 'bar', number: true}]}`.
  77. * `opts.boolean`: arguments should be parsed as booleans: `{boolean: ['x', 'y']}`.
  78. * `opts.coerce`: provide a custom synchronous function that returns a coerced value from the argument provided
  79. (or throws an error). For arrays the function is called only once for the entire array:<br>
  80. `{coerce: {foo: function (arg) {return modifiedArg}}}`.
  81. * `opts.config`: indicate a key that represents a path to a configuration file (this file will be loaded and parsed).
  82. * `opts.configObjects`: configuration objects to parse, their properties will be set as arguments:<br>
  83. `{configObjects: [{'x': 5, 'y': 33}, {'z': 44}]}`.
  84. * `opts.configuration`: provide configuration options to the yargs-parser (see: [configuration](#configuration)).
  85. * `opts.count`: indicate a key that should be used as a counter, e.g., `-vvv` = `{v: 3}`.
  86. * `opts.default`: provide default values for keys: `{default: {x: 33, y: 'hello world!'}}`.
  87. * `opts.envPrefix`: environment variables (`process.env`) with the prefix provided should be parsed.
  88. * `opts.narg`: specify that a key requires `n` arguments: `{narg: {x: 2}}`.
  89. * `opts.normalize`: `path.normalize()` will be applied to values set to this key.
  90. * `opts.number`: keys should be treated as numbers.
  91. * `opts.string`: keys should be treated as strings (even if they resemble a number `-x 33`).
  92. **returns:**
  93. * `obj`: an object representing the parsed value of `args`
  94. * `key/value`: key value pairs for each argument and their aliases.
  95. * `_`: an array representing the positional arguments.
  96. * [optional] `--`: an array with arguments after the end-of-options flag `--`.
  97. ### require('yargs-parser').detailed(args, opts={})
  98. Parses a command line string, returning detailed information required by the
  99. yargs engine.
  100. **expects:**
  101. * `args`: a string or array of strings representing options to parse.
  102. * `opts`: provide a set of hints indicating how `args`, inputs are identical to `require('yargs-parser')(args, opts={})`.
  103. **returns:**
  104. * `argv`: an object representing the parsed value of `args`
  105. * `key/value`: key value pairs for each argument and their aliases.
  106. * `_`: an array representing the positional arguments.
  107. * [optional] `--`: an array with arguments after the end-of-options flag `--`.
  108. * `error`: populated with an error object if an exception occurred during parsing.
  109. * `aliases`: the inferred list of aliases built by combining lists in `opts.alias`.
  110. * `newAliases`: any new aliases added via camel-case expansion:
  111. * `boolean`: `{ fooBar: true }`
  112. * `defaulted`: any new argument created by `opts.default`, no aliases included.
  113. * `boolean`: `{ foo: true }`
  114. * `configuration`: given by default settings and `opts.configuration`.
  115. <a name="configuration"></a>
  116. ### Configuration
  117. The yargs-parser applies several automated transformations on the keys provided
  118. in `args`. These features can be turned on and off using the `configuration` field
  119. of `opts`.
  120. ```js
  121. var parsed = parser(['--no-dice'], {
  122. configuration: {
  123. 'boolean-negation': false
  124. }
  125. })
  126. ```
  127. ### short option groups
  128. * default: `true`.
  129. * key: `short-option-groups`.
  130. Should a group of short-options be treated as boolean flags?
  131. ```console
  132. $ node example.js -abc
  133. { _: [], a: true, b: true, c: true }
  134. ```
  135. _if disabled:_
  136. ```console
  137. $ node example.js -abc
  138. { _: [], abc: true }
  139. ```
  140. ### camel-case expansion
  141. * default: `true`.
  142. * key: `camel-case-expansion`.
  143. Should hyphenated arguments be expanded into camel-case aliases?
  144. ```console
  145. $ node example.js --foo-bar
  146. { _: [], 'foo-bar': true, fooBar: true }
  147. ```
  148. _if disabled:_
  149. ```console
  150. $ node example.js --foo-bar
  151. { _: [], 'foo-bar': true }
  152. ```
  153. ### dot-notation
  154. * default: `true`
  155. * key: `dot-notation`
  156. Should keys that contain `.` be treated as objects?
  157. ```console
  158. $ node example.js --foo.bar
  159. { _: [], foo: { bar: true } }
  160. ```
  161. _if disabled:_
  162. ```console
  163. $ node example.js --foo.bar
  164. { _: [], "foo.bar": true }
  165. ```
  166. ### parse numbers
  167. * default: `true`
  168. * key: `parse-numbers`
  169. Should keys that look like numbers be treated as such?
  170. ```console
  171. $ node example.js --foo=99.3
  172. { _: [], foo: 99.3 }
  173. ```
  174. _if disabled:_
  175. ```console
  176. $ node example.js --foo=99.3
  177. { _: [], foo: "99.3" }
  178. ```
  179. ### parse positional numbers
  180. * default: `true`
  181. * key: `parse-positional-numbers`
  182. Should positional keys that look like numbers be treated as such.
  183. ```console
  184. $ node example.js 99.3
  185. { _: [99.3] }
  186. ```
  187. _if disabled:_
  188. ```console
  189. $ node example.js 99.3
  190. { _: ['99.3'] }
  191. ```
  192. ### boolean negation
  193. * default: `true`
  194. * key: `boolean-negation`
  195. Should variables prefixed with `--no` be treated as negations?
  196. ```console
  197. $ node example.js --no-foo
  198. { _: [], foo: false }
  199. ```
  200. _if disabled:_
  201. ```console
  202. $ node example.js --no-foo
  203. { _: [], "no-foo": true }
  204. ```
  205. ### combine arrays
  206. * default: `false`
  207. * key: `combine-arrays`
  208. Should arrays be combined when provided by both command line arguments and
  209. a configuration file.
  210. ### duplicate arguments array
  211. * default: `true`
  212. * key: `duplicate-arguments-array`
  213. Should arguments be coerced into an array when duplicated:
  214. ```console
  215. $ node example.js -x 1 -x 2
  216. { _: [], x: [1, 2] }
  217. ```
  218. _if disabled:_
  219. ```console
  220. $ node example.js -x 1 -x 2
  221. { _: [], x: 2 }
  222. ```
  223. ### flatten duplicate arrays
  224. * default: `true`
  225. * key: `flatten-duplicate-arrays`
  226. Should array arguments be coerced into a single array when duplicated:
  227. ```console
  228. $ node example.js -x 1 2 -x 3 4
  229. { _: [], x: [1, 2, 3, 4] }
  230. ```
  231. _if disabled:_
  232. ```console
  233. $ node example.js -x 1 2 -x 3 4
  234. { _: [], x: [[1, 2], [3, 4]] }
  235. ```
  236. ### greedy arrays
  237. * default: `true`
  238. * key: `greedy-arrays`
  239. Should arrays consume more than one positional argument following their flag.
  240. ```console
  241. $ node example --arr 1 2
  242. { _: [], arr: [1, 2] }
  243. ```
  244. _if disabled:_
  245. ```console
  246. $ node example --arr 1 2
  247. { _: [2], arr: [1] }
  248. ```
  249. **Note: in `v18.0.0` we are considering defaulting greedy arrays to `false`.**
  250. ### nargs eats options
  251. * default: `false`
  252. * key: `nargs-eats-options`
  253. Should nargs consume dash options as well as positional arguments.
  254. ### negation prefix
  255. * default: `no-`
  256. * key: `negation-prefix`
  257. The prefix to use for negated boolean variables.
  258. ```console
  259. $ node example.js --no-foo
  260. { _: [], foo: false }
  261. ```
  262. _if set to `quux`:_
  263. ```console
  264. $ node example.js --quuxfoo
  265. { _: [], foo: false }
  266. ```
  267. ### populate --
  268. * default: `false`.
  269. * key: `populate--`
  270. Should unparsed flags be stored in `--` or `_`.
  271. _If disabled:_
  272. ```console
  273. $ node example.js a -b -- x y
  274. { _: [ 'a', 'x', 'y' ], b: true }
  275. ```
  276. _If enabled:_
  277. ```console
  278. $ node example.js a -b -- x y
  279. { _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
  280. ```
  281. ### set placeholder key
  282. * default: `false`.
  283. * key: `set-placeholder-key`.
  284. Should a placeholder be added for keys not set via the corresponding CLI argument?
  285. _If disabled:_
  286. ```console
  287. $ node example.js -a 1 -c 2
  288. { _: [], a: 1, c: 2 }
  289. ```
  290. _If enabled:_
  291. ```console
  292. $ node example.js -a 1 -c 2
  293. { _: [], a: 1, b: undefined, c: 2 }
  294. ```
  295. ### halt at non-option
  296. * default: `false`.
  297. * key: `halt-at-non-option`.
  298. Should parsing stop at the first positional argument? This is similar to how e.g. `ssh` parses its command line.
  299. _If disabled:_
  300. ```console
  301. $ node example.js -a run b -x y
  302. { _: [ 'b' ], a: 'run', x: 'y' }
  303. ```
  304. _If enabled:_
  305. ```console
  306. $ node example.js -a run b -x y
  307. { _: [ 'b', '-x', 'y' ], a: 'run' }
  308. ```
  309. ### strip aliased
  310. * default: `false`
  311. * key: `strip-aliased`
  312. Should aliases be removed before returning results?
  313. _If disabled:_
  314. ```console
  315. $ node example.js --test-field 1
  316. { _: [], 'test-field': 1, testField: 1, 'test-alias': 1, testAlias: 1 }
  317. ```
  318. _If enabled:_
  319. ```console
  320. $ node example.js --test-field 1
  321. { _: [], 'test-field': 1, testField: 1 }
  322. ```
  323. ### strip dashed
  324. * default: `false`
  325. * key: `strip-dashed`
  326. Should dashed keys be removed before returning results? This option has no effect if
  327. `camel-case-expansion` is disabled.
  328. _If disabled:_
  329. ```console
  330. $ node example.js --test-field 1
  331. { _: [], 'test-field': 1, testField: 1 }
  332. ```
  333. _If enabled:_
  334. ```console
  335. $ node example.js --test-field 1
  336. { _: [], testField: 1 }
  337. ```
  338. ### unknown options as args
  339. * default: `false`
  340. * key: `unknown-options-as-args`
  341. Should unknown options be treated like regular arguments? An unknown option is one that is not
  342. configured in `opts`.
  343. _If disabled_
  344. ```console
  345. $ node example.js --unknown-option --known-option 2 --string-option --unknown-option2
  346. { _: [], unknownOption: true, knownOption: 2, stringOption: '', unknownOption2: true }
  347. ```
  348. _If enabled_
  349. ```console
  350. $ node example.js --unknown-option --known-option 2 --string-option --unknown-option2
  351. { _: ['--unknown-option'], knownOption: 2, stringOption: '--unknown-option2' }
  352. ```
  353. ## Supported Node.js Versions
  354. Libraries in this ecosystem make a best effort to track
  355. [Node.js' release schedule](https://nodejs.org/en/about/releases/). Here's [a
  356. post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).
  357. ## Special Thanks
  358. The yargs project evolves from optimist and minimist. It owes its
  359. existence to a lot of James Halliday's hard work. Thanks [substack](https://github.com/substack) **beep** **boop** \o/
  360. ## License
  361. ISC