yargs 16.0.3 → 16.2.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 +34 -293
- package/README.md +27 -8
- package/browser.mjs +4 -4
- package/build/index.cjs +432 -276
- package/build/lib/argsert.js +9 -5
- package/build/lib/command.js +98 -74
- 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 -26
- package/yargs +3 -2
- package/helpers.mjs +0 -14
package/build/lib/argsert.js
CHANGED
|
@@ -5,15 +5,19 @@ export function argsert(arg1, arg2, arg3) {
|
|
|
5
5
|
function parseArgs() {
|
|
6
6
|
return typeof arg1 === 'object'
|
|
7
7
|
? [{ demanded: [], optional: [] }, arg1, arg2]
|
|
8
|
-
: [
|
|
8
|
+
: [
|
|
9
|
+
parseCommand(`cmd ${arg1}`),
|
|
10
|
+
arg2,
|
|
11
|
+
arg3,
|
|
12
|
+
];
|
|
9
13
|
}
|
|
10
14
|
try {
|
|
11
15
|
let position = 0;
|
|
12
|
-
|
|
16
|
+
const [parsed, callerArguments, _length] = parseArgs();
|
|
13
17
|
const args = [].slice.call(callerArguments);
|
|
14
18
|
while (args.length && args[args.length - 1] === undefined)
|
|
15
19
|
args.pop();
|
|
16
|
-
length =
|
|
20
|
+
const length = _length || args.length;
|
|
17
21
|
if (length < parsed.demanded.length) {
|
|
18
22
|
throw new YError(`Not enough arguments provided. Expected ${parsed.demanded.length} but received ${args.length}.`);
|
|
19
23
|
}
|
|
@@ -21,7 +25,7 @@ export function argsert(arg1, arg2, arg3) {
|
|
|
21
25
|
if (length > totalCommands) {
|
|
22
26
|
throw new YError(`Too many arguments provided. Expected max ${totalCommands} but received ${length}.`);
|
|
23
27
|
}
|
|
24
|
-
parsed.demanded.forEach(
|
|
28
|
+
parsed.demanded.forEach(demanded => {
|
|
25
29
|
const arg = args.shift();
|
|
26
30
|
const observedType = guessType(arg);
|
|
27
31
|
const matchingTypes = demanded.cmd.filter(type => type === observedType || type === '*');
|
|
@@ -29,7 +33,7 @@ export function argsert(arg1, arg2, arg3) {
|
|
|
29
33
|
argumentTypeError(observedType, demanded.cmd, position);
|
|
30
34
|
position += 1;
|
|
31
35
|
});
|
|
32
|
-
parsed.optional.forEach(
|
|
36
|
+
parsed.optional.forEach(optional => {
|
|
33
37
|
if (args.length === 0)
|
|
34
38
|
return;
|
|
35
39
|
const arg = args.shift();
|
package/build/lib/command.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { assertNotStrictEqual } from './typings/common-types.js';
|
|
1
|
+
import { assertNotStrictEqual, } from './typings/common-types.js';
|
|
2
2
|
import { isPromise } from './utils/is-promise.js';
|
|
3
|
-
import { applyMiddleware, commandMiddlewareFactory } from './middleware.js';
|
|
3
|
+
import { applyMiddleware, commandMiddlewareFactory, } from './middleware.js';
|
|
4
4
|
import { parseCommand } from './parse-command.js';
|
|
5
|
-
import { isYargsInstance } from './yargs-factory.js';
|
|
5
|
+
import { isYargsInstance, } from './yargs-factory.js';
|
|
6
6
|
import whichModule from './utils/which-module.js';
|
|
7
7
|
const DEFAULT_MARKER = /(^\*)|(^\$0)/;
|
|
8
8
|
export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
@@ -15,55 +15,65 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
15
15
|
const middlewares = commandMiddlewareFactory(commandMiddleware);
|
|
16
16
|
handler = handler || (() => { });
|
|
17
17
|
if (Array.isArray(cmd)) {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
if (isCommandAndAliases(cmd)) {
|
|
19
|
+
[cmd, ...aliases] = cmd;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
for (const command of cmd) {
|
|
23
|
+
self.addHandler(command);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
20
26
|
}
|
|
21
27
|
else if (isCommandHandlerDefinition(cmd)) {
|
|
22
|
-
let command =
|
|
28
|
+
let command = Array.isArray(cmd.command) || typeof cmd.command === 'string'
|
|
29
|
+
? cmd.command
|
|
30
|
+
: moduleName(cmd);
|
|
23
31
|
if (cmd.aliases)
|
|
24
32
|
command = [].concat(command).concat(cmd.aliases);
|
|
25
33
|
self.addHandler(command, extractDesc(cmd), cmd.builder, cmd.handler, cmd.middlewares, cmd.deprecated);
|
|
26
34
|
return;
|
|
27
35
|
}
|
|
28
|
-
if (isCommandBuilderDefinition(builder)) {
|
|
36
|
+
else if (isCommandBuilderDefinition(builder)) {
|
|
29
37
|
self.addHandler([cmd].concat(aliases), description, builder.builder, builder.handler, builder.middlewares, builder.deprecated);
|
|
30
38
|
return;
|
|
31
39
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
if (typeof cmd === 'string') {
|
|
41
|
+
const parsedCommand = parseCommand(cmd);
|
|
42
|
+
aliases = aliases.map(alias => parseCommand(alias).cmd);
|
|
43
|
+
let isDefault = false;
|
|
44
|
+
const parsedAliases = [parsedCommand.cmd].concat(aliases).filter(c => {
|
|
45
|
+
if (DEFAULT_MARKER.test(c)) {
|
|
46
|
+
isDefault = true;
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
});
|
|
51
|
+
if (parsedAliases.length === 0 && isDefault)
|
|
52
|
+
parsedAliases.push('$0');
|
|
53
|
+
if (isDefault) {
|
|
54
|
+
parsedCommand.cmd = parsedAliases[0];
|
|
55
|
+
aliases = parsedAliases.slice(1);
|
|
56
|
+
cmd = cmd.replace(DEFAULT_MARKER, parsedCommand.cmd);
|
|
39
57
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
aliases.forEach(alias => {
|
|
59
|
+
aliasMap[alias] = parsedCommand.cmd;
|
|
60
|
+
});
|
|
61
|
+
if (description !== false) {
|
|
62
|
+
usage.command(cmd, description, isDefault, aliases, deprecated);
|
|
63
|
+
}
|
|
64
|
+
handlers[parsedCommand.cmd] = {
|
|
65
|
+
original: cmd,
|
|
66
|
+
description,
|
|
67
|
+
handler,
|
|
68
|
+
builder: builder || {},
|
|
69
|
+
middlewares,
|
|
70
|
+
deprecated,
|
|
71
|
+
demanded: parsedCommand.demanded,
|
|
72
|
+
optional: parsedCommand.optional,
|
|
73
|
+
};
|
|
74
|
+
if (isDefault)
|
|
75
|
+
defaultCommand = handlers[parsedCommand.cmd];
|
|
54
76
|
}
|
|
55
|
-
handlers[parsedCommand.cmd] = {
|
|
56
|
-
original: cmd,
|
|
57
|
-
description,
|
|
58
|
-
handler,
|
|
59
|
-
builder: builder || {},
|
|
60
|
-
middlewares,
|
|
61
|
-
deprecated,
|
|
62
|
-
demanded: parsedCommand.demanded,
|
|
63
|
-
optional: parsedCommand.optional
|
|
64
|
-
};
|
|
65
|
-
if (isDefault)
|
|
66
|
-
defaultCommand = handlers[parsedCommand.cmd];
|
|
67
77
|
};
|
|
68
78
|
self.addDirectory = function addDirectory(dir, context, req, callerFile, opts) {
|
|
69
79
|
opts = opts || {};
|
|
@@ -93,7 +103,7 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
93
103
|
function commandFromFilename(filename) {
|
|
94
104
|
return shim.path.basename(filename, shim.path.extname(filename));
|
|
95
105
|
}
|
|
96
|
-
function extractDesc({ describe, description, desc }) {
|
|
106
|
+
function extractDesc({ describe, description, desc, }) {
|
|
97
107
|
for (const test of [describe, description, desc]) {
|
|
98
108
|
if (typeof test === 'string' || test === false)
|
|
99
109
|
return test;
|
|
@@ -121,7 +131,9 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
121
131
|
const builderOutput = builder(yargs.reset(parsed.aliases));
|
|
122
132
|
const innerYargs = isYargsInstance(builderOutput) ? builderOutput : yargs;
|
|
123
133
|
if (shouldUpdateUsage(innerYargs)) {
|
|
124
|
-
innerYargs
|
|
134
|
+
innerYargs
|
|
135
|
+
.getUsageInstance()
|
|
136
|
+
.usage(usageFromParentCommandsCommandHandler(parentCommands, commandHandler), commandHandler.description);
|
|
125
137
|
}
|
|
126
138
|
innerArgv = innerYargs._parseArgs(null, null, true, commandIndex);
|
|
127
139
|
aliases = innerYargs.parsed.aliases;
|
|
@@ -129,9 +141,11 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
129
141
|
else if (isCommandBuilderOptionDefinitions(builder)) {
|
|
130
142
|
const innerYargs = yargs.reset(parsed.aliases);
|
|
131
143
|
if (shouldUpdateUsage(innerYargs)) {
|
|
132
|
-
innerYargs
|
|
144
|
+
innerYargs
|
|
145
|
+
.getUsageInstance()
|
|
146
|
+
.usage(usageFromParentCommandsCommandHandler(parentCommands, commandHandler), commandHandler.description);
|
|
133
147
|
}
|
|
134
|
-
Object.keys(commandHandler.builder).forEach(
|
|
148
|
+
Object.keys(commandHandler.builder).forEach(key => {
|
|
135
149
|
innerYargs.option(key, builder[key]);
|
|
136
150
|
});
|
|
137
151
|
innerArgv = innerYargs._parseArgs(null, null, true, commandIndex);
|
|
@@ -140,7 +154,9 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
140
154
|
if (!yargs._hasOutput()) {
|
|
141
155
|
positionalMap = populatePositionals(commandHandler, innerArgv, currentContext);
|
|
142
156
|
}
|
|
143
|
-
const middlewares = globalMiddleware
|
|
157
|
+
const middlewares = globalMiddleware
|
|
158
|
+
.slice(0)
|
|
159
|
+
.concat(commandHandler.middlewares);
|
|
144
160
|
applyMiddleware(innerArgv, yargs, middlewares, true);
|
|
145
161
|
if (!yargs._hasOutput()) {
|
|
146
162
|
yargs._runValidation(innerArgv, aliases, positionalMap, yargs.parsed.error, !command);
|
|
@@ -148,8 +164,7 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
148
164
|
if (commandHandler.handler && !yargs._hasOutput()) {
|
|
149
165
|
yargs._setHasOutput();
|
|
150
166
|
const populateDoubleDash = !!yargs.getOptions().configuration['populate--'];
|
|
151
|
-
|
|
152
|
-
yargs._copyDoubleDash(innerArgv);
|
|
167
|
+
yargs._postProcess(innerArgv, populateDoubleDash);
|
|
153
168
|
innerArgv = applyMiddleware(innerArgv, yargs, middlewares, false);
|
|
154
169
|
let handlerResult;
|
|
155
170
|
if (isPromise(innerArgv)) {
|
|
@@ -194,12 +209,16 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
194
209
|
return innerArgv;
|
|
195
210
|
};
|
|
196
211
|
function shouldUpdateUsage(yargs) {
|
|
197
|
-
return !yargs.getUsageInstance().getUsageDisabled() &&
|
|
198
|
-
yargs.getUsageInstance().getUsage().length === 0;
|
|
212
|
+
return (!yargs.getUsageInstance().getUsageDisabled() &&
|
|
213
|
+
yargs.getUsageInstance().getUsage().length === 0);
|
|
199
214
|
}
|
|
200
215
|
function usageFromParentCommandsCommandHandler(parentCommands, commandHandler) {
|
|
201
|
-
const c = DEFAULT_MARKER.test(commandHandler.original)
|
|
202
|
-
|
|
216
|
+
const c = DEFAULT_MARKER.test(commandHandler.original)
|
|
217
|
+
? commandHandler.original.replace(DEFAULT_MARKER, '').trim()
|
|
218
|
+
: commandHandler.original;
|
|
219
|
+
const pc = parentCommands.filter(c => {
|
|
220
|
+
return !DEFAULT_MARKER.test(c);
|
|
221
|
+
});
|
|
203
222
|
pc.push(c);
|
|
204
223
|
return `$0 ${pc.join(' ')}`;
|
|
205
224
|
}
|
|
@@ -207,15 +226,16 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
207
226
|
assertNotStrictEqual(defaultCommand, undefined, shim);
|
|
208
227
|
if (shouldUpdateUsage(yargs)) {
|
|
209
228
|
const commandString = DEFAULT_MARKER.test(defaultCommand.original)
|
|
210
|
-
? defaultCommand.original
|
|
229
|
+
? defaultCommand.original
|
|
230
|
+
: defaultCommand.original.replace(/^[^[\]<>]*/, '$0 ');
|
|
211
231
|
yargs.getUsageInstance().usage(commandString, defaultCommand.description);
|
|
212
232
|
}
|
|
213
233
|
const builder = defaultCommand.builder;
|
|
214
234
|
if (isCommandBuilderCallback(builder)) {
|
|
215
235
|
builder(yargs);
|
|
216
236
|
}
|
|
217
|
-
else {
|
|
218
|
-
Object.keys(builder).forEach(
|
|
237
|
+
else if (!isCommandBuilderDefinition(builder)) {
|
|
238
|
+
Object.keys(builder).forEach(key => {
|
|
219
239
|
yargs.option(key, builder[key]);
|
|
220
240
|
});
|
|
221
241
|
}
|
|
@@ -257,8 +277,8 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
257
277
|
options.array = options.array.concat(parseOptions.array);
|
|
258
278
|
options.config = {};
|
|
259
279
|
const unparsed = [];
|
|
260
|
-
Object.keys(positionalMap).forEach(
|
|
261
|
-
positionalMap[key].map(
|
|
280
|
+
Object.keys(positionalMap).forEach(key => {
|
|
281
|
+
positionalMap[key].map(value => {
|
|
262
282
|
if (options.configuration['unknown-options-as-args'])
|
|
263
283
|
options.key[key] = true;
|
|
264
284
|
unparsed.push(`--${key}`);
|
|
@@ -268,20 +288,20 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
268
288
|
if (!unparsed.length)
|
|
269
289
|
return;
|
|
270
290
|
const config = Object.assign({}, options.configuration, {
|
|
271
|
-
'populate--': true
|
|
291
|
+
'populate--': true,
|
|
272
292
|
});
|
|
273
293
|
const parsed = shim.Parser.detailed(unparsed, Object.assign({}, options, {
|
|
274
|
-
configuration: config
|
|
294
|
+
configuration: config,
|
|
275
295
|
}));
|
|
276
296
|
if (parsed.error) {
|
|
277
297
|
yargs.getUsageInstance().fail(parsed.error.message, parsed.error);
|
|
278
298
|
}
|
|
279
299
|
else {
|
|
280
300
|
const positionalKeys = Object.keys(positionalMap);
|
|
281
|
-
Object.keys(positionalMap).forEach(
|
|
301
|
+
Object.keys(positionalMap).forEach(key => {
|
|
282
302
|
positionalKeys.push(...parsed.aliases[key]);
|
|
283
303
|
});
|
|
284
|
-
Object.keys(parsed.argv).forEach(
|
|
304
|
+
Object.keys(parsed.argv).forEach(key => {
|
|
285
305
|
if (positionalKeys.indexOf(key) !== -1) {
|
|
286
306
|
if (!positionalMap[key])
|
|
287
307
|
positionalMap[key] = parsed.argv[key];
|
|
@@ -295,10 +315,10 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
295
315
|
array: [],
|
|
296
316
|
default: {},
|
|
297
317
|
alias: {},
|
|
298
|
-
demand: {}
|
|
318
|
+
demand: {},
|
|
299
319
|
};
|
|
300
320
|
const parsed = parseCommand(cmdString);
|
|
301
|
-
parsed.demanded.forEach(
|
|
321
|
+
parsed.demanded.forEach(d => {
|
|
302
322
|
const [cmd, ...aliases] = d.cmd;
|
|
303
323
|
if (d.variadic) {
|
|
304
324
|
parseOptions.array.push(cmd);
|
|
@@ -307,7 +327,7 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
307
327
|
parseOptions.alias[cmd] = aliases;
|
|
308
328
|
parseOptions.demand[cmd] = true;
|
|
309
329
|
});
|
|
310
|
-
parsed.optional.forEach(
|
|
330
|
+
parsed.optional.forEach(o => {
|
|
311
331
|
const [cmd, ...aliases] = o.cmd;
|
|
312
332
|
if (o.variadic) {
|
|
313
333
|
parseOptions.array.push(cmd);
|
|
@@ -328,27 +348,28 @@ export function command(yargs, usage, validation, globalMiddleware = [], shim) {
|
|
|
328
348
|
frozens.push({
|
|
329
349
|
handlers,
|
|
330
350
|
aliasMap,
|
|
331
|
-
defaultCommand
|
|
351
|
+
defaultCommand,
|
|
332
352
|
});
|
|
333
353
|
};
|
|
334
354
|
self.unfreeze = () => {
|
|
335
355
|
const frozen = frozens.pop();
|
|
336
356
|
assertNotStrictEqual(frozen, undefined, shim);
|
|
337
|
-
({
|
|
338
|
-
handlers,
|
|
339
|
-
aliasMap,
|
|
340
|
-
defaultCommand
|
|
341
|
-
} = frozen);
|
|
357
|
+
({ handlers, aliasMap, defaultCommand } = frozen);
|
|
342
358
|
};
|
|
343
359
|
return self;
|
|
344
360
|
}
|
|
345
|
-
export function isCommandHandlerDefinition(cmd) {
|
|
346
|
-
return typeof cmd === 'object';
|
|
347
|
-
}
|
|
348
361
|
export function isCommandBuilderDefinition(builder) {
|
|
349
|
-
return typeof builder === 'object' &&
|
|
362
|
+
return (typeof builder === 'object' &&
|
|
350
363
|
!!builder.builder &&
|
|
351
|
-
typeof builder.handler === 'function';
|
|
364
|
+
typeof builder.handler === 'function');
|
|
365
|
+
}
|
|
366
|
+
function isCommandAndAliases(cmd) {
|
|
367
|
+
if (cmd.every(c => typeof c === 'string')) {
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
352
373
|
}
|
|
353
374
|
export function isCommandBuilderCallback(builder) {
|
|
354
375
|
return typeof builder === 'function';
|
|
@@ -356,3 +377,6 @@ export function isCommandBuilderCallback(builder) {
|
|
|
356
377
|
function isCommandBuilderOptionDefinitions(builder) {
|
|
357
378
|
return typeof builder === 'object';
|
|
358
379
|
}
|
|
380
|
+
export function isCommandHandlerDefinition(cmd) {
|
|
381
|
+
return typeof cmd === 'object' && !Array.isArray(cmd);
|
|
382
|
+
}
|
package/build/lib/completion.js
CHANGED
|
@@ -5,7 +5,7 @@ import { isPromise } from './utils/is-promise.js';
|
|
|
5
5
|
import { parseCommand } from './parse-command.js';
|
|
6
6
|
export function completion(yargs, usage, command, shim) {
|
|
7
7
|
const self = {
|
|
8
|
-
completionKey: 'get-yargs-completions'
|
|
8
|
+
completionKey: 'get-yargs-completions',
|
|
9
9
|
};
|
|
10
10
|
let aliases;
|
|
11
11
|
self.setParsed = function setParsed(parsed) {
|
|
@@ -23,22 +23,30 @@ export function completion(yargs, usage, command, shim) {
|
|
|
23
23
|
if (isSyncCompletionFunction(completionFunction)) {
|
|
24
24
|
const result = completionFunction(current, argv);
|
|
25
25
|
if (isPromise(result)) {
|
|
26
|
-
return result
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
return result
|
|
27
|
+
.then(list => {
|
|
28
|
+
shim.process.nextTick(() => {
|
|
29
|
+
done(list);
|
|
30
|
+
});
|
|
31
|
+
})
|
|
32
|
+
.catch(err => {
|
|
33
|
+
shim.process.nextTick(() => {
|
|
34
|
+
throw err;
|
|
35
|
+
});
|
|
30
36
|
});
|
|
31
37
|
}
|
|
32
38
|
return done(result);
|
|
33
39
|
}
|
|
34
40
|
else {
|
|
35
|
-
return completionFunction(current, argv,
|
|
41
|
+
return completionFunction(current, argv, completions => {
|
|
36
42
|
done(completions);
|
|
37
43
|
});
|
|
38
44
|
}
|
|
39
45
|
}
|
|
40
46
|
if (completionFunction) {
|
|
41
|
-
return isPromise(argv)
|
|
47
|
+
return isPromise(argv)
|
|
48
|
+
? argv.then(runCompletionFunction)
|
|
49
|
+
: runCompletionFunction(argv);
|
|
42
50
|
}
|
|
43
51
|
const handlers = command.getCommandHandlers();
|
|
44
52
|
for (let i = 0, ii = args.length; i < ii; ++i) {
|
|
@@ -51,8 +59,9 @@ export function completion(yargs, usage, command, shim) {
|
|
|
51
59
|
}
|
|
52
60
|
}
|
|
53
61
|
}
|
|
54
|
-
if (!current.match(/^-/) &&
|
|
55
|
-
|
|
62
|
+
if (!current.match(/^-/) &&
|
|
63
|
+
parentCommands[parentCommands.length - 1] !== current) {
|
|
64
|
+
usage.getCommands().forEach(usageCommand => {
|
|
56
65
|
const commandName = parseCommand(usageCommand[0]).cmd;
|
|
57
66
|
if (args.indexOf(commandName) === -1) {
|
|
58
67
|
if (!zshShell) {
|
|
@@ -68,8 +77,9 @@ export function completion(yargs, usage, command, shim) {
|
|
|
68
77
|
if (current.match(/^-/) || (current === '' && completions.length === 0)) {
|
|
69
78
|
const descs = usage.getDescriptions();
|
|
70
79
|
const options = yargs.getOptions();
|
|
71
|
-
Object.keys(options.key).forEach(
|
|
72
|
-
const negable = !!options.configuration['boolean-negation'] &&
|
|
80
|
+
Object.keys(options.key).forEach(key => {
|
|
81
|
+
const negable = !!options.configuration['boolean-negation'] &&
|
|
82
|
+
options.boolean.includes(key);
|
|
73
83
|
let keyAndAliases = [key].concat(aliases[key] || []);
|
|
74
84
|
if (negable)
|
|
75
85
|
keyAndAliases = keyAndAliases.concat(keyAndAliases.map(key => `no-${key}`));
|
|
@@ -84,7 +94,8 @@ export function completion(yargs, usage, command, shim) {
|
|
|
84
94
|
}
|
|
85
95
|
else {
|
|
86
96
|
const desc = descs[key] || '';
|
|
87
|
-
completions.push(dashes +
|
|
97
|
+
completions.push(dashes +
|
|
98
|
+
`${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`);
|
|
88
99
|
}
|
|
89
100
|
}
|
|
90
101
|
}
|
|
@@ -96,7 +107,9 @@ export function completion(yargs, usage, command, shim) {
|
|
|
96
107
|
done(completions);
|
|
97
108
|
};
|
|
98
109
|
self.generateCompletionScript = function generateCompletionScript($0, cmd) {
|
|
99
|
-
let script = zshShell
|
|
110
|
+
let script = zshShell
|
|
111
|
+
? templates.completionZshTemplate
|
|
112
|
+
: templates.completionShTemplate;
|
|
100
113
|
const name = shim.path.basename($0);
|
|
101
114
|
if ($0.match(/\.js$/))
|
|
102
115
|
$0 = `./${$0}`;
|
|
@@ -105,7 +118,7 @@ export function completion(yargs, usage, command, shim) {
|
|
|
105
118
|
return script.replace(/{{app_path}}/g, $0);
|
|
106
119
|
};
|
|
107
120
|
let completionFunction = null;
|
|
108
|
-
self.registerFunction =
|
|
121
|
+
self.registerFunction = fn => {
|
|
109
122
|
completionFunction = fn;
|
|
110
123
|
};
|
|
111
124
|
return self;
|
package/build/lib/middleware.js
CHANGED
|
@@ -29,14 +29,16 @@ export function commandMiddlewareFactory(commandMiddleware) {
|
|
|
29
29
|
}
|
|
30
30
|
export function applyMiddleware(argv, yargs, middlewares, beforeValidation) {
|
|
31
31
|
const beforeValidationError = new Error('middleware cannot return a promise when applyBeforeValidation is true');
|
|
32
|
-
return middlewares
|
|
33
|
-
.reduce((acc, middleware) => {
|
|
32
|
+
return middlewares.reduce((acc, middleware) => {
|
|
34
33
|
if (middleware.applyBeforeValidation !== beforeValidation) {
|
|
35
34
|
return acc;
|
|
36
35
|
}
|
|
37
36
|
if (isPromise(acc)) {
|
|
38
37
|
return acc
|
|
39
|
-
.then(initialObj => Promise.all([
|
|
38
|
+
.then(initialObj => Promise.all([
|
|
39
|
+
initialObj,
|
|
40
|
+
middleware(initialObj, yargs),
|
|
41
|
+
]))
|
|
40
42
|
.then(([initialObj, middlewareObj]) => Object.assign(initialObj, middlewareObj));
|
|
41
43
|
}
|
|
42
44
|
else {
|
|
@@ -8,7 +8,7 @@ export function parseCommand(cmd) {
|
|
|
8
8
|
const parsedCommand = {
|
|
9
9
|
cmd: firstCommand.replace(bregex, ''),
|
|
10
10
|
demanded: [],
|
|
11
|
-
optional: []
|
|
11
|
+
optional: [],
|
|
12
12
|
};
|
|
13
13
|
splitCommand.forEach((cmd, i) => {
|
|
14
14
|
let variadic = false;
|
|
@@ -18,13 +18,13 @@ export function parseCommand(cmd) {
|
|
|
18
18
|
if (/^\[/.test(cmd)) {
|
|
19
19
|
parsedCommand.optional.push({
|
|
20
20
|
cmd: cmd.replace(bregex, '').split('|'),
|
|
21
|
-
variadic
|
|
21
|
+
variadic,
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
25
|
parsedCommand.demanded.push({
|
|
26
26
|
cmd: cmd.replace(bregex, '').split('|'),
|
|
27
|
-
variadic
|
|
27
|
+
variadic,
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
});
|