yargs 16.0.4-candidate.0 → 16.1.0
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 +16 -0
- package/README.md +25 -6
- package/browser.mjs +4 -4
- package/build/index.cjs +348 -231
- package/build/lib/argsert.js +9 -5
- package/build/lib/command.js +44 -35
- 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 +37 -31
- package/build/lib/yargs-factory.js +126 -83
- package/helpers.mjs +7 -11
- package/index.cjs +17 -17
- package/index.mjs +5 -5
- package/lib/platform-shims/browser.mjs +38 -37
- package/package.json +31 -25
- package/yargs +2 -1
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', '--', '_'];
|
|
@@ -11,11 +11,14 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
11
11
|
const demandedCommands = yargs.getDemandedCommands();
|
|
12
12
|
const positionalCount = argv._.length + (argv['--'] ? argv['--'].length : 0);
|
|
13
13
|
const _s = positionalCount - yargs.getContext().commands.length;
|
|
14
|
-
if (demandedCommands._ &&
|
|
14
|
+
if (demandedCommands._ &&
|
|
15
|
+
(_s < demandedCommands._.min || _s > demandedCommands._.max)) {
|
|
15
16
|
if (_s < demandedCommands._.min) {
|
|
16
17
|
if (demandedCommands._.minMsg !== undefined) {
|
|
17
18
|
usage.fail(demandedCommands._.minMsg
|
|
18
|
-
? demandedCommands._.minMsg
|
|
19
|
+
? demandedCommands._.minMsg
|
|
20
|
+
.replace(/\$0/g, _s.toString())
|
|
21
|
+
.replace(/\$1/, demandedCommands._.min.toString())
|
|
19
22
|
: null);
|
|
20
23
|
}
|
|
21
24
|
else {
|
|
@@ -25,7 +28,9 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
25
28
|
else if (_s > demandedCommands._.max) {
|
|
26
29
|
if (demandedCommands._.maxMsg !== undefined) {
|
|
27
30
|
usage.fail(demandedCommands._.maxMsg
|
|
28
|
-
? demandedCommands._.maxMsg
|
|
31
|
+
? demandedCommands._.maxMsg
|
|
32
|
+
.replace(/\$0/g, _s.toString())
|
|
33
|
+
.replace(/\$1/, demandedCommands._.max.toString())
|
|
29
34
|
: null);
|
|
30
35
|
}
|
|
31
36
|
else {
|
|
@@ -43,7 +48,8 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
43
48
|
const demandedOptions = yargs.getDemandedOptions();
|
|
44
49
|
let missing = null;
|
|
45
50
|
for (const key of Object.keys(demandedOptions)) {
|
|
46
|
-
if (!Object.prototype.hasOwnProperty.call(argv, key) ||
|
|
51
|
+
if (!Object.prototype.hasOwnProperty.call(argv, key) ||
|
|
52
|
+
typeof argv[key] === 'undefined') {
|
|
47
53
|
missing = missing || {};
|
|
48
54
|
missing[key] = demandedOptions[key];
|
|
49
55
|
}
|
|
@@ -64,7 +70,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
64
70
|
const commandKeys = yargs.getCommandInstance().getCommands();
|
|
65
71
|
const unknown = [];
|
|
66
72
|
const currentContext = yargs.getContext();
|
|
67
|
-
Object.keys(argv).forEach(
|
|
73
|
+
Object.keys(argv).forEach(key => {
|
|
68
74
|
if (specialKeys.indexOf(key) === -1 &&
|
|
69
75
|
!Object.prototype.hasOwnProperty.call(positionalMap, key) &&
|
|
70
76
|
!Object.prototype.hasOwnProperty.call(yargs._getParseContext(), key) &&
|
|
@@ -72,8 +78,11 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
72
78
|
unknown.push(key);
|
|
73
79
|
}
|
|
74
80
|
});
|
|
75
|
-
if (checkPositionals &&
|
|
76
|
-
|
|
81
|
+
if (checkPositionals &&
|
|
82
|
+
(currentContext.commands.length > 0 ||
|
|
83
|
+
commandKeys.length > 0 ||
|
|
84
|
+
isDefaultCommand)) {
|
|
85
|
+
argv._.slice(currentContext.commands.length).forEach(key => {
|
|
77
86
|
if (commandKeys.indexOf('' + key) === -1) {
|
|
78
87
|
unknown.push('' + key);
|
|
79
88
|
}
|
|
@@ -87,8 +96,8 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
87
96
|
const commandKeys = yargs.getCommandInstance().getCommands();
|
|
88
97
|
const unknown = [];
|
|
89
98
|
const currentContext = yargs.getContext();
|
|
90
|
-
if (
|
|
91
|
-
argv._.slice(currentContext.commands.length).forEach(
|
|
99
|
+
if (currentContext.commands.length > 0 || commandKeys.length > 0) {
|
|
100
|
+
argv._.slice(currentContext.commands.length).forEach(key => {
|
|
92
101
|
if (commandKeys.indexOf('' + key) === -1) {
|
|
93
102
|
unknown.push('' + key);
|
|
94
103
|
}
|
|
@@ -108,7 +117,8 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
108
117
|
}
|
|
109
118
|
const newAliases = yargs.parsed.newAliases;
|
|
110
119
|
for (const a of [key, ...aliases[key]]) {
|
|
111
|
-
if (!Object.prototype.hasOwnProperty.call(newAliases, a) ||
|
|
120
|
+
if (!Object.prototype.hasOwnProperty.call(newAliases, a) ||
|
|
121
|
+
!newAliases[key]) {
|
|
112
122
|
return true;
|
|
113
123
|
}
|
|
114
124
|
}
|
|
@@ -119,10 +129,10 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
119
129
|
const invalid = {};
|
|
120
130
|
if (!Object.keys(options.choices).length)
|
|
121
131
|
return;
|
|
122
|
-
Object.keys(argv).forEach(
|
|
132
|
+
Object.keys(argv).forEach(key => {
|
|
123
133
|
if (specialKeys.indexOf(key) === -1 &&
|
|
124
134
|
Object.prototype.hasOwnProperty.call(options.choices, key)) {
|
|
125
|
-
[].concat(argv[key]).forEach(
|
|
135
|
+
[].concat(argv[key]).forEach(value => {
|
|
126
136
|
if (options.choices[key].indexOf(value) === -1 &&
|
|
127
137
|
value !== undefined) {
|
|
128
138
|
invalid[key] = (invalid[key] || []).concat(value);
|
|
@@ -134,7 +144,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
134
144
|
if (!invalidKeys.length)
|
|
135
145
|
return;
|
|
136
146
|
let msg = __('Invalid values:');
|
|
137
|
-
invalidKeys.forEach(
|
|
147
|
+
invalidKeys.forEach(key => {
|
|
138
148
|
msg += `\n ${__('Argument: %s, Given: %s, Choices: %s', key, usage.stringifiedValues(invalid[key]), usage.stringifiedValues(options.choices[key]))}`;
|
|
139
149
|
});
|
|
140
150
|
usage.fail(msg);
|
|
@@ -143,7 +153,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
143
153
|
self.check = function check(f, global) {
|
|
144
154
|
checks.push({
|
|
145
155
|
func: f,
|
|
146
|
-
global
|
|
156
|
+
global,
|
|
147
157
|
});
|
|
148
158
|
};
|
|
149
159
|
self.customChecks = function customChecks(argv, aliases) {
|
|
@@ -169,7 +179,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
169
179
|
self.implies = function implies(key, value) {
|
|
170
180
|
argsert('<string|object> [array|number|string]', [key, value], arguments.length);
|
|
171
181
|
if (typeof key === 'object') {
|
|
172
|
-
Object.keys(key).forEach(
|
|
182
|
+
Object.keys(key).forEach(k => {
|
|
173
183
|
self.implies(k, key[k]);
|
|
174
184
|
});
|
|
175
185
|
}
|
|
@@ -179,7 +189,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
179
189
|
implied[key] = [];
|
|
180
190
|
}
|
|
181
191
|
if (Array.isArray(value)) {
|
|
182
|
-
value.forEach(
|
|
192
|
+
value.forEach(i => self.implies(key, i));
|
|
183
193
|
}
|
|
184
194
|
else {
|
|
185
195
|
assertNotStrictEqual(value, undefined, shim);
|
|
@@ -207,9 +217,9 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
207
217
|
}
|
|
208
218
|
self.implications = function implications(argv) {
|
|
209
219
|
const implyFail = [];
|
|
210
|
-
Object.keys(implied).forEach(
|
|
220
|
+
Object.keys(implied).forEach(key => {
|
|
211
221
|
const origKey = key;
|
|
212
|
-
(implied[key] || []).forEach(
|
|
222
|
+
(implied[key] || []).forEach(value => {
|
|
213
223
|
let key = origKey;
|
|
214
224
|
const origValue = value;
|
|
215
225
|
key = keyExists(argv, key);
|
|
@@ -221,8 +231,8 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
221
231
|
});
|
|
222
232
|
if (implyFail.length) {
|
|
223
233
|
let msg = `${__('Implications failed:')}\n`;
|
|
224
|
-
implyFail.forEach(
|
|
225
|
-
msg +=
|
|
234
|
+
implyFail.forEach(value => {
|
|
235
|
+
msg += value;
|
|
226
236
|
});
|
|
227
237
|
usage.fail(msg);
|
|
228
238
|
}
|
|
@@ -231,7 +241,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
231
241
|
self.conflicts = function conflicts(key, value) {
|
|
232
242
|
argsert('<string|object> [array|string]', [key, value], arguments.length);
|
|
233
243
|
if (typeof key === 'object') {
|
|
234
|
-
Object.keys(key).forEach(
|
|
244
|
+
Object.keys(key).forEach(k => {
|
|
235
245
|
self.conflicts(k, key[k]);
|
|
236
246
|
});
|
|
237
247
|
}
|
|
@@ -241,7 +251,7 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
241
251
|
conflicting[key] = [];
|
|
242
252
|
}
|
|
243
253
|
if (Array.isArray(value)) {
|
|
244
|
-
value.forEach(
|
|
254
|
+
value.forEach(i => self.conflicts(key, i));
|
|
245
255
|
}
|
|
246
256
|
else {
|
|
247
257
|
conflicting[key].push(value);
|
|
@@ -250,9 +260,9 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
250
260
|
};
|
|
251
261
|
self.getConflicting = () => conflicting;
|
|
252
262
|
self.conflicting = function conflictingFn(argv) {
|
|
253
|
-
Object.keys(argv).forEach(
|
|
263
|
+
Object.keys(argv).forEach(key => {
|
|
254
264
|
if (conflicting[key]) {
|
|
255
|
-
conflicting[key].forEach(
|
|
265
|
+
conflicting[key].forEach(value => {
|
|
256
266
|
if (value && argv[key] !== undefined && argv[value] !== undefined) {
|
|
257
267
|
usage.fail(__('Arguments %s and %s are mutually exclusive', key, value));
|
|
258
268
|
}
|
|
@@ -286,17 +296,13 @@ export function validation(yargs, usage, y18n, shim) {
|
|
|
286
296
|
frozens.push({
|
|
287
297
|
implied,
|
|
288
298
|
checks,
|
|
289
|
-
conflicting
|
|
299
|
+
conflicting,
|
|
290
300
|
});
|
|
291
301
|
};
|
|
292
302
|
self.unfreeze = function unfreeze() {
|
|
293
303
|
const frozen = frozens.pop();
|
|
294
304
|
assertNotStrictEqual(frozen, undefined, shim);
|
|
295
|
-
({
|
|
296
|
-
implied,
|
|
297
|
-
checks,
|
|
298
|
-
conflicting
|
|
299
|
-
} = frozen);
|
|
305
|
+
({ implied, checks, conflicting } = frozen);
|
|
300
306
|
};
|
|
301
307
|
return self;
|
|
302
308
|
}
|