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