yargs 16.0.2 → 16.1.1
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/CHANGELOG.md +32 -295
- package/README.md +27 -8
- package/browser.mjs +4 -4
- package/build/index.cjs +378 -238
- package/build/lib/argsert.js +9 -5
- package/build/lib/command.js +45 -37
- package/build/lib/completion.js +27 -14
- package/build/lib/middleware.js +5 -3
- package/build/lib/parse-command.js +3 -3
- package/build/lib/usage.js +81 -47
- package/build/lib/utils/apply-extends.js +8 -7
- package/build/lib/utils/is-promise.js +2 -2
- package/build/lib/utils/obj-filter.js +1 -1
- package/build/lib/utils/set-blocking.js +4 -2
- package/build/lib/utils/which-module.js +1 -1
- package/build/lib/validation.js +39 -32
- package/build/lib/yargs-factory.js +153 -87
- package/helpers/helpers.mjs +10 -0
- package/helpers/index.js +14 -0
- package/helpers/package.json +3 -0
- package/index.cjs +17 -17
- package/index.mjs +5 -5
- package/lib/platform-shims/browser.mjs +38 -37
- package/package.json +35 -27
- package/{yargs.cjs → yargs} +3 -2
- package/helpers.mjs +0 -14
package/build/lib/usage.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assertNotStrictEqual } from './typings/common-types.js';
|
|
1
|
+
import { assertNotStrictEqual, } from './typings/common-types.js';
|
|
2
2
|
import { objFilter } from './utils/obj-filter.js';
|
|
3
3
|
import { YError } from './yerror.js';
|
|
4
4
|
import setBlocking from './utils/set-blocking.js';
|
|
@@ -85,7 +85,7 @@ export function usage(yargs, y18n, shim) {
|
|
|
85
85
|
let commands = [];
|
|
86
86
|
self.command = function command(cmd, description, isDefault, aliases, deprecated = false) {
|
|
87
87
|
if (isDefault) {
|
|
88
|
-
commands = commands.map(
|
|
88
|
+
commands = commands.map(cmdArray => {
|
|
89
89
|
cmdArray[2] = false;
|
|
90
90
|
return cmdArray;
|
|
91
91
|
});
|
|
@@ -96,12 +96,12 @@ export function usage(yargs, y18n, shim) {
|
|
|
96
96
|
let descriptions = {};
|
|
97
97
|
self.describe = function describe(keyOrKeys, desc) {
|
|
98
98
|
if (Array.isArray(keyOrKeys)) {
|
|
99
|
-
keyOrKeys.forEach(
|
|
99
|
+
keyOrKeys.forEach(k => {
|
|
100
100
|
self.describe(k, desc);
|
|
101
101
|
});
|
|
102
102
|
}
|
|
103
103
|
else if (typeof keyOrKeys === 'object') {
|
|
104
|
-
Object.keys(keyOrKeys).forEach(
|
|
104
|
+
Object.keys(keyOrKeys).forEach(k => {
|
|
105
105
|
self.describe(k, keyOrKeys[k]);
|
|
106
106
|
});
|
|
107
107
|
}
|
|
@@ -111,12 +111,12 @@ export function usage(yargs, y18n, shim) {
|
|
|
111
111
|
};
|
|
112
112
|
self.getDescriptions = () => descriptions;
|
|
113
113
|
let epilogs = [];
|
|
114
|
-
self.epilog =
|
|
114
|
+
self.epilog = msg => {
|
|
115
115
|
epilogs.push(msg);
|
|
116
116
|
};
|
|
117
117
|
let wrapSet = false;
|
|
118
118
|
let wrap;
|
|
119
|
-
self.wrap =
|
|
119
|
+
self.wrap = cols => {
|
|
120
120
|
wrapSet = true;
|
|
121
121
|
wrap = cols;
|
|
122
122
|
};
|
|
@@ -133,7 +133,9 @@ export function usage(yargs, y18n, shim) {
|
|
|
133
133
|
if (cachedHelpMessage)
|
|
134
134
|
return cachedHelpMessage;
|
|
135
135
|
normalizeAliases();
|
|
136
|
-
const base$0 = yargs.customScriptName
|
|
136
|
+
const base$0 = yargs.customScriptName
|
|
137
|
+
? yargs.$0
|
|
138
|
+
: shim.path.basename(yargs.$0);
|
|
137
139
|
const demandedOptions = yargs.getDemandedOptions();
|
|
138
140
|
const demandedCommands = yargs.getDemandedCommands();
|
|
139
141
|
const deprecatedOptions = yargs.getDeprecatedOptions();
|
|
@@ -153,11 +155,11 @@ export function usage(yargs, y18n, shim) {
|
|
|
153
155
|
const theWrap = getWrap();
|
|
154
156
|
const ui = shim.cliui({
|
|
155
157
|
width: theWrap,
|
|
156
|
-
wrap: !!theWrap
|
|
158
|
+
wrap: !!theWrap,
|
|
157
159
|
});
|
|
158
160
|
if (!usageDisabled) {
|
|
159
161
|
if (usages.length) {
|
|
160
|
-
usages.forEach(
|
|
162
|
+
usages.forEach(usage => {
|
|
161
163
|
ui.div(`${usage[0].replace(/\$0/g, base$0)}`);
|
|
162
164
|
if (usage[1]) {
|
|
163
165
|
ui.div({ text: `${usage[1]}`, padding: [1, 0, 0, 0] });
|
|
@@ -179,16 +181,18 @@ export function usage(yargs, y18n, shim) {
|
|
|
179
181
|
if (commands.length) {
|
|
180
182
|
ui.div(__('Commands:'));
|
|
181
183
|
const context = yargs.getContext();
|
|
182
|
-
const parentCommands = context.commands.length
|
|
184
|
+
const parentCommands = context.commands.length
|
|
185
|
+
? `${context.commands.join(' ')} `
|
|
186
|
+
: '';
|
|
183
187
|
if (yargs.getParserConfiguration()['sort-commands'] === true) {
|
|
184
188
|
commands = commands.sort((a, b) => a[0].localeCompare(b[0]));
|
|
185
189
|
}
|
|
186
|
-
commands.forEach(
|
|
190
|
+
commands.forEach(command => {
|
|
187
191
|
const commandString = `${base$0} ${parentCommands}${command[0].replace(/^\$0 ?/, '')}`;
|
|
188
192
|
ui.span({
|
|
189
193
|
text: commandString,
|
|
190
194
|
padding: [0, 2, 0, 2],
|
|
191
|
-
width: maxWidth(commands, theWrap, `${base$0}${parentCommands}`) + 4
|
|
195
|
+
width: maxWidth(commands, theWrap, `${base$0}${parentCommands}`) + 4,
|
|
192
196
|
}, { text: command[1] });
|
|
193
197
|
const hints = [];
|
|
194
198
|
if (command[2])
|
|
@@ -205,7 +209,11 @@ export function usage(yargs, y18n, shim) {
|
|
|
205
209
|
}
|
|
206
210
|
}
|
|
207
211
|
if (hints.length) {
|
|
208
|
-
ui.div({
|
|
212
|
+
ui.div({
|
|
213
|
+
text: hints.join(' '),
|
|
214
|
+
padding: [0, 0, 0, 2],
|
|
215
|
+
align: 'right',
|
|
216
|
+
});
|
|
209
217
|
}
|
|
210
218
|
else {
|
|
211
219
|
ui.div();
|
|
@@ -213,9 +221,9 @@ export function usage(yargs, y18n, shim) {
|
|
|
213
221
|
});
|
|
214
222
|
ui.div();
|
|
215
223
|
}
|
|
216
|
-
const aliasKeys = (Object.keys(options.alias) || [])
|
|
217
|
-
|
|
218
|
-
|
|
224
|
+
const aliasKeys = (Object.keys(options.alias) || []).concat(Object.keys(yargs.parsed.newAliases) || []);
|
|
225
|
+
keys = keys.filter(key => !yargs.parsed.newAliases[key] &&
|
|
226
|
+
aliasKeys.every(alias => (options.alias[alias] || []).indexOf(key) === -1));
|
|
219
227
|
const defaultGroup = __('Options:');
|
|
220
228
|
if (!groups[defaultGroup])
|
|
221
229
|
groups[defaultGroup] = [];
|
|
@@ -224,7 +232,9 @@ export function usage(yargs, y18n, shim) {
|
|
|
224
232
|
const displayedGroups = Object.keys(groups)
|
|
225
233
|
.filter(groupName => groups[groupName].length > 0)
|
|
226
234
|
.map(groupName => {
|
|
227
|
-
const normalizedKeys = groups[groupName]
|
|
235
|
+
const normalizedKeys = groups[groupName]
|
|
236
|
+
.filter(filterHiddenOptions)
|
|
237
|
+
.map(key => {
|
|
228
238
|
if (~aliasKeys.indexOf(key))
|
|
229
239
|
return key;
|
|
230
240
|
for (let i = 0, aliasKey; (aliasKey = aliasKeys[i]) !== undefined; i++) {
|
|
@@ -238,17 +248,26 @@ export function usage(yargs, y18n, shim) {
|
|
|
238
248
|
.filter(({ normalizedKeys }) => normalizedKeys.length > 0)
|
|
239
249
|
.map(({ groupName, normalizedKeys }) => {
|
|
240
250
|
const switches = normalizedKeys.reduce((acc, key) => {
|
|
241
|
-
acc[key] = [key]
|
|
251
|
+
acc[key] = [key]
|
|
252
|
+
.concat(options.alias[key] || [])
|
|
242
253
|
.map(sw => {
|
|
243
254
|
if (groupName === self.getPositionalGroupName())
|
|
244
255
|
return sw;
|
|
245
256
|
else {
|
|
246
|
-
return (/^[0-9]$/.test(sw)
|
|
247
|
-
? ~options.boolean.indexOf(key)
|
|
248
|
-
|
|
257
|
+
return ((/^[0-9]$/.test(sw)
|
|
258
|
+
? ~options.boolean.indexOf(key)
|
|
259
|
+
? '-'
|
|
260
|
+
: '--'
|
|
261
|
+
: sw.length > 1
|
|
262
|
+
? '--'
|
|
263
|
+
: '-') + sw);
|
|
249
264
|
}
|
|
250
265
|
})
|
|
251
|
-
.sort((sw1, sw2) => isLongSwitch(sw1) === isLongSwitch(sw2)
|
|
266
|
+
.sort((sw1, sw2) => isLongSwitch(sw1) === isLongSwitch(sw2)
|
|
267
|
+
? 0
|
|
268
|
+
: isLongSwitch(sw1)
|
|
269
|
+
? 1
|
|
270
|
+
: -1)
|
|
252
271
|
.join(', ');
|
|
253
272
|
return acc;
|
|
254
273
|
}, {});
|
|
@@ -270,7 +289,7 @@ export function usage(yargs, y18n, shim) {
|
|
|
270
289
|
}
|
|
271
290
|
displayedGroups.forEach(({ groupName, normalizedKeys, switches }) => {
|
|
272
291
|
ui.div(groupName);
|
|
273
|
-
normalizedKeys.forEach(
|
|
292
|
+
normalizedKeys.forEach(key => {
|
|
274
293
|
const kswitch = switches[key];
|
|
275
294
|
let desc = descriptions[key] || '';
|
|
276
295
|
let type = null;
|
|
@@ -292,13 +311,23 @@ export function usage(yargs, y18n, shim) {
|
|
|
292
311
|
? `[${__('deprecated: %s', deprecated)}]`
|
|
293
312
|
: `[${__('deprecated')}]`;
|
|
294
313
|
const extra = [
|
|
295
|
-
|
|
314
|
+
key in deprecatedOptions
|
|
315
|
+
? deprecatedExtra(deprecatedOptions[key])
|
|
316
|
+
: null,
|
|
296
317
|
type,
|
|
297
|
-
|
|
298
|
-
options.choices && options.choices[key]
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
318
|
+
key in demandedOptions ? `[${__('required')}]` : null,
|
|
319
|
+
options.choices && options.choices[key]
|
|
320
|
+
? `[${__('choices:')} ${self.stringifiedValues(options.choices[key])}]`
|
|
321
|
+
: null,
|
|
322
|
+
defaultString(options.default[key], options.defaultDescription[key]),
|
|
323
|
+
]
|
|
324
|
+
.filter(Boolean)
|
|
325
|
+
.join(' ');
|
|
326
|
+
ui.span({
|
|
327
|
+
text: getText(kswitch),
|
|
328
|
+
padding: [0, 2, 0, 2 + getIndentation(kswitch)],
|
|
329
|
+
width: maxWidth(switches, theWrap) + 4,
|
|
330
|
+
}, desc);
|
|
302
331
|
if (extra)
|
|
303
332
|
ui.div({ text: extra, padding: [0, 0, 0, 2], align: 'right' });
|
|
304
333
|
else
|
|
@@ -308,30 +337,32 @@ export function usage(yargs, y18n, shim) {
|
|
|
308
337
|
});
|
|
309
338
|
if (examples.length) {
|
|
310
339
|
ui.div(__('Examples:'));
|
|
311
|
-
examples.forEach(
|
|
340
|
+
examples.forEach(example => {
|
|
312
341
|
example[0] = example[0].replace(/\$0/g, base$0);
|
|
313
342
|
});
|
|
314
|
-
examples.forEach(
|
|
343
|
+
examples.forEach(example => {
|
|
315
344
|
if (example[1] === '') {
|
|
316
345
|
ui.div({
|
|
317
346
|
text: example[0],
|
|
318
|
-
padding: [0, 2, 0, 2]
|
|
347
|
+
padding: [0, 2, 0, 2],
|
|
319
348
|
});
|
|
320
349
|
}
|
|
321
350
|
else {
|
|
322
351
|
ui.div({
|
|
323
352
|
text: example[0],
|
|
324
353
|
padding: [0, 2, 0, 2],
|
|
325
|
-
width: maxWidth(examples, theWrap) + 4
|
|
354
|
+
width: maxWidth(examples, theWrap) + 4,
|
|
326
355
|
}, {
|
|
327
|
-
text: example[1]
|
|
356
|
+
text: example[1],
|
|
328
357
|
});
|
|
329
358
|
}
|
|
330
359
|
});
|
|
331
360
|
ui.div();
|
|
332
361
|
}
|
|
333
362
|
if (epilogs.length > 0) {
|
|
334
|
-
const e = epilogs
|
|
363
|
+
const e = epilogs
|
|
364
|
+
.map(epilog => epilog.replace(/\$0/g, base$0))
|
|
365
|
+
.join('\n');
|
|
335
366
|
ui.div(`${e}\n`);
|
|
336
367
|
}
|
|
337
368
|
return ui.toString().replace(/\s*$/, '');
|
|
@@ -341,7 +372,7 @@ export function usage(yargs, y18n, shim) {
|
|
|
341
372
|
if (!Array.isArray(table)) {
|
|
342
373
|
table = Object.values(table).map(v => [v]);
|
|
343
374
|
}
|
|
344
|
-
table.forEach(
|
|
375
|
+
table.forEach(v => {
|
|
345
376
|
width = Math.max(shim.stringWidth(modifier ? `${modifier} ${getText(v[0])}` : getText(v[0])) + getIndentation(v[0]), width);
|
|
346
377
|
});
|
|
347
378
|
if (theWrap)
|
|
@@ -351,8 +382,8 @@ export function usage(yargs, y18n, shim) {
|
|
|
351
382
|
function normalizeAliases() {
|
|
352
383
|
const demandedOptions = yargs.getDemandedOptions();
|
|
353
384
|
const options = yargs.getOptions();
|
|
354
|
-
(Object.keys(options.alias) || []).forEach(
|
|
355
|
-
options.alias[key].forEach(
|
|
385
|
+
(Object.keys(options.alias) || []).forEach(key => {
|
|
386
|
+
options.alias[key].forEach(alias => {
|
|
356
387
|
if (descriptions[alias])
|
|
357
388
|
self.describe(key, descriptions[alias]);
|
|
358
389
|
if (alias in demandedOptions)
|
|
@@ -382,10 +413,10 @@ export function usage(yargs, y18n, shim) {
|
|
|
382
413
|
function addUngroupedKeys(keys, aliases, groups, defaultGroup) {
|
|
383
414
|
let groupedKeys = [];
|
|
384
415
|
let toCheck = null;
|
|
385
|
-
Object.keys(groups).forEach(
|
|
416
|
+
Object.keys(groups).forEach(group => {
|
|
386
417
|
groupedKeys = groupedKeys.concat(groups[group]);
|
|
387
418
|
});
|
|
388
|
-
keys.forEach(
|
|
419
|
+
keys.forEach(key => {
|
|
389
420
|
toCheck = [key].concat(aliases[key]);
|
|
390
421
|
if (!toCheck.some(k => groupedKeys.indexOf(k) !== -1)) {
|
|
391
422
|
groups[defaultGroup].push(key);
|
|
@@ -394,7 +425,8 @@ export function usage(yargs, y18n, shim) {
|
|
|
394
425
|
return groupedKeys;
|
|
395
426
|
}
|
|
396
427
|
function filterHiddenOptions(key) {
|
|
397
|
-
return yargs.getOptions().hiddenOptions.indexOf(key) < 0 ||
|
|
428
|
+
return (yargs.getOptions().hiddenOptions.indexOf(key) < 0 ||
|
|
429
|
+
yargs.parsed.argv[yargs.getOptions().showHiddenOpt]);
|
|
398
430
|
}
|
|
399
431
|
self.showHelp = (level) => {
|
|
400
432
|
const logger = yargs._getLoggerInstance();
|
|
@@ -403,8 +435,10 @@ export function usage(yargs, y18n, shim) {
|
|
|
403
435
|
const emit = typeof level === 'function' ? level : logger[level];
|
|
404
436
|
emit(self.help());
|
|
405
437
|
};
|
|
406
|
-
self.functionDescription =
|
|
407
|
-
const description = fn.name
|
|
438
|
+
self.functionDescription = fn => {
|
|
439
|
+
const description = fn.name
|
|
440
|
+
? shim.Parser.decamelize(fn.name, '-')
|
|
441
|
+
: __('generated-value');
|
|
408
442
|
return ['(', description, ')'].join('');
|
|
409
443
|
};
|
|
410
444
|
self.stringifiedValues = function stringifiedValues(values, separator) {
|
|
@@ -413,7 +447,7 @@ export function usage(yargs, y18n, shim) {
|
|
|
413
447
|
const array = [].concat(values);
|
|
414
448
|
if (!values || !array.length)
|
|
415
449
|
return string;
|
|
416
|
-
array.forEach(
|
|
450
|
+
array.forEach(value => {
|
|
417
451
|
if (string.length)
|
|
418
452
|
string += sep;
|
|
419
453
|
string += JSON.stringify(value);
|
|
@@ -451,7 +485,7 @@ export function usage(yargs, y18n, shim) {
|
|
|
451
485
|
}
|
|
452
486
|
}
|
|
453
487
|
let version = null;
|
|
454
|
-
self.version =
|
|
488
|
+
self.version = ver => {
|
|
455
489
|
version = ver;
|
|
456
490
|
};
|
|
457
491
|
self.showVersion = () => {
|
|
@@ -479,7 +513,7 @@ export function usage(yargs, y18n, shim) {
|
|
|
479
513
|
epilogs,
|
|
480
514
|
examples,
|
|
481
515
|
commands,
|
|
482
|
-
descriptions
|
|
516
|
+
descriptions,
|
|
483
517
|
});
|
|
484
518
|
};
|
|
485
519
|
self.unfreeze = function unfreeze() {
|
|
@@ -493,7 +527,7 @@ export function usage(yargs, y18n, shim) {
|
|
|
493
527
|
epilogs,
|
|
494
528
|
examples,
|
|
495
529
|
commands,
|
|
496
|
-
descriptions
|
|
530
|
+
descriptions,
|
|
497
531
|
} = frozen);
|
|
498
532
|
};
|
|
499
533
|
return self;
|
|
@@ -13,24 +13,25 @@ export function applyExtends(config, cwd, mergeExtends, _shim) {
|
|
|
13
13
|
try {
|
|
14
14
|
pathToDefault = require.resolve(config.extends);
|
|
15
15
|
}
|
|
16
|
-
catch (
|
|
16
|
+
catch (_err) {
|
|
17
|
+
return config;
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
else {
|
|
20
21
|
pathToDefault = getPathToDefaultConfig(cwd, config.extends);
|
|
21
22
|
}
|
|
22
|
-
if (!pathToDefault && !isPath)
|
|
23
|
-
return config;
|
|
24
|
-
if (!pathToDefault)
|
|
25
|
-
throw new YError(`Unable to find extended config '${config.extends}' in '${cwd}'.`);
|
|
26
23
|
checkForCircularExtends(pathToDefault);
|
|
27
24
|
previouslyVisitedConfigs.push(pathToDefault);
|
|
28
|
-
defaultConfig = isPath
|
|
25
|
+
defaultConfig = isPath
|
|
26
|
+
? JSON.parse(shim.readFileSync(pathToDefault, 'utf8'))
|
|
27
|
+
: require(config.extends);
|
|
29
28
|
delete config.extends;
|
|
30
29
|
defaultConfig = applyExtends(defaultConfig, shim.path.dirname(pathToDefault), mergeExtends, shim);
|
|
31
30
|
}
|
|
32
31
|
previouslyVisitedConfigs = [];
|
|
33
|
-
return mergeExtends
|
|
32
|
+
return mergeExtends
|
|
33
|
+
? mergeDeep(defaultConfig, config)
|
|
34
|
+
: Object.assign({}, defaultConfig, config);
|
|
34
35
|
}
|
|
35
36
|
function checkForCircularExtends(cfgPath) {
|
|
36
37
|
if (previouslyVisitedConfigs.indexOf(cfgPath) > -1) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { objectKeys } from '../typings/common-types.js';
|
|
2
2
|
export function objFilter(original = {}, filter = () => true) {
|
|
3
3
|
const obj = {};
|
|
4
|
-
objectKeys(original).forEach(
|
|
4
|
+
objectKeys(original).forEach(key => {
|
|
5
5
|
if (filter(key, original[key])) {
|
|
6
6
|
obj[key] = original[key];
|
|
7
7
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export default function setBlocking(blocking) {
|
|
2
2
|
if (typeof process === 'undefined')
|
|
3
3
|
return;
|
|
4
|
-
[process.stdout, process.stderr].forEach(
|
|
4
|
+
[process.stdout, process.stderr].forEach(_stream => {
|
|
5
5
|
const stream = _stream;
|
|
6
|
-
if (stream._handle &&
|
|
6
|
+
if (stream._handle &&
|
|
7
|
+
stream.isTTY &&
|
|
8
|
+
typeof stream._handle.setBlocking === 'function') {
|
|
7
9
|
stream._handle.setBlocking(blocking);
|
|
8
10
|
}
|
|
9
11
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export default function whichModule(exported) {
|
|
2
2
|
if (typeof require === 'undefined')
|
|
3
3
|
return null;
|
|
4
|
-
for (
|
|
4
|
+
for (let i = 0, files = Object.keys(require.cache), mod; i < files.length; i++) {
|
|
5
5
|
mod = require.cache[files[i]];
|
|
6
6
|
if (mod.exports === exported)
|
|
7
7
|
return mod;
|
package/build/lib/validation.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { argsert } from './argsert.js';
|
|
2
|
-
import { assertNotStrictEqual } from './typings/common-types.js';
|
|
2
|
+
import { assertNotStrictEqual, } from './typings/common-types.js';
|
|
3
3
|
import { levenshtein as distance } from './utils/levenshtein.js';
|
|
4
4
|
import { objFilter } from './utils/obj-filter.js';
|
|
5
5
|
const specialKeys = ['$0', '--', '_'];
|
|
@@ -9,12 +9,16 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
9
9
|
const self = {};
|
|
10
10
|
self.nonOptionCount = function nonOptionCount(argv) {
|
|
11
11
|
const demandedCommands = yargs.getDemandedCommands();
|
|
12
|
-
const
|
|
13
|
-
|
|
12
|
+
const positionalCount = argv._.length + (argv['--'] ? argv['--'].length : 0);
|
|
13
|
+
const _s = positionalCount - yargs.getContext().commands.length;
|
|
14
|
+
if (demandedCommands._ &&
|
|
15
|
+
(_s < demandedCommands._.min || _s > demandedCommands._.max)) {
|
|
14
16
|
if (_s < demandedCommands._.min) {
|
|
15
17
|
if (demandedCommands._.minMsg !== undefined) {
|
|
16
18
|
usage.fail(demandedCommands._.minMsg
|
|
17
|
-
? demandedCommands._.minMsg
|
|
19
|
+
? demandedCommands._.minMsg
|
|
20
|
+
.replace(/\$0/g, _s.toString())
|
|
21
|
+
.replace(/\$1/, demandedCommands._.min.toString())
|
|
18
22
|
: null);
|
|
19
23
|
}
|
|
20
24
|
else {
|
|
@@ -24,7 +28,9 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
24
28
|
else if (_s > demandedCommands._.max) {
|
|
25
29
|
if (demandedCommands._.maxMsg !== undefined) {
|
|
26
30
|
usage.fail(demandedCommands._.maxMsg
|
|
27
|
-
? demandedCommands._.maxMsg
|
|
31
|
+
? demandedCommands._.maxMsg
|
|
32
|
+
.replace(/\$0/g, _s.toString())
|
|
33
|
+
.replace(/\$1/, demandedCommands._.max.toString())
|
|
28
34
|
: null);
|
|
29
35
|
}
|
|
30
36
|
else {
|
|
@@ -42,7 +48,8 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
42
48
|
const demandedOptions = yargs.getDemandedOptions();
|
|
43
49
|
let missing = null;
|
|
44
50
|
for (const key of Object.keys(demandedOptions)) {
|
|
45
|
-
if (!Object.prototype.hasOwnProperty.call(argv, key) ||
|
|
51
|
+
if (!Object.prototype.hasOwnProperty.call(argv, key) ||
|
|
52
|
+
typeof argv[key] === 'undefined') {
|
|
46
53
|
missing = missing || {};
|
|
47
54
|
missing[key] = demandedOptions[key];
|
|
48
55
|
}
|
|
@@ -63,7 +70,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
63
70
|
const commandKeys = yargs.getCommandInstance().getCommands();
|
|
64
71
|
const unknown = [];
|
|
65
72
|
const currentContext = yargs.getContext();
|
|
66
|
-
Object.keys(argv).forEach(
|
|
73
|
+
Object.keys(argv).forEach(key => {
|
|
67
74
|
if (specialKeys.indexOf(key) === -1 &&
|
|
68
75
|
!Object.prototype.hasOwnProperty.call(positionalMap, key) &&
|
|
69
76
|
!Object.prototype.hasOwnProperty.call(yargs._getParseContext(), key) &&
|
|
@@ -71,8 +78,11 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
71
78
|
unknown.push(key);
|
|
72
79
|
}
|
|
73
80
|
});
|
|
74
|
-
if (checkPositionals &&
|
|
75
|
-
|
|
81
|
+
if (checkPositionals &&
|
|
82
|
+
(currentContext.commands.length > 0 ||
|
|
83
|
+
commandKeys.length > 0 ||
|
|
84
|
+
isDefaultCommand)) {
|
|
85
|
+
argv._.slice(currentContext.commands.length).forEach(key => {
|
|
76
86
|
if (commandKeys.indexOf('' + key) === -1) {
|
|
77
87
|
unknown.push('' + key);
|
|
78
88
|
}
|
|
@@ -86,8 +96,8 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
86
96
|
const commandKeys = yargs.getCommandInstance().getCommands();
|
|
87
97
|
const unknown = [];
|
|
88
98
|
const currentContext = yargs.getContext();
|
|
89
|
-
if (
|
|
90
|
-
argv._.slice(currentContext.commands.length).forEach(
|
|
99
|
+
if (currentContext.commands.length > 0 || commandKeys.length > 0) {
|
|
100
|
+
argv._.slice(currentContext.commands.length).forEach(key => {
|
|
91
101
|
if (commandKeys.indexOf('' + key) === -1) {
|
|
92
102
|
unknown.push('' + key);
|
|
93
103
|
}
|
|
@@ -107,7 +117,8 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
107
117
|
}
|
|
108
118
|
const newAliases = yargs.parsed.newAliases;
|
|
109
119
|
for (const a of [key, ...aliases[key]]) {
|
|
110
|
-
if (!Object.prototype.hasOwnProperty.call(newAliases, a) ||
|
|
120
|
+
if (!Object.prototype.hasOwnProperty.call(newAliases, a) ||
|
|
121
|
+
!newAliases[key]) {
|
|
111
122
|
return true;
|
|
112
123
|
}
|
|
113
124
|
}
|
|
@@ -118,10 +129,10 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
118
129
|
const invalid = {};
|
|
119
130
|
if (!Object.keys(options.choices).length)
|
|
120
131
|
return;
|
|
121
|
-
Object.keys(argv).forEach(
|
|
132
|
+
Object.keys(argv).forEach(key => {
|
|
122
133
|
if (specialKeys.indexOf(key) === -1 &&
|
|
123
134
|
Object.prototype.hasOwnProperty.call(options.choices, key)) {
|
|
124
|
-
[].concat(argv[key]).forEach(
|
|
135
|
+
[].concat(argv[key]).forEach(value => {
|
|
125
136
|
if (options.choices[key].indexOf(value) === -1 &&
|
|
126
137
|
value !== undefined) {
|
|
127
138
|
invalid[key] = (invalid[key] || []).concat(value);
|
|
@@ -133,7 +144,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
133
144
|
if (!invalidKeys.length)
|
|
134
145
|
return;
|
|
135
146
|
let msg = __('Invalid values:');
|
|
136
|
-
invalidKeys.forEach(
|
|
147
|
+
invalidKeys.forEach(key => {
|
|
137
148
|
msg += `\n ${__('Argument: %s, Given: %s, Choices: %s', key, usage.stringifiedValues(invalid[key]), usage.stringifiedValues(options.choices[key]))}`;
|
|
138
149
|
});
|
|
139
150
|
usage.fail(msg);
|
|
@@ -142,7 +153,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
142
153
|
self.check = function check(f, global) {
|
|
143
154
|
checks.push({
|
|
144
155
|
func: f,
|
|
145
|
-
global
|
|
156
|
+
global,
|
|
146
157
|
});
|
|
147
158
|
};
|
|
148
159
|
self.customChecks = function customChecks(argv, aliases) {
|
|
@@ -168,7 +179,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
168
179
|
self.implies = function implies(key, value) {
|
|
169
180
|
argsert('<string|object> [array|number|string]', [key, value], arguments.length);
|
|
170
181
|
if (typeof key === 'object') {
|
|
171
|
-
Object.keys(key).forEach(
|
|
182
|
+
Object.keys(key).forEach(k => {
|
|
172
183
|
self.implies(k, key[k]);
|
|
173
184
|
});
|
|
174
185
|
}
|
|
@@ -178,7 +189,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
178
189
|
implied[key] = [];
|
|
179
190
|
}
|
|
180
191
|
if (Array.isArray(value)) {
|
|
181
|
-
value.forEach(
|
|
192
|
+
value.forEach(i => self.implies(key, i));
|
|
182
193
|
}
|
|
183
194
|
else {
|
|
184
195
|
assertNotStrictEqual(value, undefined, shim);
|
|
@@ -206,9 +217,9 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
206
217
|
}
|
|
207
218
|
self.implications = function implications(argv) {
|
|
208
219
|
const implyFail = [];
|
|
209
|
-
Object.keys(implied).forEach(
|
|
220
|
+
Object.keys(implied).forEach(key => {
|
|
210
221
|
const origKey = key;
|
|
211
|
-
(implied[key] || []).forEach(
|
|
222
|
+
(implied[key] || []).forEach(value => {
|
|
212
223
|
let key = origKey;
|
|
213
224
|
const origValue = value;
|
|
214
225
|
key = keyExists(argv, key);
|
|
@@ -220,8 +231,8 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
220
231
|
});
|
|
221
232
|
if (implyFail.length) {
|
|
222
233
|
let msg = `${__('Implications failed:')}\n`;
|
|
223
|
-
implyFail.forEach(
|
|
224
|
-
msg +=
|
|
234
|
+
implyFail.forEach(value => {
|
|
235
|
+
msg += value;
|
|
225
236
|
});
|
|
226
237
|
usage.fail(msg);
|
|
227
238
|
}
|
|
@@ -230,7 +241,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
230
241
|
self.conflicts = function conflicts(key, value) {
|
|
231
242
|
argsert('<string|object> [array|string]', [key, value], arguments.length);
|
|
232
243
|
if (typeof key === 'object') {
|
|
233
|
-
Object.keys(key).forEach(
|
|
244
|
+
Object.keys(key).forEach(k => {
|
|
234
245
|
self.conflicts(k, key[k]);
|
|
235
246
|
});
|
|
236
247
|
}
|
|
@@ -240,7 +251,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
240
251
|
conflicting[key] = [];
|
|
241
252
|
}
|
|
242
253
|
if (Array.isArray(value)) {
|
|
243
|
-
value.forEach(
|
|
254
|
+
value.forEach(i => self.conflicts(key, i));
|
|
244
255
|
}
|
|
245
256
|
else {
|
|
246
257
|
conflicting[key].push(value);
|
|
@@ -249,9 +260,9 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
249
260
|
};
|
|
250
261
|
self.getConflicting = () => conflicting;
|
|
251
262
|
self.conflicting = function conflictingFn(argv) {
|
|
252
|
-
Object.keys(argv).forEach(
|
|
263
|
+
Object.keys(argv).forEach(key => {
|
|
253
264
|
if (conflicting[key]) {
|
|
254
|
-
conflicting[key].forEach(
|
|
265
|
+
conflicting[key].forEach(value => {
|
|
255
266
|
if (value && argv[key] !== undefined && argv[value] !== undefined) {
|
|
256
267
|
usage.fail(__('Arguments %s and %s are mutually exclusive', key, value));
|
|
257
268
|
}
|
|
@@ -285,17 +296,13 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
285
296
|
frozens.push({
|
|
286
297
|
implied,
|
|
287
298
|
checks,
|
|
288
|
-
conflicting
|
|
299
|
+
conflicting,
|
|
289
300
|
});
|
|
290
301
|
};
|
|
291
302
|
self.unfreeze = function unfreeze() {
|
|
292
303
|
const frozen = frozens.pop();
|
|
293
304
|
assertNotStrictEqual(frozen, undefined, shim);
|
|
294
|
-
({
|
|
295
|
-
implied,
|
|
296
|
-
checks,
|
|
297
|
-
conflicting
|
|
298
|
-
} = frozen);
|
|
305
|
+
({ implied, checks, conflicting } = frozen);
|
|
299
306
|
};
|
|
300
307
|
return self;
|
|
301
308
|
}
|