minimist 0.2.2 → 0.2.3

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 CHANGED
@@ -1,54 +1,29 @@
1
1
  {
2
- "root": true,
2
+ "root": true,
3
3
 
4
- "extends": "@ljharb/eslint-config/node/0.4",
4
+ "extends": "@ljharb/eslint-config/node/0.4",
5
5
 
6
- "rules": {
7
- "array-bracket-spacing": 0,
8
- "array-element-newline": 0,
9
- "brace-style": 1,
10
- "camelcase": 1,
11
- "comma-dangle": 1,
12
- "comma-spacing": 1,
13
- "complexity": 0,
14
- "curly": 1,
15
- "dot-notation": 1,
16
- "eol-last": 1,
17
- "func-style": 1,
18
- "function-paren-newline": 1,
19
- "indent": [1, 4],
20
- "key-spacing": 1,
21
- "max-lines-per-function": 0,
22
- "max-nested-callbacks": 1,
23
- "max-statements": 0,
24
- "multiline-comment-style": 1,
25
- "no-array-constructor": 1,
26
- "no-continue": 1,
27
- "no-div-regex": 1,
28
- "no-extra-parens": 1,
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
@@ -109,13 +109,36 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
109
109
  - coverage script [`e5531ba`](https://github.com/minimistjs/minimist/commit/e5531ba0479da3b8138d3d8cac545d84ccb1c8df)
110
110
  - extra fn to get 100% coverage again [`a6972da`](https://github.com/minimistjs/minimist/commit/a6972da89e56bf77642f8ec05a13b6558db93498)
111
111
 
112
- ## [v1.0.0](https://github.com/minimistjs/minimist/compare/v0.2.2...v1.0.0) - 2014-08-10
112
+ ## [v1.0.0](https://github.com/minimistjs/minimist/compare/v0.2.3...v1.0.0) - 2014-08-10
113
113
 
114
114
  ### Commits
115
115
 
116
116
  - added stopEarly option [`471c7e4`](https://github.com/minimistjs/minimist/commit/471c7e4a7e910fc7ad8f9df850a186daf32c64e9)
117
117
  - fix list [`fef6ae7`](https://github.com/minimistjs/minimist/commit/fef6ae79c38b9dc1c49569abb7cd04eb965eac5e)
118
118
 
119
+ ## [v0.2.3](https://github.com/minimistjs/minimist/compare/v0.2.2...v0.2.3) - 2023-02-09
120
+
121
+ ### Merged
122
+
123
+ - [Fix] Fix long option followed by single dash [`#17`](https://github.com/minimistjs/minimist/pull/17)
124
+ - [Tests] Remove duplicate test [`#12`](https://github.com/minimistjs/minimist/pull/12)
125
+ - [Fix] opt.string works with multiple aliases [`#10`](https://github.com/minimistjs/minimist/pull/10)
126
+
127
+ ### Fixed
128
+
129
+ - [Fix] Fix long option followed by single dash (#17) [`#15`](https://github.com/minimistjs/minimist/issues/15)
130
+ - [Tests] Remove duplicate test (#12) [`#8`](https://github.com/minimistjs/minimist/issues/8)
131
+ - [Fix] opt.string works with multiple aliases (#10) [`#9`](https://github.com/minimistjs/minimist/issues/9)
132
+
133
+ ### Commits
134
+
135
+ - [eslint] fix indentation and whitespace [`e5f5067`](https://github.com/minimistjs/minimist/commit/e5f5067259ceeaf0b098d14bec910f87e58708c7)
136
+ - [eslint] more cleanup [`36ac5d0`](https://github.com/minimistjs/minimist/commit/36ac5d0d95e4947d074e5737d94814034ca335d1)
137
+ - [eslint] fix indentation [`34b0f1c`](https://github.com/minimistjs/minimist/commit/34b0f1ccaa45183c3c4f06a91f9b405180a6f982)
138
+ - isConstructorOrProto adapted from PR [`ef9153f`](https://github.com/minimistjs/minimist/commit/ef9153fc52b6cea0744b2239921c5dcae4697f11)
139
+ - [Dev Deps] update `@ljharb/eslint-config`, `aud` [`098873c`](https://github.com/minimistjs/minimist/commit/098873c213cdb7c92e55ae1ef5aa1af3a8192a79)
140
+ - [Dev Deps] add missing `npmignore` dev dep [`3226afa`](https://github.com/minimistjs/minimist/commit/3226afaf09e9d127ca369742437fe6e88f752d6b)
141
+
119
142
  ## [v0.2.2](https://github.com/minimistjs/minimist/compare/v0.2.1...v0.2.2) - 2022-10-10
120
143
 
121
144
  ### 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
- { _: [ 'foo', 'bar', 'baz' ],
34
- x: 3,
35
- y: 4,
36
- n: 5,
37
- a: true,
38
- b: true,
39
- c: true,
40
- beep: 'boop' }
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
- { _: [ 'one', 'two', 'three' ],
77
- '--': [ 'four', 'five', '--six' ] }
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
@@ -1,2 +1,4 @@
1
+ 'use strict';
2
+
1
3
  var argv = require('../')(process.argv.slice(2));
2
4
  console.dir(argv);
package/index.js CHANGED
@@ -1,209 +1,223 @@
1
- module.exports = function (args, opts) {
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 hasKey (obj, keys) {
167
- var o = obj;
168
- keys.slice(0,-1).forEach(function (key) {
169
- o = (o[key] || {});
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
- var key = keys[keys.length - 1];
173
- return key in o;
9
+ function isConstructorOrProto(obj, key) {
10
+ return key === 'constructor' && (typeof obj[key] === 'function' || key === '__proto__');
174
11
  }
175
12
 
176
- function setKey (obj, keys, value) {
177
- var o = obj;
178
- for (var i = 0; i < keys.length-1; i++) {
179
- var key = keys[i];
180
- if (key === '__proto__') return;
181
- if (o[key] === undefined) o[key] = {};
182
- if (o[key] === Object.prototype || o[key] === Number.prototype
183
- || o[key] === String.prototype) o[key] = {};
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 isNumber (x) {
205
- if (typeof x === 'number') return true;
206
- if (/^0x[0-9a-f]+$/i.test(x)) return true;
207
- return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
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 (key === '__proto__' || 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 (key === '__proto__') { 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
+ };