minimist 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +25 -50
- package/CHANGELOG.md +78 -1
- package/README.md +15 -11
- package/example/parse.js +2 -0
- package/index.js +215 -201
- package/package.json +73 -72
- package/test/all_bool.js +26 -24
- package/test/bool.js +99 -99
- package/test/dash.js +33 -21
- package/test/default_bool.js +14 -12
- package/test/dotted.js +13 -11
- package/test/long.js +28 -26
- package/test/num.js +30 -28
- package/test/parse.js +169 -157
- package/test/parse_modified.js +7 -5
- package/test/proto.js +45 -27
- package/test/short.js +57 -55
- package/test/whitespace.js +6 -4
package/.eslintrc
CHANGED
|
@@ -1,54 +1,29 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
2
|
+
"root": true,
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
"extends": "@ljharb/eslint-config/node/0.4",
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"no-mixed-operators": 1,
|
|
30
|
-
"no-multi-spaces": 1,
|
|
31
|
-
"no-multiple-empty-lines": 1,
|
|
32
|
-
"no-param-reassign": 1,
|
|
33
|
-
"no-plusplus": 1,
|
|
34
|
-
"no-proto": 1,
|
|
35
|
-
"no-redeclare": 1,
|
|
36
|
-
"no-restricted-syntax": 1,
|
|
37
|
-
"no-shadow": 1,
|
|
38
|
-
"no-trailing-spaces": 1,
|
|
39
|
-
"no-unused-vars": 1,
|
|
40
|
-
"no-use-before-define": 1,
|
|
41
|
-
"object-curly-newline": 1,
|
|
42
|
-
"object-curly-spacing": 1,
|
|
43
|
-
"operator-linebreak": 1,
|
|
44
|
-
"quote-props": 1,
|
|
45
|
-
"quotes": 1,
|
|
46
|
-
"semi-style": 1,
|
|
47
|
-
"semi": 1,
|
|
48
|
-
"space-before-blocks": 1,
|
|
49
|
-
"space-before-function-paren": 1,
|
|
50
|
-
"space-infix-ops": 1,
|
|
51
|
-
"strict": 1,
|
|
52
|
-
"wrap-regex": 1,
|
|
53
|
-
},
|
|
6
|
+
"rules": {
|
|
7
|
+
"array-element-newline": 0,
|
|
8
|
+
"complexity": 0,
|
|
9
|
+
"func-style": [2, "declaration"],
|
|
10
|
+
"max-lines-per-function": 0,
|
|
11
|
+
"max-nested-callbacks": 1,
|
|
12
|
+
"max-statements-per-line": 1,
|
|
13
|
+
"max-statements": 0,
|
|
14
|
+
"multiline-comment-style": 0,
|
|
15
|
+
"no-continue": 1,
|
|
16
|
+
"no-param-reassign": 1,
|
|
17
|
+
"no-restricted-syntax": 1,
|
|
18
|
+
"object-curly-newline": 0,
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
"overrides": [
|
|
22
|
+
{
|
|
23
|
+
"files": "test/**",
|
|
24
|
+
"rules": {
|
|
25
|
+
"camelcase": 0,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
]
|
|
54
29
|
}
|
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,52 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [v1.2.8](https://github.com/minimistjs/minimist/compare/v1.2.7...v1.2.8) - 2023-02-09
|
|
9
|
+
|
|
10
|
+
### Merged
|
|
11
|
+
|
|
12
|
+
- [Fix] Fix long option followed by single dash [`#17`](https://github.com/minimistjs/minimist/pull/17)
|
|
13
|
+
- [Tests] Remove duplicate test [`#12`](https://github.com/minimistjs/minimist/pull/12)
|
|
14
|
+
- [Fix] opt.string works with multiple aliases [`#10`](https://github.com/minimistjs/minimist/pull/10)
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- [Fix] Fix long option followed by single dash (#17) [`#15`](https://github.com/minimistjs/minimist/issues/15)
|
|
19
|
+
- [Tests] Remove duplicate test (#12) [`#8`](https://github.com/minimistjs/minimist/issues/8)
|
|
20
|
+
- [Fix] Fix long option followed by single dash [`#15`](https://github.com/minimistjs/minimist/issues/15)
|
|
21
|
+
- [Fix] opt.string works with multiple aliases (#10) [`#9`](https://github.com/minimistjs/minimist/issues/9)
|
|
22
|
+
- [Fix] Fix handling of short option with non-trivial equals [`#5`](https://github.com/minimistjs/minimist/issues/5)
|
|
23
|
+
- [Tests] Remove duplicate test [`#8`](https://github.com/minimistjs/minimist/issues/8)
|
|
24
|
+
- [Fix] opt.string works with multiple aliases [`#9`](https://github.com/minimistjs/minimist/issues/9)
|
|
25
|
+
|
|
26
|
+
### Commits
|
|
27
|
+
|
|
28
|
+
- Merge tag 'v0.2.3' [`a026794`](https://github.com/minimistjs/minimist/commit/a0267947c7870fc5847cf2d437fbe33f392767da)
|
|
29
|
+
- [eslint] fix indentation and whitespace [`5368ca4`](https://github.com/minimistjs/minimist/commit/5368ca4147e974138a54cc0dc4cea8f756546b70)
|
|
30
|
+
- [eslint] fix indentation and whitespace [`e5f5067`](https://github.com/minimistjs/minimist/commit/e5f5067259ceeaf0b098d14bec910f87e58708c7)
|
|
31
|
+
- [eslint] more cleanup [`62fde7d`](https://github.com/minimistjs/minimist/commit/62fde7d935f83417fb046741531a9e2346a36976)
|
|
32
|
+
- [eslint] more cleanup [`36ac5d0`](https://github.com/minimistjs/minimist/commit/36ac5d0d95e4947d074e5737d94814034ca335d1)
|
|
33
|
+
- [meta] add `auto-changelog` [`73923d2`](https://github.com/minimistjs/minimist/commit/73923d223553fca08b1ba77e3fbc2a492862ae4c)
|
|
34
|
+
- [actions] add reusable workflows [`d80727d`](https://github.com/minimistjs/minimist/commit/d80727df77bfa9e631044d7f16368d8f09242c91)
|
|
35
|
+
- [eslint] add eslint; rules to enable later are warnings [`48bc06a`](https://github.com/minimistjs/minimist/commit/48bc06a1b41f00e9cdf183db34f7a51ba70e98d4)
|
|
36
|
+
- [eslint] fix indentation [`34b0f1c`](https://github.com/minimistjs/minimist/commit/34b0f1ccaa45183c3c4f06a91f9b405180a6f982)
|
|
37
|
+
- [readme] rename and add badges [`5df0fe4`](https://github.com/minimistjs/minimist/commit/5df0fe49211bd09a3636f8686a7cb3012c3e98f0)
|
|
38
|
+
- [Dev Deps] switch from `covert` to `nyc` [`a48b128`](https://github.com/minimistjs/minimist/commit/a48b128fdb8d427dfb20a15273f83e38d97bef07)
|
|
39
|
+
- [Dev Deps] update `covert`, `tape`; remove unnecessary `tap` [`f0fb958`](https://github.com/minimistjs/minimist/commit/f0fb958e9a1fe980cdffc436a211b0bda58f621b)
|
|
40
|
+
- [meta] create FUNDING.yml; add `funding` in package.json [`3639e0c`](https://github.com/minimistjs/minimist/commit/3639e0c819359a366387e425ab6eabf4c78d3caa)
|
|
41
|
+
- [meta] use `npmignore` to autogenerate an npmignore file [`be2e038`](https://github.com/minimistjs/minimist/commit/be2e038c342d8333b32f0fde67a0026b79c8150e)
|
|
42
|
+
- Only apps should have lockfiles [`282b570`](https://github.com/minimistjs/minimist/commit/282b570e7489d01b03f2d6d3dabf79cd3e5f84cf)
|
|
43
|
+
- isConstructorOrProto adapted from PR [`ef9153f`](https://github.com/minimistjs/minimist/commit/ef9153fc52b6cea0744b2239921c5dcae4697f11)
|
|
44
|
+
- [Dev Deps] update `@ljharb/eslint-config`, `aud` [`098873c`](https://github.com/minimistjs/minimist/commit/098873c213cdb7c92e55ae1ef5aa1af3a8192a79)
|
|
45
|
+
- [Dev Deps] update `@ljharb/eslint-config`, `aud` [`3124ed3`](https://github.com/minimistjs/minimist/commit/3124ed3e46306301ebb3c834874ce0241555c2c4)
|
|
46
|
+
- [meta] add `safe-publish-latest` [`4b927de`](https://github.com/minimistjs/minimist/commit/4b927de696d561c636b4f43bf49d4597cb36d6d6)
|
|
47
|
+
- [Tests] add `aud` in `posttest` [`b32d9bd`](https://github.com/minimistjs/minimist/commit/b32d9bd0ab340f4e9f8c3a97ff2a4424f25fab8c)
|
|
48
|
+
- [meta] update repo URLs [`f9fdfc0`](https://github.com/minimistjs/minimist/commit/f9fdfc032c54884d9a9996a390c63cd0719bbe1a)
|
|
49
|
+
- [actions] Avoid 0.6 tests due to build failures [`ba92fe6`](https://github.com/minimistjs/minimist/commit/ba92fe6ebbdc0431cca9a2ea8f27beb492f5e4ec)
|
|
50
|
+
- [Dev Deps] update `tape` [`950eaa7`](https://github.com/minimistjs/minimist/commit/950eaa74f112e04d23e9c606c67472c46739b473)
|
|
51
|
+
- [Dev Deps] add missing `npmignore` dev dep [`3226afa`](https://github.com/minimistjs/minimist/commit/3226afaf09e9d127ca369742437fe6e88f752d6b)
|
|
52
|
+
- Merge tag 'v0.2.2' [`980d7ac`](https://github.com/minimistjs/minimist/commit/980d7ac61a0b4bd552711251ac107d506b23e41f)
|
|
53
|
+
|
|
8
54
|
## [v1.2.7](https://github.com/minimistjs/minimist/compare/v1.2.6...v1.2.7) - 2022-10-10
|
|
9
55
|
|
|
10
56
|
### Commits
|
|
@@ -109,13 +155,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
109
155
|
- coverage script [`e5531ba`](https://github.com/minimistjs/minimist/commit/e5531ba0479da3b8138d3d8cac545d84ccb1c8df)
|
|
110
156
|
- extra fn to get 100% coverage again [`a6972da`](https://github.com/minimistjs/minimist/commit/a6972da89e56bf77642f8ec05a13b6558db93498)
|
|
111
157
|
|
|
112
|
-
## [v1.0.0](https://github.com/minimistjs/minimist/compare/v0.2.
|
|
158
|
+
## [v1.0.0](https://github.com/minimistjs/minimist/compare/v0.2.4...v1.0.0) - 2014-08-10
|
|
113
159
|
|
|
114
160
|
### Commits
|
|
115
161
|
|
|
116
162
|
- added stopEarly option [`471c7e4`](https://github.com/minimistjs/minimist/commit/471c7e4a7e910fc7ad8f9df850a186daf32c64e9)
|
|
117
163
|
- fix list [`fef6ae7`](https://github.com/minimistjs/minimist/commit/fef6ae79c38b9dc1c49569abb7cd04eb965eac5e)
|
|
118
164
|
|
|
165
|
+
## [v0.2.4](https://github.com/minimistjs/minimist/compare/v0.2.3...v0.2.4) - 2023-02-24
|
|
166
|
+
|
|
167
|
+
### Commits
|
|
168
|
+
|
|
169
|
+
- [Tests] check side-effects of pollution protection [`3dbebff`](https://github.com/minimistjs/minimist/commit/3dbebff0f4436cf946cb6c1e6db606f4b9bded4f)
|
|
170
|
+
- [Robustness] rework isConstructorOrProto [`34e20b8`](https://github.com/minimistjs/minimist/commit/34e20b8461118608703d6485326abbb8e35e1703)
|
|
171
|
+
- [Dev Deps] update `tape` [`d031f9b`](https://github.com/minimistjs/minimist/commit/d031f9bf57a620b834ab5a8f1b4e027b67904a60)
|
|
172
|
+
|
|
173
|
+
## [v0.2.3](https://github.com/minimistjs/minimist/compare/v0.2.2...v0.2.3) - 2023-02-09
|
|
174
|
+
|
|
175
|
+
### Merged
|
|
176
|
+
|
|
177
|
+
- [Fix] Fix long option followed by single dash [`#17`](https://github.com/minimistjs/minimist/pull/17)
|
|
178
|
+
- [Tests] Remove duplicate test [`#12`](https://github.com/minimistjs/minimist/pull/12)
|
|
179
|
+
- [Fix] opt.string works with multiple aliases [`#10`](https://github.com/minimistjs/minimist/pull/10)
|
|
180
|
+
|
|
181
|
+
### Fixed
|
|
182
|
+
|
|
183
|
+
- [Fix] Fix long option followed by single dash (#17) [`#15`](https://github.com/minimistjs/minimist/issues/15)
|
|
184
|
+
- [Tests] Remove duplicate test (#12) [`#8`](https://github.com/minimistjs/minimist/issues/8)
|
|
185
|
+
- [Fix] opt.string works with multiple aliases (#10) [`#9`](https://github.com/minimistjs/minimist/issues/9)
|
|
186
|
+
|
|
187
|
+
### Commits
|
|
188
|
+
|
|
189
|
+
- [eslint] fix indentation and whitespace [`e5f5067`](https://github.com/minimistjs/minimist/commit/e5f5067259ceeaf0b098d14bec910f87e58708c7)
|
|
190
|
+
- [eslint] more cleanup [`36ac5d0`](https://github.com/minimistjs/minimist/commit/36ac5d0d95e4947d074e5737d94814034ca335d1)
|
|
191
|
+
- [eslint] fix indentation [`34b0f1c`](https://github.com/minimistjs/minimist/commit/34b0f1ccaa45183c3c4f06a91f9b405180a6f982)
|
|
192
|
+
- isConstructorOrProto adapted from PR [`ef9153f`](https://github.com/minimistjs/minimist/commit/ef9153fc52b6cea0744b2239921c5dcae4697f11)
|
|
193
|
+
- [Dev Deps] update `@ljharb/eslint-config`, `aud` [`098873c`](https://github.com/minimistjs/minimist/commit/098873c213cdb7c92e55ae1ef5aa1af3a8192a79)
|
|
194
|
+
- [Dev Deps] add missing `npmignore` dev dep [`3226afa`](https://github.com/minimistjs/minimist/commit/3226afaf09e9d127ca369742437fe6e88f752d6b)
|
|
195
|
+
|
|
119
196
|
## [v0.2.2](https://github.com/minimistjs/minimist/compare/v0.2.1...v0.2.2) - 2022-10-10
|
|
120
197
|
|
|
121
198
|
### Commits
|
package/README.md
CHANGED
|
@@ -30,14 +30,16 @@ $ node example/parse.js -a beep -b boop
|
|
|
30
30
|
|
|
31
31
|
```
|
|
32
32
|
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
|
|
33
|
-
{
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
{
|
|
34
|
+
_: ['foo', 'bar', 'baz'],
|
|
35
|
+
x: 3,
|
|
36
|
+
y: 4,
|
|
37
|
+
n: 5,
|
|
38
|
+
a: true,
|
|
39
|
+
b: true,
|
|
40
|
+
c: true,
|
|
41
|
+
beep: 'boop'
|
|
42
|
+
}
|
|
41
43
|
```
|
|
42
44
|
|
|
43
45
|
# methods
|
|
@@ -71,10 +73,12 @@ argument names to use as aliases
|
|
|
71
73
|
* `opts['--']` - when true, populate `argv._` with everything before the `--`
|
|
72
74
|
and `argv['--']` with everything after the `--`. Here's an example:
|
|
73
75
|
|
|
74
|
-
```
|
|
76
|
+
```sh
|
|
75
77
|
> require('./')('one two three -- four five --six'.split(' '), { '--': true })
|
|
76
|
-
{
|
|
77
|
-
|
|
78
|
+
{
|
|
79
|
+
_: ['one', 'two', 'three'],
|
|
80
|
+
'--': ['four', 'five', '--six'],
|
|
81
|
+
}
|
|
78
82
|
```
|
|
79
83
|
|
|
80
84
|
Note that with `opts['--']` set, parsing for arguments still stops after the
|
package/example/parse.js
CHANGED
package/index.js
CHANGED
|
@@ -1,209 +1,223 @@
|
|
|
1
|
-
|
|
2
|
-
if (!opts) opts = {};
|
|
3
|
-
|
|
4
|
-
var flags = { bools : {}, strings : {} };
|
|
5
|
-
|
|
6
|
-
if (typeof opts['boolean'] === 'boolean' && opts['boolean']) {
|
|
7
|
-
flags.allBools = true;
|
|
8
|
-
} else {
|
|
9
|
-
[].concat(opts['boolean']).filter(Boolean).forEach(function (key) {
|
|
10
|
-
flags.bools[key] = true;
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
var aliases = {};
|
|
15
|
-
Object.keys(opts.alias || {}).forEach(function (key) {
|
|
16
|
-
aliases[key] = [].concat(opts.alias[key]);
|
|
17
|
-
aliases[key].forEach(function (x) {
|
|
18
|
-
aliases[x] = [key].concat(aliases[key].filter(function (y) {
|
|
19
|
-
return x !== y;
|
|
20
|
-
}));
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
[].concat(opts.string).filter(Boolean).forEach(function (key) {
|
|
25
|
-
flags.strings[key] = true;
|
|
26
|
-
if (aliases[key]) {
|
|
27
|
-
flags.strings[aliases[key]] = true;
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
var defaults = opts['default'] || {};
|
|
32
|
-
|
|
33
|
-
var argv = { _ : [] };
|
|
34
|
-
Object.keys(flags.bools).forEach(function (key) {
|
|
35
|
-
setArg(key, defaults[key] === undefined ? false : defaults[key]);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
var notFlags = [];
|
|
39
|
-
|
|
40
|
-
if (args.indexOf('--') !== -1) {
|
|
41
|
-
notFlags = args.slice(args.indexOf('--')+1);
|
|
42
|
-
args = args.slice(0, args.indexOf('--'));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function setArg (key, val) {
|
|
46
|
-
var value = !flags.strings[key] && isNumber(val)
|
|
47
|
-
? Number(val) : val
|
|
48
|
-
;
|
|
49
|
-
setKey(argv, key.split('.'), value);
|
|
50
|
-
|
|
51
|
-
(aliases[key] || []).forEach(function (x) {
|
|
52
|
-
setKey(argv, x.split('.'), value);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
for (var i = 0; i < args.length; i++) {
|
|
57
|
-
var arg = args[i];
|
|
58
|
-
|
|
59
|
-
if (/^--.+=/.test(arg)) {
|
|
60
|
-
// Using [\s\S] instead of . because js doesn't support the
|
|
61
|
-
// 'dotall' regex modifier. See:
|
|
62
|
-
// http://stackoverflow.com/a/1068308/13216
|
|
63
|
-
var m = arg.match(/^--([^=]+)=([\s\S]*)$/);
|
|
64
|
-
setArg(m[1], m[2]);
|
|
65
|
-
}
|
|
66
|
-
else if (/^--no-.+/.test(arg)) {
|
|
67
|
-
var key = arg.match(/^--no-(.+)/)[1];
|
|
68
|
-
setArg(key, false);
|
|
69
|
-
}
|
|
70
|
-
else if (/^--.+/.test(arg)) {
|
|
71
|
-
var key = arg.match(/^--(.+)/)[1];
|
|
72
|
-
var next = args[i + 1];
|
|
73
|
-
if (next !== undefined && !/^-/.test(next)
|
|
74
|
-
&& !flags.bools[key]
|
|
75
|
-
&& !flags.allBools
|
|
76
|
-
&& (aliases[key] ? !flags.bools[aliases[key]] : true)) {
|
|
77
|
-
setArg(key, next);
|
|
78
|
-
i++;
|
|
79
|
-
}
|
|
80
|
-
else if (/^(true|false)$/.test(next)) {
|
|
81
|
-
setArg(key, next === 'true');
|
|
82
|
-
i++;
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
setArg(key, flags.strings[key] ? '' : true);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
else if (/^-[^-]+/.test(arg)) {
|
|
89
|
-
var letters = arg.slice(1,-1).split('');
|
|
90
|
-
|
|
91
|
-
var broken = false;
|
|
92
|
-
for (var j = 0; j < letters.length; j++) {
|
|
93
|
-
var next = arg.slice(j+2);
|
|
94
|
-
|
|
95
|
-
if (next === '-') {
|
|
96
|
-
setArg(letters[j], next)
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (/[A-Za-z]/.test(letters[j])
|
|
101
|
-
&& /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
|
|
102
|
-
setArg(letters[j], next);
|
|
103
|
-
broken = true;
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (letters[j+1] && letters[j+1].match(/\W/)) {
|
|
108
|
-
setArg(letters[j], arg.slice(j+2));
|
|
109
|
-
broken = true;
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
setArg(letters[j], flags.strings[letters[j]] ? '' : true);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
var key = arg.slice(-1)[0];
|
|
118
|
-
if (!broken && key !== '-') {
|
|
119
|
-
if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
|
|
120
|
-
&& !flags.bools[key]
|
|
121
|
-
&& (aliases[key] ? !flags.bools[aliases[key]] : true)) {
|
|
122
|
-
setArg(key, args[i+1]);
|
|
123
|
-
i++;
|
|
124
|
-
}
|
|
125
|
-
else if (args[i+1] && /true|false/.test(args[i+1])) {
|
|
126
|
-
setArg(key, args[i+1] === 'true');
|
|
127
|
-
i++;
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
setArg(key, flags.strings[key] ? '' : true);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
argv._.push(
|
|
136
|
-
flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
Object.keys(defaults).forEach(function (key) {
|
|
142
|
-
if (!hasKey(argv, key.split('.'))) {
|
|
143
|
-
setKey(argv, key.split('.'), defaults[key]);
|
|
144
|
-
|
|
145
|
-
(aliases[key] || []).forEach(function (x) {
|
|
146
|
-
setKey(argv, x.split('.'), defaults[key]);
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
if (opts['--']) {
|
|
152
|
-
argv['--'] = new Array();
|
|
153
|
-
notFlags.forEach(function(key) {
|
|
154
|
-
argv['--'].push(key);
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
notFlags.forEach(function(key) {
|
|
159
|
-
argv._.push(key);
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return argv;
|
|
164
|
-
};
|
|
1
|
+
'use strict';
|
|
165
2
|
|
|
166
|
-
function
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
3
|
+
function isNumber(x) {
|
|
4
|
+
if (typeof x === 'number') { return true; }
|
|
5
|
+
if ((/^0x[0-9a-f]+$/i).test(x)) { return true; }
|
|
6
|
+
return (/^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/).test(x);
|
|
7
|
+
}
|
|
171
8
|
|
|
172
|
-
|
|
173
|
-
|
|
9
|
+
function isConstructorOrProto(obj, key) {
|
|
10
|
+
return (key === 'constructor' && typeof obj[key] === 'function') || key === '__proto__';
|
|
174
11
|
}
|
|
175
12
|
|
|
176
|
-
function
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (o[key] === Array.prototype) o[key] = [];
|
|
185
|
-
o = o[key];
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
var key = keys[keys.length - 1];
|
|
189
|
-
if (key === '__proto__') return;
|
|
190
|
-
if (o === Object.prototype || o === Number.prototype
|
|
191
|
-
|| o === String.prototype) o = {};
|
|
192
|
-
if (o === Array.prototype) o = [];
|
|
193
|
-
if (o[key] === undefined || typeof o[key] === 'boolean') {
|
|
194
|
-
o[key] = value;
|
|
195
|
-
}
|
|
196
|
-
else if (Array.isArray(o[key])) {
|
|
197
|
-
o[key].push(value);
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
o[key] = [ o[key], value ];
|
|
201
|
-
}
|
|
13
|
+
function hasKey(obj, keys) {
|
|
14
|
+
var o = obj;
|
|
15
|
+
keys.slice(0, -1).forEach(function (key) {
|
|
16
|
+
o = o[key] || {};
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
var key = keys[keys.length - 1];
|
|
20
|
+
return key in o;
|
|
202
21
|
}
|
|
203
22
|
|
|
204
|
-
function
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
23
|
+
function setKey(obj, keys, value) {
|
|
24
|
+
var o = obj;
|
|
25
|
+
var key;
|
|
26
|
+
for (var i = 0; i < keys.length - 1; i++) {
|
|
27
|
+
key = keys[i];
|
|
28
|
+
if (isConstructorOrProto(o, key)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (o[key] === undefined) { o[key] = {}; }
|
|
32
|
+
if (
|
|
33
|
+
o[key] === Object.prototype
|
|
34
|
+
|| o[key] === Number.prototype
|
|
35
|
+
|| o[key] === String.prototype
|
|
36
|
+
) {
|
|
37
|
+
o[key] = {};
|
|
38
|
+
}
|
|
39
|
+
if (o[key] === Array.prototype) { o[key] = []; }
|
|
40
|
+
o = o[key];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
key = keys[keys.length - 1];
|
|
44
|
+
if (isConstructorOrProto(o, key)) { return; }
|
|
45
|
+
if (
|
|
46
|
+
o === Object.prototype
|
|
47
|
+
|| o === Number.prototype
|
|
48
|
+
|| o === String.prototype
|
|
49
|
+
) {
|
|
50
|
+
o = {};
|
|
51
|
+
}
|
|
52
|
+
if (o === Array.prototype) { o = []; }
|
|
53
|
+
if (o[key] === undefined || typeof o[key] === 'boolean') {
|
|
54
|
+
o[key] = value;
|
|
55
|
+
} else if (Array.isArray(o[key])) {
|
|
56
|
+
o[key].push(value);
|
|
57
|
+
} else {
|
|
58
|
+
o[key] = [o[key], value];
|
|
59
|
+
}
|
|
208
60
|
}
|
|
209
61
|
|
|
62
|
+
module.exports = function (args, opts) {
|
|
63
|
+
if (!opts) { opts = {}; }
|
|
64
|
+
|
|
65
|
+
var flags = { bools: {}, strings: {} };
|
|
66
|
+
|
|
67
|
+
if (typeof opts.boolean === 'boolean' && opts.boolean) {
|
|
68
|
+
flags.allBools = true;
|
|
69
|
+
} else {
|
|
70
|
+
[].concat(opts.boolean).filter(Boolean).forEach(function (key) {
|
|
71
|
+
flags.bools[key] = true;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
var aliases = {};
|
|
76
|
+
Object.keys(opts.alias || {}).forEach(function (key) {
|
|
77
|
+
aliases[key] = [].concat(opts.alias[key]);
|
|
78
|
+
aliases[key].forEach(function (x) {
|
|
79
|
+
aliases[x] = [key].concat(aliases[key].filter(function (y) {
|
|
80
|
+
return x !== y;
|
|
81
|
+
}));
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
[].concat(opts.string).filter(Boolean).forEach(function (key) {
|
|
86
|
+
flags.strings[key] = true;
|
|
87
|
+
if (aliases[key]) {
|
|
88
|
+
[].concat(aliases[key]).forEach(function (k) {
|
|
89
|
+
flags.strings[k] = true;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
var defaults = opts.default || {};
|
|
95
|
+
|
|
96
|
+
var argv = { _: [] };
|
|
97
|
+
|
|
98
|
+
function setArg(key, val) {
|
|
99
|
+
var value = !flags.strings[key] && isNumber(val)
|
|
100
|
+
? Number(val)
|
|
101
|
+
: val;
|
|
102
|
+
setKey(argv, key.split('.'), value);
|
|
103
|
+
|
|
104
|
+
(aliases[key] || []).forEach(function (x) {
|
|
105
|
+
setKey(argv, x.split('.'), value);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
Object.keys(flags.bools).forEach(function (key) {
|
|
110
|
+
setArg(key, defaults[key] === undefined ? false : defaults[key]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
var notFlags = [];
|
|
114
|
+
|
|
115
|
+
if (args.indexOf('--') !== -1) {
|
|
116
|
+
notFlags = args.slice(args.indexOf('--') + 1);
|
|
117
|
+
args = args.slice(0, args.indexOf('--'));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
for (var i = 0; i < args.length; i++) {
|
|
121
|
+
var arg = args[i];
|
|
122
|
+
var key;
|
|
123
|
+
var next;
|
|
124
|
+
|
|
125
|
+
if ((/^--.+=/).test(arg)) {
|
|
126
|
+
// Using [\s\S] instead of . because js doesn't support the
|
|
127
|
+
// 'dotall' regex modifier. See:
|
|
128
|
+
// http://stackoverflow.com/a/1068308/13216
|
|
129
|
+
var m = arg.match(/^--([^=]+)=([\s\S]*)$/);
|
|
130
|
+
setArg(m[1], m[2]);
|
|
131
|
+
} else if ((/^--no-.+/).test(arg)) {
|
|
132
|
+
key = arg.match(/^--no-(.+)/)[1];
|
|
133
|
+
setArg(key, false);
|
|
134
|
+
} else if ((/^--.+/).test(arg)) {
|
|
135
|
+
key = arg.match(/^--(.+)/)[1];
|
|
136
|
+
next = args[i + 1];
|
|
137
|
+
if (
|
|
138
|
+
next !== undefined
|
|
139
|
+
&& !(/^(-|--)[^-]/).test(next)
|
|
140
|
+
&& !flags.bools[key]
|
|
141
|
+
&& !flags.allBools
|
|
142
|
+
&& (aliases[key] ? !flags.bools[aliases[key]] : true)
|
|
143
|
+
) {
|
|
144
|
+
setArg(key, next);
|
|
145
|
+
i += 1;
|
|
146
|
+
} else if ((/^(true|false)$/).test(next)) {
|
|
147
|
+
setArg(key, next === 'true');
|
|
148
|
+
i += 1;
|
|
149
|
+
} else {
|
|
150
|
+
setArg(key, flags.strings[key] ? '' : true);
|
|
151
|
+
}
|
|
152
|
+
} else if ((/^-[^-]+/).test(arg)) {
|
|
153
|
+
var letters = arg.slice(1, -1).split('');
|
|
154
|
+
|
|
155
|
+
var broken = false;
|
|
156
|
+
for (var j = 0; j < letters.length; j++) {
|
|
157
|
+
next = arg.slice(j + 2);
|
|
158
|
+
|
|
159
|
+
if (next === '-') {
|
|
160
|
+
setArg(letters[j], next);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (
|
|
165
|
+
(/[A-Za-z]/).test(letters[j])
|
|
166
|
+
&& (/-?\d+(\.\d*)?(e-?\d+)?$/).test(next)
|
|
167
|
+
) {
|
|
168
|
+
setArg(letters[j], next);
|
|
169
|
+
broken = true;
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (letters[j + 1] && letters[j + 1].match(/\W/)) {
|
|
174
|
+
setArg(letters[j], arg.slice(j + 2));
|
|
175
|
+
broken = true;
|
|
176
|
+
break;
|
|
177
|
+
} else {
|
|
178
|
+
setArg(letters[j], flags.strings[letters[j]] ? '' : true);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
key = arg.slice(-1)[0];
|
|
183
|
+
if (!broken && key !== '-') {
|
|
184
|
+
if (
|
|
185
|
+
args[i + 1]
|
|
186
|
+
&& !(/^(-|--)[^-]/).test(args[i + 1])
|
|
187
|
+
&& !flags.bools[key]
|
|
188
|
+
&& (aliases[key] ? !flags.bools[aliases[key]] : true)
|
|
189
|
+
) {
|
|
190
|
+
setArg(key, args[i + 1]);
|
|
191
|
+
i += 1;
|
|
192
|
+
} else if (args[i + 1] && (/true|false/).test(args[i + 1])) {
|
|
193
|
+
setArg(key, args[i + 1] === 'true');
|
|
194
|
+
i += 1;
|
|
195
|
+
} else {
|
|
196
|
+
setArg(key, flags.strings[key] ? '' : true);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
} else {
|
|
200
|
+
argv._.push(flags.strings._ || !isNumber(arg) ? arg : Number(arg));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
Object.keys(defaults).forEach(function (k) {
|
|
205
|
+
if (!hasKey(argv, k.split('.'))) {
|
|
206
|
+
setKey(argv, k.split('.'), defaults[k]);
|
|
207
|
+
|
|
208
|
+
(aliases[k] || []).forEach(function (x) {
|
|
209
|
+
setKey(argv, x.split('.'), defaults[k]);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
if (opts['--']) {
|
|
215
|
+
argv['--'] = notFlags.slice();
|
|
216
|
+
} else {
|
|
217
|
+
notFlags.forEach(function (k) {
|
|
218
|
+
argv._.push(k);
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return argv;
|
|
223
|
+
};
|