| | |
| | | # yargs-parser |
| | | |
| | |  |
| | | [](https://travis-ci.org/yargs/yargs-parser) |
| | | [](https://www.npmjs.com/package/yargs-parser) |
| | | [](https://conventionalcommits.org) |
| | |  |
| | | [](https://github.com/conventional-changelog/standard-version) |
| | | |
| | | |
| | | The mighty option parser used by [yargs](https://github.com/yargs/yargs). |
| | | |
| | | visit the [yargs website](http://yargs.js.org/) for more examples, and thorough usage instructions. |
| | | |
| | | <img width="250" src="https://raw.githubusercontent.com/yargs/yargs-parser/main/yargs-logo.png"> |
| | | <img width="250" src="https://raw.githubusercontent.com/yargs/yargs-parser/master/yargs-logo.png"> |
| | | |
| | | ## Example |
| | | |
| | |
| | | ``` |
| | | |
| | | ```js |
| | | const argv = require('yargs-parser')(process.argv.slice(2)) |
| | | var argv = require('yargs-parser')(process.argv.slice(2)) |
| | | console.log(argv) |
| | | ``` |
| | | |
| | | ```console |
| | | $ node example.js --foo=33 --bar hello |
| | | ```sh |
| | | node example.js --foo=33 --bar hello |
| | | { _: [], foo: 33, bar: 'hello' } |
| | | ``` |
| | | |
| | | _or parse a string!_ |
| | | |
| | | ```js |
| | | const argv = require('yargs-parser')('--foo=99 --bar=33') |
| | | var argv = require('yargs-parser')('--foo=99 --bar=33') |
| | | console.log(argv) |
| | | ``` |
| | | |
| | | ```console |
| | | ```sh |
| | | { _: [], foo: 99, bar: 33 } |
| | | ``` |
| | | |
| | | Convert an array of mixed types before passing to `yargs-parser`: |
| | | |
| | | ```js |
| | | const parse = require('yargs-parser') |
| | | var parse = require('yargs-parser') |
| | | parse(['-f', 11, '--zoom', 55].join(' ')) // <-- array to string |
| | | parse(['-f', 11, '--zoom', 55].map(String)) // <-- array of strings |
| | | ``` |
| | | |
| | | ## Deno Example |
| | | |
| | | As of `v19` `yargs-parser` supports [Deno](https://github.com/denoland/deno): |
| | | |
| | | ```typescript |
| | | import parser from "https://deno.land/x/yargs_parser/deno.ts"; |
| | | |
| | | const argv = parser('--foo=99 --bar=9987930', { |
| | | string: ['bar'] |
| | | }) |
| | | console.log(argv) |
| | | ``` |
| | | |
| | | ## ESM Example |
| | | |
| | | As of `v19` `yargs-parser` supports ESM (_both in Node.js and in the browser_): |
| | | |
| | | **Node.js:** |
| | | |
| | | ```js |
| | | import parser from 'yargs-parser' |
| | | |
| | | const argv = parser('--foo=99 --bar=9987930', { |
| | | string: ['bar'] |
| | | }) |
| | | console.log(argv) |
| | | ``` |
| | | |
| | | **Browsers:** |
| | | |
| | | ```html |
| | | <!doctype html> |
| | | <body> |
| | | <script type="module"> |
| | | import parser from "https://unpkg.com/yargs-parser@19.0.0/browser.js"; |
| | | |
| | | const argv = parser('--foo=99 --bar=9987930', { |
| | | string: ['bar'] |
| | | }) |
| | | console.log(argv) |
| | | </script> |
| | | </body> |
| | | ``` |
| | | |
| | | ## API |
| | | |
| | | ### parser(args, opts={}) |
| | | ### require('yargs-parser')(args, opts={}) |
| | | |
| | | Parses command line arguments returning a simple mapping of keys and values. |
| | | |
| | |
| | | |
| | | Should a group of short-options be treated as boolean flags? |
| | | |
| | | ```console |
| | | $ node example.js -abc |
| | | ```sh |
| | | node example.js -abc |
| | | { _: [], a: true, b: true, c: true } |
| | | ``` |
| | | |
| | | _if disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js -abc |
| | | ```sh |
| | | node example.js -abc |
| | | { _: [], abc: true } |
| | | ``` |
| | | |
| | |
| | | |
| | | Should hyphenated arguments be expanded into camel-case aliases? |
| | | |
| | | ```console |
| | | $ node example.js --foo-bar |
| | | ```sh |
| | | node example.js --foo-bar |
| | | { _: [], 'foo-bar': true, fooBar: true } |
| | | ``` |
| | | |
| | | _if disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js --foo-bar |
| | | ```sh |
| | | node example.js --foo-bar |
| | | { _: [], 'foo-bar': true } |
| | | ``` |
| | | |
| | |
| | | |
| | | Should keys that contain `.` be treated as objects? |
| | | |
| | | ```console |
| | | $ node example.js --foo.bar |
| | | ```sh |
| | | node example.js --foo.bar |
| | | { _: [], foo: { bar: true } } |
| | | ``` |
| | | |
| | | _if disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js --foo.bar |
| | | ```sh |
| | | node example.js --foo.bar |
| | | { _: [], "foo.bar": true } |
| | | ``` |
| | | |
| | |
| | | |
| | | Should keys that look like numbers be treated as such? |
| | | |
| | | ```console |
| | | $ node example.js --foo=99.3 |
| | | ```sh |
| | | node example.js --foo=99.3 |
| | | { _: [], foo: 99.3 } |
| | | ``` |
| | | |
| | | _if disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js --foo=99.3 |
| | | ```sh |
| | | node example.js --foo=99.3 |
| | | { _: [], foo: "99.3" } |
| | | ``` |
| | | |
| | | ### parse positional numbers |
| | | |
| | | * default: `true` |
| | | * key: `parse-positional-numbers` |
| | | |
| | | Should positional keys that look like numbers be treated as such. |
| | | |
| | | ```console |
| | | $ node example.js 99.3 |
| | | { _: [99.3] } |
| | | ``` |
| | | |
| | | _if disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js 99.3 |
| | | { _: ['99.3'] } |
| | | ``` |
| | | |
| | | ### boolean negation |
| | |
| | | |
| | | Should variables prefixed with `--no` be treated as negations? |
| | | |
| | | ```console |
| | | $ node example.js --no-foo |
| | | ```sh |
| | | node example.js --no-foo |
| | | { _: [], foo: false } |
| | | ``` |
| | | |
| | | _if disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js --no-foo |
| | | ```sh |
| | | node example.js --no-foo |
| | | { _: [], "no-foo": true } |
| | | ``` |
| | | |
| | |
| | | |
| | | Should arguments be coerced into an array when duplicated: |
| | | |
| | | ```console |
| | | $ node example.js -x 1 -x 2 |
| | | ```sh |
| | | node example.js -x 1 -x 2 |
| | | { _: [], x: [1, 2] } |
| | | ``` |
| | | |
| | | _if disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js -x 1 -x 2 |
| | | ```sh |
| | | node example.js -x 1 -x 2 |
| | | { _: [], x: 2 } |
| | | ``` |
| | | |
| | |
| | | |
| | | Should array arguments be coerced into a single array when duplicated: |
| | | |
| | | ```console |
| | | $ node example.js -x 1 2 -x 3 4 |
| | | ```sh |
| | | node example.js -x 1 2 -x 3 4 |
| | | { _: [], x: [1, 2, 3, 4] } |
| | | ``` |
| | | |
| | | _if disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js -x 1 2 -x 3 4 |
| | | ```sh |
| | | node example.js -x 1 2 -x 3 4 |
| | | { _: [], x: [[1, 2], [3, 4]] } |
| | | ``` |
| | | |
| | |
| | | |
| | | Should arrays consume more than one positional argument following their flag. |
| | | |
| | | ```console |
| | | $ node example --arr 1 2 |
| | | { _: [], arr: [1, 2] } |
| | | ```sh |
| | | node example --arr 1 2 |
| | | { _[], arr: [1, 2] } |
| | | ``` |
| | | |
| | | _if disabled:_ |
| | | |
| | | ```console |
| | | $ node example --arr 1 2 |
| | | { _: [2], arr: [1] } |
| | | ```sh |
| | | node example --arr 1 2 |
| | | { _[2], arr: [1] } |
| | | ``` |
| | | |
| | | **Note: in `v18.0.0` we are considering defaulting greedy arrays to `false`.** |
| | |
| | | |
| | | The prefix to use for negated boolean variables. |
| | | |
| | | ```console |
| | | $ node example.js --no-foo |
| | | ```sh |
| | | node example.js --no-foo |
| | | { _: [], foo: false } |
| | | ``` |
| | | |
| | | _if set to `quux`:_ |
| | | |
| | | ```console |
| | | $ node example.js --quuxfoo |
| | | ```sh |
| | | node example.js --quuxfoo |
| | | { _: [], foo: false } |
| | | ``` |
| | | |
| | |
| | | |
| | | _If disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js a -b -- x y |
| | | ```sh |
| | | node example.js a -b -- x y |
| | | { _: [ 'a', 'x', 'y' ], b: true } |
| | | ``` |
| | | |
| | | _If enabled:_ |
| | | |
| | | ```console |
| | | $ node example.js a -b -- x y |
| | | ```sh |
| | | node example.js a -b -- x y |
| | | { _: [ 'a' ], '--': [ 'x', 'y' ], b: true } |
| | | ``` |
| | | |
| | |
| | | |
| | | _If disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js -a 1 -c 2 |
| | | ```sh |
| | | node example.js -a 1 -c 2 |
| | | { _: [], a: 1, c: 2 } |
| | | ``` |
| | | |
| | | _If enabled:_ |
| | | |
| | | ```console |
| | | $ node example.js -a 1 -c 2 |
| | | ```sh |
| | | node example.js -a 1 -c 2 |
| | | { _: [], a: 1, b: undefined, c: 2 } |
| | | ``` |
| | | |
| | |
| | | |
| | | _If disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js -a run b -x y |
| | | ```sh |
| | | node example.js -a run b -x y |
| | | { _: [ 'b' ], a: 'run', x: 'y' } |
| | | ``` |
| | | |
| | | _If enabled:_ |
| | | |
| | | ```console |
| | | $ node example.js -a run b -x y |
| | | ```sh |
| | | node example.js -a run b -x y |
| | | { _: [ 'b', '-x', 'y' ], a: 'run' } |
| | | ``` |
| | | |
| | |
| | | |
| | | _If disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js --test-field 1 |
| | | ```sh |
| | | node example.js --test-field 1 |
| | | { _: [], 'test-field': 1, testField: 1, 'test-alias': 1, testAlias: 1 } |
| | | ``` |
| | | |
| | | _If enabled:_ |
| | | |
| | | ```console |
| | | $ node example.js --test-field 1 |
| | | ```sh |
| | | node example.js --test-field 1 |
| | | { _: [], 'test-field': 1, testField: 1 } |
| | | ``` |
| | | |
| | |
| | | |
| | | _If disabled:_ |
| | | |
| | | ```console |
| | | $ node example.js --test-field 1 |
| | | ```sh |
| | | node example.js --test-field 1 |
| | | { _: [], 'test-field': 1, testField: 1 } |
| | | ``` |
| | | |
| | | _If enabled:_ |
| | | |
| | | ```console |
| | | $ node example.js --test-field 1 |
| | | ```sh |
| | | node example.js --test-field 1 |
| | | { _: [], testField: 1 } |
| | | ``` |
| | | |
| | |
| | | |
| | | _If disabled_ |
| | | |
| | | ```console |
| | | $ node example.js --unknown-option --known-option 2 --string-option --unknown-option2 |
| | | ```sh |
| | | node example.js --unknown-option --known-option 2 --string-option --unknown-option2 |
| | | { _: [], unknownOption: true, knownOption: 2, stringOption: '', unknownOption2: true } |
| | | ``` |
| | | |
| | | _If enabled_ |
| | | |
| | | ```console |
| | | $ node example.js --unknown-option --known-option 2 --string-option --unknown-option2 |
| | | ```sh |
| | | node example.js --unknown-option --known-option 2 --string-option --unknown-option2 |
| | | { _: ['--unknown-option'], knownOption: 2, stringOption: '--unknown-option2' } |
| | | ``` |
| | | |
| | | ## Supported Node.js Versions |
| | | |
| | | Libraries in this ecosystem make a best effort to track |
| | | [Node.js' release schedule](https://nodejs.org/en/about/releases/). Here's [a |
| | | post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a). |
| | | |
| | | ## Special Thanks |
| | | |