burgee 0.0.0 → 0.3.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/README.md +28 -1
- package/dist/agent.d.ts +19 -0
- package/dist/agent.js +25 -0
- package/dist/brand.d.ts +186 -0
- package/dist/brand.js +232 -0
- package/dist/cli.d.ts +62 -0
- package/dist/cli.js +121 -0
- package/dist/commander-argument.d.ts +22 -0
- package/dist/commander-argument.js +72 -0
- package/dist/commander-command.d.ts +355 -0
- package/dist/commander-command.js +1621 -0
- package/dist/commander-error.d.ts +10 -0
- package/dist/commander-error.js +20 -0
- package/dist/commander-help.d.ts +67 -0
- package/dist/commander-help.js +319 -0
- package/dist/commander-option.d.ts +58 -0
- package/dist/commander-option.js +164 -0
- package/dist/commander-suggest.d.ts +2 -0
- package/dist/commander-suggest.js +59 -0
- package/dist/commander.d.ts +18 -0
- package/dist/commander.js +12 -0
- package/dist/completions.d.ts +39 -0
- package/dist/completions.js +224 -0
- package/dist/config.d.ts +28 -0
- package/dist/config.js +98 -0
- package/dist/contrast.d.ts +70 -0
- package/dist/contrast.js +93 -0
- package/dist/dev.d.ts +47 -0
- package/dist/dev.js +132 -0
- package/dist/execute.d.ts +118 -0
- package/dist/execute.js +469 -0
- package/dist/exit-code.d.ts +18 -0
- package/dist/exit-code.js +12 -0
- package/dist/help.d.ts +34 -0
- package/dist/help.js +187 -0
- package/dist/index.d.ts +16 -1
- package/dist/index.js +9 -2
- package/dist/manifest.d.ts +207 -0
- package/dist/manifest.js +66 -0
- package/dist/mcp.d.ts +52 -0
- package/dist/mcp.js +124 -0
- package/dist/names.d.ts +5 -0
- package/dist/names.js +6 -0
- package/dist/pkg.d.ts +5 -0
- package/dist/pkg.js +21 -0
- package/dist/precedence.d.ts +55 -0
- package/dist/precedence.js +100 -0
- package/dist/runtime.d.ts +39 -0
- package/dist/runtime.js +24 -0
- package/dist/schema.d.ts +79 -0
- package/dist/schema.js +116 -0
- package/dist/testing-helpers.d.ts +79 -0
- package/dist/testing-helpers.js +145 -0
- package/dist/testing.d.ts +10 -0
- package/dist/testing.js +3 -0
- package/dist/validate.d.ts +27 -0
- package/dist/validate.js +133 -0
- package/dist/yargs-burgee.d.ts +50 -0
- package/dist/yargs-burgee.js +104 -0
- package/dist/yargs-cliui.d.ts +56 -0
- package/dist/yargs-cliui.js +421 -0
- package/dist/yargs-command.d.ts +82 -0
- package/dist/yargs-command.js +414 -0
- package/dist/yargs-completion.d.ts +41 -0
- package/dist/yargs-completion.js +271 -0
- package/dist/yargs-factory.d.ts +193 -0
- package/dist/yargs-factory.js +1606 -0
- package/dist/yargs-helpers.d.ts +6 -0
- package/dist/yargs-helpers.js +2 -0
- package/dist/yargs-middleware.d.ts +32 -0
- package/dist/yargs-middleware.js +81 -0
- package/dist/yargs-parser.d.ts +41 -0
- package/dist/yargs-parser.js +929 -0
- package/dist/yargs-shim.d.ts +54 -0
- package/dist/yargs-shim.js +84 -0
- package/dist/yargs-usage.d.ts +42 -0
- package/dist/yargs-usage.js +479 -0
- package/dist/yargs-utils.d.ts +33 -0
- package/dist/yargs-utils.js +209 -0
- package/dist/yargs-validation.d.ts +26 -0
- package/dist/yargs-validation.js +261 -0
- package/dist/yargs-y18n.d.ts +21 -0
- package/dist/yargs-y18n.js +117 -0
- package/dist/yargs.d.ts +6 -0
- package/dist/yargs.js +8 -0
- package/locales/be.json +46 -0
- package/locales/cs.json +51 -0
- package/locales/de.json +46 -0
- package/locales/en.json +55 -0
- package/locales/es.json +46 -0
- package/locales/fi.json +49 -0
- package/locales/fr.json +53 -0
- package/locales/he.json +55 -0
- package/locales/hi.json +49 -0
- package/locales/hu.json +46 -0
- package/locales/id.json +50 -0
- package/locales/it.json +46 -0
- package/locales/ja.json +51 -0
- package/locales/ka.json +55 -0
- package/locales/ko.json +49 -0
- package/locales/nb.json +44 -0
- package/locales/nl.json +49 -0
- package/locales/nn.json +44 -0
- package/locales/pirate.json +13 -0
- package/locales/pl.json +49 -0
- package/locales/pt.json +45 -0
- package/locales/pt_BR.json +48 -0
- package/locales/ru.json +51 -0
- package/locales/th.json +46 -0
- package/locales/tr.json +48 -0
- package/locales/uk_UA.json +51 -0
- package/locales/uz.json +52 -0
- package/locales/zh_CN.json +48 -0
- package/locales/zh_TW.json +51 -0
- package/package.json +62 -9
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { applyMiddleware, commandMiddlewareFactory } from './yargs-middleware.js';
|
|
2
|
+
import { isPromise, maybeAsyncResult, parseCommand } from './yargs-utils.js';
|
|
3
|
+
const DEFAULT_MARKER = /(^\*)|(^\$0)/;
|
|
4
|
+
export function isYargsInstance(y) {
|
|
5
|
+
return !!y && typeof y.getInternalMethods === 'function';
|
|
6
|
+
}
|
|
7
|
+
export class CommandInstance {
|
|
8
|
+
requireCache = new Set();
|
|
9
|
+
handlers = {};
|
|
10
|
+
aliasMap = {};
|
|
11
|
+
defaultCommand;
|
|
12
|
+
frozens = [];
|
|
13
|
+
shim;
|
|
14
|
+
usage;
|
|
15
|
+
globalMiddleware;
|
|
16
|
+
validation;
|
|
17
|
+
constructor(usage, validation, globalMiddleware, shim) {
|
|
18
|
+
this.shim = shim;
|
|
19
|
+
this.usage = usage;
|
|
20
|
+
this.globalMiddleware = globalMiddleware;
|
|
21
|
+
this.validation = validation;
|
|
22
|
+
}
|
|
23
|
+
addDirectory(dir, req, callerFile, opts) {
|
|
24
|
+
opts = opts || {};
|
|
25
|
+
this.requireCache.add(callerFile);
|
|
26
|
+
const fullDirPath = this.shim.path.resolve(this.shim.path.dirname(callerFile), dir);
|
|
27
|
+
const files = this.shim.readdirSync(fullDirPath, { recursive: opts.recurse ? true : false });
|
|
28
|
+
if (!Array.isArray(opts.extensions))
|
|
29
|
+
opts.extensions = ['js'];
|
|
30
|
+
const visit = typeof opts.visit === 'function' ? opts.visit : (o) => o;
|
|
31
|
+
for (const fileb of files) {
|
|
32
|
+
const file = fileb.toString();
|
|
33
|
+
if (opts.exclude) {
|
|
34
|
+
let exclude = false;
|
|
35
|
+
if (typeof opts.exclude === 'function')
|
|
36
|
+
exclude = opts.exclude(file);
|
|
37
|
+
else
|
|
38
|
+
exclude = opts.exclude.test(file);
|
|
39
|
+
if (exclude)
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (opts.include) {
|
|
43
|
+
let include = false;
|
|
44
|
+
if (typeof opts.include === 'function')
|
|
45
|
+
include = opts.include(file);
|
|
46
|
+
else
|
|
47
|
+
include = opts.include.test(file);
|
|
48
|
+
if (!include)
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
let supportedExtension = false;
|
|
52
|
+
for (const ext of opts.extensions) {
|
|
53
|
+
if (file.endsWith(ext))
|
|
54
|
+
supportedExtension = true;
|
|
55
|
+
}
|
|
56
|
+
if (supportedExtension) {
|
|
57
|
+
const joined = this.shim.path.join(fullDirPath, file);
|
|
58
|
+
const module = req(joined);
|
|
59
|
+
const extendableModule = Object.create(null, Object.getOwnPropertyDescriptors({ ...module }));
|
|
60
|
+
const visited = visit(extendableModule, joined, file);
|
|
61
|
+
if (visited) {
|
|
62
|
+
if (this.requireCache.has(joined))
|
|
63
|
+
continue;
|
|
64
|
+
else
|
|
65
|
+
this.requireCache.add(joined);
|
|
66
|
+
if (!extendableModule.command)
|
|
67
|
+
extendableModule.command = this.shim.path.basename(joined, this.shim.path.extname(joined));
|
|
68
|
+
this.addHandler(extendableModule);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
addHandler(cmd, description, builder, handler, commandMiddleware, deprecated) {
|
|
74
|
+
let aliases = [];
|
|
75
|
+
const middlewares = commandMiddlewareFactory(commandMiddleware);
|
|
76
|
+
handler = handler || (() => { });
|
|
77
|
+
if (Array.isArray(cmd)) {
|
|
78
|
+
if (isCommandAndAliases(cmd))
|
|
79
|
+
[cmd, ...aliases] = cmd;
|
|
80
|
+
else {
|
|
81
|
+
for (const command of cmd)
|
|
82
|
+
this.addHandler(command);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else if (isCommandHandlerDefinition(cmd)) {
|
|
86
|
+
let command = Array.isArray(cmd.command) || typeof cmd.command === 'string' ? cmd.command : null;
|
|
87
|
+
if (command === null)
|
|
88
|
+
throw new Error(`No command name given for module: ${this.shim.inspect(cmd)}`);
|
|
89
|
+
if (cmd.aliases)
|
|
90
|
+
command = [].concat(command).concat(cmd.aliases);
|
|
91
|
+
this.addHandler(command, this.extractDesc(cmd), cmd.builder, cmd.handler, cmd.middlewares, cmd.deprecated);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
else if (isCommandBuilderDefinition(builder)) {
|
|
95
|
+
this.addHandler([cmd].concat(aliases), description, builder.builder, builder.handler, builder.middlewares, builder.deprecated);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (typeof cmd === 'string') {
|
|
99
|
+
const parsedCommand = parseCommand(cmd);
|
|
100
|
+
aliases = aliases.map((alias) => parseCommand(alias).cmd);
|
|
101
|
+
let isDefault = false;
|
|
102
|
+
const parsedAliases = [parsedCommand.cmd].concat(aliases).filter((c) => {
|
|
103
|
+
if (DEFAULT_MARKER.test(c)) {
|
|
104
|
+
isDefault = true;
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
});
|
|
109
|
+
if (parsedAliases.length === 0 && isDefault)
|
|
110
|
+
parsedAliases.push('$0');
|
|
111
|
+
if (isDefault) {
|
|
112
|
+
parsedCommand.cmd = parsedAliases[0];
|
|
113
|
+
aliases = parsedAliases.slice(1);
|
|
114
|
+
cmd = cmd.replace(DEFAULT_MARKER, parsedCommand.cmd);
|
|
115
|
+
}
|
|
116
|
+
aliases.forEach((alias) => {
|
|
117
|
+
this.aliasMap[alias] = parsedCommand.cmd;
|
|
118
|
+
});
|
|
119
|
+
if (description !== false)
|
|
120
|
+
this.usage.command(cmd, description, isDefault, aliases, deprecated);
|
|
121
|
+
this.handlers[parsedCommand.cmd] = {
|
|
122
|
+
original: cmd,
|
|
123
|
+
description,
|
|
124
|
+
handler,
|
|
125
|
+
builder: builder || {},
|
|
126
|
+
middlewares,
|
|
127
|
+
deprecated,
|
|
128
|
+
demanded: parsedCommand.demanded,
|
|
129
|
+
optional: parsedCommand.optional,
|
|
130
|
+
};
|
|
131
|
+
if (isDefault)
|
|
132
|
+
this.defaultCommand = this.handlers[parsedCommand.cmd];
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
getCommandHandlers() {
|
|
136
|
+
return this.handlers;
|
|
137
|
+
}
|
|
138
|
+
getCommands() {
|
|
139
|
+
return Object.keys(this.handlers).concat(Object.keys(this.aliasMap));
|
|
140
|
+
}
|
|
141
|
+
hasDefaultCommand() {
|
|
142
|
+
return !!this.defaultCommand;
|
|
143
|
+
}
|
|
144
|
+
runCommand(command, yargs, parsed, commandIndex, helpOnly, helpOrVersionSet) {
|
|
145
|
+
const commandHandler = (this.handlers[command] || this.handlers[this.aliasMap[command]] || this.defaultCommand);
|
|
146
|
+
const currentContext = yargs.getInternalMethods().getContext();
|
|
147
|
+
const parentCommands = currentContext.commands.slice();
|
|
148
|
+
const isDefaultCommand = !command;
|
|
149
|
+
if (command) {
|
|
150
|
+
currentContext.commands.push(command);
|
|
151
|
+
currentContext.fullCommands.push(commandHandler.original);
|
|
152
|
+
}
|
|
153
|
+
const builderResult = this.applyBuilderUpdateUsageAndParse(isDefaultCommand, commandHandler, yargs, parsed.aliases, parentCommands, commandIndex, helpOnly, helpOrVersionSet);
|
|
154
|
+
return isPromise(builderResult)
|
|
155
|
+
? builderResult.then((result) => this.applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, result.innerArgv, currentContext, helpOnly, result.aliases, yargs))
|
|
156
|
+
: this.applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, builderResult.innerArgv, currentContext, helpOnly, builderResult.aliases, yargs);
|
|
157
|
+
}
|
|
158
|
+
applyBuilderUpdateUsageAndParse(isDefaultCommand, commandHandler, yargs, aliases, parentCommands, commandIndex, helpOnly, helpOrVersionSet) {
|
|
159
|
+
const builder = commandHandler.builder;
|
|
160
|
+
let innerYargs = yargs;
|
|
161
|
+
if (isCommandBuilderCallback(builder)) {
|
|
162
|
+
yargs.getInternalMethods().getUsageInstance().freeze();
|
|
163
|
+
const builderOutput = builder(yargs.getInternalMethods().reset(aliases), helpOrVersionSet);
|
|
164
|
+
if (isPromise(builderOutput)) {
|
|
165
|
+
return builderOutput.then((output) => {
|
|
166
|
+
innerYargs = isYargsInstance(output) ? output : yargs;
|
|
167
|
+
return this.parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else if (isCommandBuilderOptionDefinitions(builder)) {
|
|
172
|
+
yargs.getInternalMethods().getUsageInstance().freeze();
|
|
173
|
+
innerYargs = yargs.getInternalMethods().reset(aliases);
|
|
174
|
+
Object.keys(commandHandler.builder).forEach((key) => {
|
|
175
|
+
innerYargs.option(key, builder[key]);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return this.parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly);
|
|
179
|
+
}
|
|
180
|
+
parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly) {
|
|
181
|
+
if (isDefaultCommand)
|
|
182
|
+
innerYargs.getInternalMethods().getUsageInstance().unfreeze(true);
|
|
183
|
+
if (this.shouldUpdateUsage(innerYargs)) {
|
|
184
|
+
innerYargs.getInternalMethods().getUsageInstance().usage(this.usageFromParentCommandsCommandHandler(parentCommands, commandHandler), commandHandler.description);
|
|
185
|
+
}
|
|
186
|
+
const innerArgv = innerYargs.getInternalMethods().runYargsParserAndExecuteCommands(null, undefined, true, commandIndex, helpOnly);
|
|
187
|
+
return isPromise(innerArgv)
|
|
188
|
+
? innerArgv.then((argv) => ({ aliases: innerYargs.parsed.aliases, innerArgv: argv }))
|
|
189
|
+
: { aliases: innerYargs.parsed.aliases, innerArgv };
|
|
190
|
+
}
|
|
191
|
+
shouldUpdateUsage(yargs) {
|
|
192
|
+
return !yargs.getInternalMethods().getUsageInstance().getUsageDisabled() && yargs.getInternalMethods().getUsageInstance().getUsage().length === 0;
|
|
193
|
+
}
|
|
194
|
+
usageFromParentCommandsCommandHandler(parentCommands, commandHandler) {
|
|
195
|
+
const c = DEFAULT_MARKER.test(commandHandler.original) ? commandHandler.original.replace(DEFAULT_MARKER, '').trim() : commandHandler.original;
|
|
196
|
+
const pc = parentCommands.filter((p) => !DEFAULT_MARKER.test(p));
|
|
197
|
+
pc.push(c);
|
|
198
|
+
return `$0 ${pc.join(' ')}`;
|
|
199
|
+
}
|
|
200
|
+
handleValidationAndGetResult(isDefaultCommand, commandHandler, innerArgv, currentContext, aliases, yargs, middlewares, positionalMap) {
|
|
201
|
+
if (!yargs.getInternalMethods().getHasOutput()) {
|
|
202
|
+
const validate = yargs.getInternalMethods().runValidation(aliases, positionalMap, yargs.parsed.error, isDefaultCommand);
|
|
203
|
+
innerArgv = maybeAsyncResult(innerArgv, (result) => {
|
|
204
|
+
validate(result);
|
|
205
|
+
return result;
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
if (commandHandler.handler && !yargs.getInternalMethods().getHasOutput()) {
|
|
209
|
+
yargs.getInternalMethods().setHasOutput();
|
|
210
|
+
const populateDoubleDash = !!yargs.getOptions().configuration['populate--'];
|
|
211
|
+
yargs.getInternalMethods().postProcess(innerArgv, populateDoubleDash, false, false);
|
|
212
|
+
innerArgv = applyMiddleware(innerArgv, yargs, middlewares, false);
|
|
213
|
+
innerArgv = maybeAsyncResult(innerArgv, (result) => {
|
|
214
|
+
const handlerResult = yargs.getInternalMethods().runHandler(commandHandler.handler, result, commandHandler.original);
|
|
215
|
+
return isPromise(handlerResult) ? handlerResult.then(() => result) : result;
|
|
216
|
+
});
|
|
217
|
+
if (!isDefaultCommand)
|
|
218
|
+
yargs.getInternalMethods().getUsageInstance().cacheHelpMessage();
|
|
219
|
+
if (isPromise(innerArgv) && !yargs.getInternalMethods().hasParseCallback()) {
|
|
220
|
+
innerArgv.catch((error) => {
|
|
221
|
+
try {
|
|
222
|
+
yargs.getInternalMethods().getUsageInstance().fail(null, error);
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (!isDefaultCommand) {
|
|
230
|
+
currentContext.commands.pop();
|
|
231
|
+
currentContext.fullCommands.pop();
|
|
232
|
+
}
|
|
233
|
+
return innerArgv;
|
|
234
|
+
}
|
|
235
|
+
applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, innerArgv, currentContext, helpOnly, aliases, yargs) {
|
|
236
|
+
let positionalMap = {};
|
|
237
|
+
if (helpOnly)
|
|
238
|
+
return innerArgv;
|
|
239
|
+
if (!yargs.getInternalMethods().getHasOutput())
|
|
240
|
+
positionalMap = this.populatePositionals(commandHandler, innerArgv, currentContext, yargs);
|
|
241
|
+
const middlewares = this.globalMiddleware.getMiddleware().slice(0).concat(commandHandler.middlewares);
|
|
242
|
+
const maybePromiseArgv = applyMiddleware(innerArgv, yargs, middlewares, true);
|
|
243
|
+
return isPromise(maybePromiseArgv)
|
|
244
|
+
? maybePromiseArgv.then((resolvedInnerArgv) => this.handleValidationAndGetResult(isDefaultCommand, commandHandler, resolvedInnerArgv, currentContext, aliases, yargs, middlewares, positionalMap))
|
|
245
|
+
: this.handleValidationAndGetResult(isDefaultCommand, commandHandler, maybePromiseArgv, currentContext, aliases, yargs, middlewares, positionalMap);
|
|
246
|
+
}
|
|
247
|
+
populatePositionals(commandHandler, argv, context, yargs) {
|
|
248
|
+
argv._ = argv._.slice(context.commands.length);
|
|
249
|
+
const demanded = commandHandler.demanded.slice(0);
|
|
250
|
+
const optional = commandHandler.optional.slice(0);
|
|
251
|
+
const positionalMap = {};
|
|
252
|
+
this.validation.positionalCount(demanded.length, argv._.length);
|
|
253
|
+
while (demanded.length) {
|
|
254
|
+
const demand = demanded.shift();
|
|
255
|
+
this.populatePositional(demand, argv, positionalMap);
|
|
256
|
+
}
|
|
257
|
+
while (optional.length) {
|
|
258
|
+
const maybe = optional.shift();
|
|
259
|
+
this.populatePositional(maybe, argv, positionalMap);
|
|
260
|
+
}
|
|
261
|
+
argv._ = context.commands.concat(argv._.map((a) => `${a}`));
|
|
262
|
+
this.postProcessPositionals(argv, positionalMap, this.cmdToParseOptions(commandHandler.original), yargs);
|
|
263
|
+
return positionalMap;
|
|
264
|
+
}
|
|
265
|
+
populatePositional(positional, argv, positionalMap) {
|
|
266
|
+
const cmd = positional.cmd[0];
|
|
267
|
+
if (positional.variadic)
|
|
268
|
+
positionalMap[cmd] = argv._.splice(0).map(String);
|
|
269
|
+
else if (argv._.length)
|
|
270
|
+
positionalMap[cmd] = [String(argv._.shift())];
|
|
271
|
+
}
|
|
272
|
+
cmdToParseOptions(cmdString) {
|
|
273
|
+
const parseOptions = {
|
|
274
|
+
array: [],
|
|
275
|
+
default: {},
|
|
276
|
+
alias: {},
|
|
277
|
+
demand: {},
|
|
278
|
+
};
|
|
279
|
+
const parsed = parseCommand(cmdString);
|
|
280
|
+
parsed.demanded.forEach((d) => {
|
|
281
|
+
const [cmd, ...aliases] = d.cmd;
|
|
282
|
+
if (d.variadic) {
|
|
283
|
+
parseOptions.array.push(cmd);
|
|
284
|
+
parseOptions.default[cmd] = [];
|
|
285
|
+
}
|
|
286
|
+
parseOptions.alias[cmd] = aliases;
|
|
287
|
+
parseOptions.demand[cmd] = true;
|
|
288
|
+
});
|
|
289
|
+
parsed.optional.forEach((o) => {
|
|
290
|
+
const [cmd, ...aliases] = o.cmd;
|
|
291
|
+
if (o.variadic) {
|
|
292
|
+
parseOptions.array.push(cmd);
|
|
293
|
+
parseOptions.default[cmd] = [];
|
|
294
|
+
}
|
|
295
|
+
parseOptions.alias[cmd] = aliases;
|
|
296
|
+
});
|
|
297
|
+
return parseOptions;
|
|
298
|
+
}
|
|
299
|
+
postProcessPositionals(argv, positionalMap, parseOptions, yargs) {
|
|
300
|
+
const options = Object.assign({}, yargs.getOptions());
|
|
301
|
+
options.default = Object.assign(parseOptions.default, options.default);
|
|
302
|
+
for (const key of Object.keys(parseOptions.alias)) {
|
|
303
|
+
options.alias[key] = (options.alias[key] || []).concat(parseOptions.alias[key]);
|
|
304
|
+
}
|
|
305
|
+
options.array = options.array.concat(parseOptions.array);
|
|
306
|
+
options.config = {};
|
|
307
|
+
const unparsed = [];
|
|
308
|
+
Object.keys(positionalMap).forEach((key) => {
|
|
309
|
+
positionalMap[key].map((value) => {
|
|
310
|
+
if (options.configuration['unknown-options-as-args'])
|
|
311
|
+
options.key[key] = true;
|
|
312
|
+
unparsed.push(`--${key}`);
|
|
313
|
+
unparsed.push(value);
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
if (!unparsed.length)
|
|
317
|
+
return;
|
|
318
|
+
const config = Object.assign({}, options.configuration, { 'populate--': false });
|
|
319
|
+
const parsed = this.shim.Parser.detailed(unparsed, Object.assign({}, options, { configuration: config }));
|
|
320
|
+
if (parsed.error) {
|
|
321
|
+
yargs.getInternalMethods().getUsageInstance().fail(parsed.error.message, parsed.error);
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
const positionalKeys = Object.keys(positionalMap);
|
|
325
|
+
Object.keys(positionalMap).forEach((key) => {
|
|
326
|
+
positionalKeys.push(...parsed.aliases[key]);
|
|
327
|
+
});
|
|
328
|
+
Object.keys(parsed.argv).forEach((key) => {
|
|
329
|
+
if (positionalKeys.includes(key)) {
|
|
330
|
+
if (!positionalMap[key])
|
|
331
|
+
positionalMap[key] = parsed.argv[key];
|
|
332
|
+
if (!this.isInConfigs(yargs, key) &&
|
|
333
|
+
!this.isDefaulted(yargs, key) &&
|
|
334
|
+
Object.prototype.hasOwnProperty.call(argv, key) &&
|
|
335
|
+
Object.prototype.hasOwnProperty.call(parsed.argv, key) &&
|
|
336
|
+
(Array.isArray(argv[key]) || Array.isArray(parsed.argv[key]))) {
|
|
337
|
+
argv[key] = [].concat(argv[key], parsed.argv[key]);
|
|
338
|
+
}
|
|
339
|
+
else
|
|
340
|
+
argv[key] = parsed.argv[key];
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
isDefaulted(yargs, key) {
|
|
346
|
+
const { default: defaults } = yargs.getOptions();
|
|
347
|
+
return Object.prototype.hasOwnProperty.call(defaults, key) || Object.prototype.hasOwnProperty.call(defaults, this.shim.Parser.camelCase(key));
|
|
348
|
+
}
|
|
349
|
+
isInConfigs(yargs, key) {
|
|
350
|
+
const { configObjects } = yargs.getOptions();
|
|
351
|
+
return (configObjects.some((c) => Object.prototype.hasOwnProperty.call(c, key)) ||
|
|
352
|
+
configObjects.some((c) => Object.prototype.hasOwnProperty.call(c, this.shim.Parser.camelCase(key))));
|
|
353
|
+
}
|
|
354
|
+
runDefaultBuilderOn(yargs) {
|
|
355
|
+
if (!this.defaultCommand)
|
|
356
|
+
return;
|
|
357
|
+
if (this.shouldUpdateUsage(yargs)) {
|
|
358
|
+
const commandString = DEFAULT_MARKER.test(this.defaultCommand.original)
|
|
359
|
+
? this.defaultCommand.original
|
|
360
|
+
: this.defaultCommand.original.replace(/^[^[\]<>]*/, '$0 ');
|
|
361
|
+
yargs.getInternalMethods().getUsageInstance().usage(commandString, this.defaultCommand.description);
|
|
362
|
+
}
|
|
363
|
+
const builder = this.defaultCommand.builder;
|
|
364
|
+
if (isCommandBuilderCallback(builder))
|
|
365
|
+
return builder(yargs, true);
|
|
366
|
+
else if (!isCommandBuilderDefinition(builder)) {
|
|
367
|
+
Object.keys(builder).forEach((key) => {
|
|
368
|
+
yargs.option(key, builder[key]);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
373
|
+
extractDesc({ describe, description, desc }) {
|
|
374
|
+
for (const test of [describe, description, desc]) {
|
|
375
|
+
if (typeof test === 'string' || test === false)
|
|
376
|
+
return test;
|
|
377
|
+
this.shim.assert.notStrictEqual(test, true);
|
|
378
|
+
}
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
freeze() {
|
|
382
|
+
this.frozens.push({ handlers: this.handlers, aliasMap: this.aliasMap, defaultCommand: this.defaultCommand });
|
|
383
|
+
}
|
|
384
|
+
unfreeze() {
|
|
385
|
+
const frozen = this.frozens.pop();
|
|
386
|
+
this.shim.assert.notStrictEqual(frozen, undefined);
|
|
387
|
+
({ handlers: this.handlers, aliasMap: this.aliasMap, defaultCommand: this.defaultCommand } = frozen);
|
|
388
|
+
}
|
|
389
|
+
reset() {
|
|
390
|
+
this.handlers = {};
|
|
391
|
+
this.aliasMap = {};
|
|
392
|
+
this.defaultCommand = undefined;
|
|
393
|
+
this.requireCache = new Set();
|
|
394
|
+
return this;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
export function command(usage, validation, globalMiddleware, shim) {
|
|
398
|
+
return new CommandInstance(usage, validation, globalMiddleware, shim);
|
|
399
|
+
}
|
|
400
|
+
export function isCommandBuilderDefinition(builder) {
|
|
401
|
+
return typeof builder === 'object' && !!builder.builder && typeof builder.handler === 'function';
|
|
402
|
+
}
|
|
403
|
+
function isCommandAndAliases(cmd) {
|
|
404
|
+
return cmd.every((c) => typeof c === 'string');
|
|
405
|
+
}
|
|
406
|
+
export function isCommandBuilderCallback(builder) {
|
|
407
|
+
return typeof builder === 'function';
|
|
408
|
+
}
|
|
409
|
+
function isCommandBuilderOptionDefinitions(builder) {
|
|
410
|
+
return typeof builder === 'object';
|
|
411
|
+
}
|
|
412
|
+
export function isCommandHandlerDefinition(cmd) {
|
|
413
|
+
return typeof cmd === 'object' && !Array.isArray(cmd);
|
|
414
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* yargs' completion — `--get-yargs-completions`, the custom completion function in its
|
|
3
|
+
* three arities, and the bash/zsh script templates — ported for `burgee/yargs`.
|
|
4
|
+
*/
|
|
5
|
+
import { type CommandInstance } from './yargs-command.js';
|
|
6
|
+
import type { PlatformShim } from './yargs-shim.js';
|
|
7
|
+
import type { UsageInstance } from './yargs-usage.js';
|
|
8
|
+
export declare const completionShTemplate = "###-begin-{{app_name}}-completions-###\n#\n# yargs command completion script\n#\n# Installation: {{app_path}} {{completion_command}} >> ~/.bashrc\n# or {{app_path}} {{completion_command}} >> ~/.bash_profile on OSX.\n#\n_{{app_name}}_yargs_completions()\n{\n local cur_word args type_list\n\n cur_word=\"${COMP_WORDS[COMP_CWORD]}\"\n args=(\"${COMP_WORDS[@]}\")\n\n # ask yargs to generate completions.\n # see https://stackoverflow.com/a/40944195/7080036 for the spaces-handling awk\n mapfile -t type_list < <({{app_path}} --get-yargs-completions \"${args[@]}\")\n mapfile -t COMPREPLY < <(compgen -W \"$( printf '%q ' \"${type_list[@]}\" )\" -- \"${cur_word}\" |\n awk '/ / { print \"\\\"\"$0\"\\\"\" } /^[^ ]+$/ { print $0 }')\n\n # if no match was found, fall back to filename completion\n if [ ${#COMPREPLY[@]} -eq 0 ]; then\n COMPREPLY=()\n fi\n\n return 0\n}\ncomplete -o bashdefault -o default -F _{{app_name}}_yargs_completions {{app_name}}\n###-end-{{app_name}}-completions-###\n";
|
|
9
|
+
export declare const completionZshTemplate = "#compdef {{app_name}}\n###-begin-{{app_name}}-completions-###\n#\n# yargs command completion script\n#\n# Installation: {{app_path}} {{completion_command}} >> ~/.zshrc\n# or {{app_path}} {{completion_command}} >> ~/.zprofile on OSX.\n#\n_{{app_name}}_yargs_completions()\n{\n local reply\n local si=$IFS\n IFS=$'\n' reply=($(COMP_CWORD=\"$((CURRENT-1))\" COMP_LINE=\"$BUFFER\" COMP_POINT=\"$CURSOR\" {{app_path}} --get-yargs-completions \"${words[@]}\"))\n IFS=$si\n if [[ ${#reply} -gt 0 ]]; then\n _describe 'values' reply\n else\n _default\n fi\n}\nif [[ \"'${zsh_eval_context[-1]}\" == \"loadautofunc\" ]]; then\n _{{app_name}}_yargs_completions \"$@\"\nelse\n compdef _{{app_name}}_yargs_completions {{app_name}}\nfi\n###-end-{{app_name}}-completions-###\n";
|
|
10
|
+
type Done = (err: Error | null, completions: string[] | undefined) => void;
|
|
11
|
+
export type CompletionFunction = (current: string, argv: any, ...rest: any[]) => any;
|
|
12
|
+
export declare class Completion {
|
|
13
|
+
completionKey: string;
|
|
14
|
+
private aliases;
|
|
15
|
+
private customCompletionFunction;
|
|
16
|
+
private indexAfterLastReset;
|
|
17
|
+
private readonly zshShell;
|
|
18
|
+
private readonly yargs;
|
|
19
|
+
private readonly usage;
|
|
20
|
+
private readonly command;
|
|
21
|
+
private readonly shim;
|
|
22
|
+
constructor(yargs: any, usage: UsageInstance, command: CommandInstance, shim: PlatformShim);
|
|
23
|
+
private defaultCompletion;
|
|
24
|
+
private commandCompletions;
|
|
25
|
+
private optionCompletions;
|
|
26
|
+
private choicesFromOptionsCompletions;
|
|
27
|
+
private choicesFromPositionalsCompletions;
|
|
28
|
+
private getPreviousArgChoices;
|
|
29
|
+
private previousArgHasChoices;
|
|
30
|
+
private argsContainKey;
|
|
31
|
+
private completeOptionKey;
|
|
32
|
+
private customCompletion;
|
|
33
|
+
getCompletion(args: string[], done: Done): any;
|
|
34
|
+
generateCompletionScript($0: string, cmd: string): string;
|
|
35
|
+
registerFunction(fn: CompletionFunction): void;
|
|
36
|
+
setParsed(parsed: {
|
|
37
|
+
aliases: Record<string, string[]>;
|
|
38
|
+
}): void;
|
|
39
|
+
}
|
|
40
|
+
export declare function completion(yargs: any, usage: UsageInstance, command: CommandInstance, shim: PlatformShim): Completion;
|
|
41
|
+
export {};
|