Installations
npm install yargs-parser
Score
95.8
Supply Chain
99.6
Quality
79.9
Maintenance
100
Vulnerability
100
License
Releases
yargs-parser: v21.1.1
Published on 04 Aug 2022
yargs-parser: v21.1.0
Published on 03 Aug 2022
yargs-parser: v21.0.1
Published on 27 Feb 2022
yargs-parser yargs-parser-v21.0.0
Published on 16 Nov 2021
yargs-parser yargs-parser-v20.2.9
Published on 20 Jun 2021
yargs-parser yargs-parser-v20.2.8
Published on 20 Jun 2021
Developer
Developer Guide
Module System
ESM
Min. Node Version
>=12
Typescript Support
No
Node Version
14.20.0
NPM Version
6.14.17
Statistics
496 Stars
361 Commits
118 Forks
10 Watching
39 Branches
58 Contributors
Updated on 19 Nov 2024
Languages
JavaScript (68.26%)
TypeScript (31.4%)
HTML (0.35%)
Total Downloads
Cumulative downloads
Total Downloads
19,507,668,666
Last day
-8.7%
19,686,478
Compared to previous day
Last week
1.3%
116,665,759
Compared to previous week
Last month
15.3%
474,887,586
Compared to previous month
Last year
7.4%
4,775,234,828
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
23
yargs-parser
The mighty option parser used by yargs.
visit the yargs website for more examples, and thorough usage instructions.
Example
1npm i yargs-parser --save
1const argv = require('yargs-parser')(process.argv.slice(2)) 2console.log(argv)
1$ node example.js --foo=33 --bar hello 2{ _: [], foo: 33, bar: 'hello' }
or parse a string!
1const argv = require('yargs-parser')('--foo=99 --bar=33') 2console.log(argv)
1{ _: [], foo: 99, bar: 33 }
Convert an array of mixed types before passing to yargs-parser
:
1const parse = require('yargs-parser') 2parse(['-f', 11, '--zoom', 55].join(' ')) // <-- array to string 3parse(['-f', 11, '--zoom', 55].map(String)) // <-- array of strings
Deno Example
As of v19
yargs-parser
supports Deno:
1import parser from "https://deno.land/x/yargs_parser/deno.ts"; 2 3const argv = parser('--foo=99 --bar=9987930', { 4 string: ['bar'] 5}) 6console.log(argv)
ESM Example
As of v19
yargs-parser
supports ESM (both in Node.js and in the browser):
Node.js:
1import parser from 'yargs-parser' 2 3const argv = parser('--foo=99 --bar=9987930', { 4 string: ['bar'] 5}) 6console.log(argv)
Browsers:
1<!doctype html> 2<body> 3 <script type="module"> 4 import parser from "https://unpkg.com/yargs-parser@19.0.0/browser.js"; 5 6 const argv = parser('--foo=99 --bar=9987930', { 7 string: ['bar'] 8 }) 9 console.log(argv) 10 </script> 11</body>
API
parser(args, opts={})
Parses command line arguments returning a simple mapping of keys and values.
expects:
args
: a string or array of strings representing the options to parse.opts
: provide a set of hints indicating howargs
should be parsed:opts.alias
: an object representing the set of aliases for a key:{alias: {foo: ['f']}}
.opts.array
: indicate that keys should be parsed as an array:{array: ['foo', 'bar']}
.
Indicate that keys should be parsed as an array and coerced to booleans / numbers:
{array: [{ key: 'foo', boolean: true }, {key: 'bar', number: true}]}
.opts.boolean
: arguments should be parsed as booleans:{boolean: ['x', 'y']}
.opts.coerce
: provide a custom synchronous function that returns a coerced value from the argument provided (or throws an error). For arrays the function is called only once for the entire array:
{coerce: {foo: function (arg) {return modifiedArg}}}
.opts.config
: indicate a key that represents a path to a configuration file (this file will be loaded and parsed).opts.configObjects
: configuration objects to parse, their properties will be set as arguments:
{configObjects: [{'x': 5, 'y': 33}, {'z': 44}]}
.opts.configuration
: provide configuration options to the yargs-parser (see: configuration).opts.count
: indicate a key that should be used as a counter, e.g.,-vvv
={v: 3}
.opts.default
: provide default values for keys:{default: {x: 33, y: 'hello world!'}}
.opts.envPrefix
: environment variables (process.env
) with the prefix provided should be parsed.opts.narg
: specify that a key requiresn
arguments:{narg: {x: 2}}
.opts.normalize
:path.normalize()
will be applied to values set to this key.opts.number
: keys should be treated as numbers.opts.string
: keys should be treated as strings (even if they resemble a number-x 33
).
returns:
obj
: an object representing the parsed value ofargs
key/value
: key value pairs for each argument and their aliases._
: an array representing the positional arguments.- [optional]
--
: an array with arguments after the end-of-options flag--
.
require('yargs-parser').detailed(args, opts={})
Parses a command line string, returning detailed information required by the yargs engine.
expects:
args
: a string or array of strings representing options to parse.opts
: provide a set of hints indicating howargs
, inputs are identical torequire('yargs-parser')(args, opts={})
.
returns:
argv
: an object representing the parsed value ofargs
key/value
: key value pairs for each argument and their aliases._
: an array representing the positional arguments.- [optional]
--
: an array with arguments after the end-of-options flag--
.
error
: populated with an error object if an exception occurred during parsing.aliases
: the inferred list of aliases built by combining lists inopts.alias
.newAliases
: any new aliases added via camel-case expansion:boolean
:{ fooBar: true }
defaulted
: any new argument created byopts.default
, no aliases included.boolean
:{ foo: true }
configuration
: given by default settings andopts.configuration
.
Configuration
The yargs-parser applies several automated transformations on the keys provided
in args
. These features can be turned on and off using the configuration
field
of opts
.
1var parsed = parser(['--no-dice'], { 2 configuration: { 3 'boolean-negation': false 4 } 5})
short option groups
- default:
true
. - key:
short-option-groups
.
Should a group of short-options be treated as boolean flags?
1$ node example.js -abc 2{ _: [], a: true, b: true, c: true }
if disabled:
1$ node example.js -abc 2{ _: [], abc: true }
camel-case expansion
- default:
true
. - key:
camel-case-expansion
.
Should hyphenated arguments be expanded into camel-case aliases?
1$ node example.js --foo-bar 2{ _: [], 'foo-bar': true, fooBar: true }
if disabled:
1$ node example.js --foo-bar 2{ _: [], 'foo-bar': true }
dot-notation
- default:
true
- key:
dot-notation
Should keys that contain .
be treated as objects?
1$ node example.js --foo.bar 2{ _: [], foo: { bar: true } }
if disabled:
1$ node example.js --foo.bar 2{ _: [], "foo.bar": true }
parse numbers
- default:
true
- key:
parse-numbers
Should keys that look like numbers be treated as such?
1$ node example.js --foo=99.3 2{ _: [], foo: 99.3 }
if disabled:
1$ node example.js --foo=99.3 2{ _: [], foo: "99.3" }
parse positional numbers
- default:
true
- key:
parse-positional-numbers
Should positional keys that look like numbers be treated as such.
1$ node example.js 99.3 2{ _: [99.3] }
if disabled:
1$ node example.js 99.3 2{ _: ['99.3'] }
boolean negation
- default:
true
- key:
boolean-negation
Should variables prefixed with --no
be treated as negations?
1$ node example.js --no-foo 2{ _: [], foo: false }
if disabled:
1$ node example.js --no-foo 2{ _: [], "no-foo": true }
combine arrays
- default:
false
- key:
combine-arrays
Should arrays be combined when provided by both command line arguments and a configuration file.
duplicate arguments array
- default:
true
- key:
duplicate-arguments-array
Should arguments be coerced into an array when duplicated:
1$ node example.js -x 1 -x 2 2{ _: [], x: [1, 2] }
if disabled:
1$ node example.js -x 1 -x 2 2{ _: [], x: 2 }
flatten duplicate arrays
- default:
true
- key:
flatten-duplicate-arrays
Should array arguments be coerced into a single array when duplicated:
1$ node example.js -x 1 2 -x 3 4 2{ _: [], x: [1, 2, 3, 4] }
if disabled:
1$ node example.js -x 1 2 -x 3 4 2{ _: [], x: [[1, 2], [3, 4]] }
greedy arrays
- default:
true
- key:
greedy-arrays
Should arrays consume more than one positional argument following their flag.
1$ node example --arr 1 2 2{ _: [], arr: [1, 2] }
if disabled:
1$ node example --arr 1 2 2{ _: [2], arr: [1] }
Note: in v18.0.0
we are considering defaulting greedy arrays to false
.
nargs eats options
- default:
false
- key:
nargs-eats-options
Should nargs consume dash options as well as positional arguments.
negation prefix
- default:
no-
- key:
negation-prefix
The prefix to use for negated boolean variables.
1$ node example.js --no-foo 2{ _: [], foo: false }
if set to quux
:
1$ node example.js --quuxfoo 2{ _: [], foo: false }
populate --
- default:
false
. - key:
populate--
Should unparsed flags be stored in --
or _
.
If disabled:
1$ node example.js a -b -- x y 2{ _: [ 'a', 'x', 'y' ], b: true }
If enabled:
1$ node example.js a -b -- x y 2{ _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
set placeholder key
- default:
false
. - key:
set-placeholder-key
.
Should a placeholder be added for keys not set via the corresponding CLI argument?
If disabled:
1$ node example.js -a 1 -c 2 2{ _: [], a: 1, c: 2 }
If enabled:
1$ node example.js -a 1 -c 2 2{ _: [], a: 1, b: undefined, c: 2 }
halt at non-option
- default:
false
. - key:
halt-at-non-option
.
Should parsing stop at the first positional argument? This is similar to how e.g. ssh
parses its command line.
If disabled:
1$ node example.js -a run b -x y 2{ _: [ 'b' ], a: 'run', x: 'y' }
If enabled:
1$ node example.js -a run b -x y 2{ _: [ 'b', '-x', 'y' ], a: 'run' }
strip aliased
- default:
false
- key:
strip-aliased
Should aliases be removed before returning results?
If disabled:
1$ node example.js --test-field 1 2{ _: [], 'test-field': 1, testField: 1, 'test-alias': 1, testAlias: 1 }
If enabled:
1$ node example.js --test-field 1 2{ _: [], 'test-field': 1, testField: 1 }
strip dashed
- default:
false
- key:
strip-dashed
Should dashed keys be removed before returning results? This option has no effect if
camel-case-expansion
is disabled.
If disabled:
1$ node example.js --test-field 1 2{ _: [], 'test-field': 1, testField: 1 }
If enabled:
1$ node example.js --test-field 1 2{ _: [], testField: 1 }
unknown options as args
- default:
false
- key:
unknown-options-as-args
Should unknown options be treated like regular arguments? An unknown option is one that is not
configured in opts
.
If disabled
1$ node example.js --unknown-option --known-option 2 --string-option --unknown-option2 2{ _: [], unknownOption: true, knownOption: 2, stringOption: '', unknownOption2: true }
If enabled
1$ node example.js --unknown-option --known-option 2 --string-option --unknown-option2 2{ _: ['--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. Here's a post on why we think this is important.
Special Thanks
The yargs project evolves from optimist and minimist. It owes its existence to a lot of James Halliday's hard work. Thanks substack beep boop \o/
License
ISC
Stable Version
The latest stable version of the package.
Stable Version
21.1.1
MODERATE
4
5.3/10
Summary
yargs-parser Vulnerable to Prototype Pollution
Affected Versions
>= 16.0.0, < 18.1.1
Patched Versions
18.1.1
5.3/10
Summary
yargs-parser Vulnerable to Prototype Pollution
Affected Versions
<= 5.0.0
Patched Versions
5.0.1
5.3/10
Summary
yargs-parser Vulnerable to Prototype Pollution
Affected Versions
>= 6.0.0, < 13.1.2
Patched Versions
13.1.2
5.3/10
Summary
yargs-parser Vulnerable to Prototype Pollution
Affected Versions
>= 14.0.0, < 15.0.1
Patched Versions
15.0.1
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE.txt:0
- Info: FSF or OSI recognized license: ISC License: LICENSE.txt:0
Reason
Found 8/15 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yaml:1
- Warn: no topLevel permission defined: .github/workflows/release-please.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:70: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:71: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:79: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:80: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:88: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:89: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:39: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:40: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:51: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:52: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yaml:59: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/ci.yaml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release-please.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/release-please.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release-please.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/release-please.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release-please.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/release-please.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release-please.yml:36: update your workflow using https://app.stepsecurity.io/secureworkflow/yargs/yargs-parser/release-please.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yaml:44
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yaml:56
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yaml:75
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yaml:84
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yaml:93
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yaml:21
- Warn: npmCommand not pinned by hash: .github/workflows/release-please.yml:21
- Warn: npmCommand not pinned by hash: .github/workflows/release-please.yml:42
- Info: 0 out of 17 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 0 out of 8 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 25 are checked with a SAST tool
Score
4.3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to yargs-parser
@types/yargs-parser
TypeScript definitions for yargs-parser
yargs-promise
Use the headless yargs parser with promises
base-argv
Plugin that post-processes the object returned from [yargs-parser] so that values can be passed over to base-cli
process-yargs-parser
Lightweight Node.js arguments parser with 0 Dependencies 🚀. **process-yargs-parser** is an opinionated yargs-parser with many needless yargs-parser configurations disabled by default.