efiencrypt 0.0.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/LICENSE.md +7 -0
- package/README.md +98 -0
- package/build-laCVTTyp.js +2438 -0
- package/build.d.ts +2 -0
- package/codeBuilder.d.ts +15 -0
- package/efiencrypt +3474 -0
- package/extract-BW1V8DVW.js +12186 -0
- package/extract.d.ts +1 -0
- package/genCode.d.ts +5 -0
- package/handlers.d.ts +17 -0
- package/hexTransform.d.ts +14 -0
- package/index.d.ts +2 -0
- package/index.js +4 -0
- package/package.json +1 -0
- package/schema.json +1 -0
- package/smbios.d.ts +196 -0
- package/smbios.js +199 -0
- package/stringBuilder.d.ts +7 -0
- package/type.d.ts +103 -0
package/efiencrypt
ADDED
|
@@ -0,0 +1,3474 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import require$$0 from "node:events";
|
|
3
|
+
import require$$1 from "node:child_process";
|
|
4
|
+
import require$$2, { join, resolve } from "node:path";
|
|
5
|
+
import require$$3 from "node:fs";
|
|
6
|
+
import require$$4 from "node:process";
|
|
7
|
+
import { createRequire } from "node:module";
|
|
8
|
+
import { b as build } from "./build-laCVTTyp.js";
|
|
9
|
+
function getDefaultExportFromCjs(x) {
|
|
10
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
|
|
11
|
+
}
|
|
12
|
+
var commander$1 = {};
|
|
13
|
+
var argument = {};
|
|
14
|
+
var error = {};
|
|
15
|
+
var hasRequiredError;
|
|
16
|
+
function requireError() {
|
|
17
|
+
if (hasRequiredError) return error;
|
|
18
|
+
hasRequiredError = 1;
|
|
19
|
+
class CommanderError2 extends Error {
|
|
20
|
+
/**
|
|
21
|
+
* Constructs the CommanderError class
|
|
22
|
+
* @param {number} exitCode suggested exit code which could be used with process.exit
|
|
23
|
+
* @param {string} code an id string representing the error
|
|
24
|
+
* @param {string} message human-readable description of the error
|
|
25
|
+
*/
|
|
26
|
+
constructor(exitCode, code, message) {
|
|
27
|
+
super(message);
|
|
28
|
+
Error.captureStackTrace(this, this.constructor);
|
|
29
|
+
this.name = this.constructor.name;
|
|
30
|
+
this.code = code;
|
|
31
|
+
this.exitCode = exitCode;
|
|
32
|
+
this.nestedError = void 0;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
class InvalidArgumentError2 extends CommanderError2 {
|
|
36
|
+
/**
|
|
37
|
+
* Constructs the InvalidArgumentError class
|
|
38
|
+
* @param {string} [message] explanation of why argument is invalid
|
|
39
|
+
*/
|
|
40
|
+
constructor(message) {
|
|
41
|
+
super(1, "commander.invalidArgument", message);
|
|
42
|
+
Error.captureStackTrace(this, this.constructor);
|
|
43
|
+
this.name = this.constructor.name;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
error.CommanderError = CommanderError2;
|
|
47
|
+
error.InvalidArgumentError = InvalidArgumentError2;
|
|
48
|
+
return error;
|
|
49
|
+
}
|
|
50
|
+
var hasRequiredArgument;
|
|
51
|
+
function requireArgument() {
|
|
52
|
+
if (hasRequiredArgument) return argument;
|
|
53
|
+
hasRequiredArgument = 1;
|
|
54
|
+
const { InvalidArgumentError: InvalidArgumentError2 } = requireError();
|
|
55
|
+
class Argument2 {
|
|
56
|
+
/**
|
|
57
|
+
* Initialize a new command argument with the given name and description.
|
|
58
|
+
* The default is that the argument is required, and you can explicitly
|
|
59
|
+
* indicate this with <> around the name. Put [] around the name for an optional argument.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} name
|
|
62
|
+
* @param {string} [description]
|
|
63
|
+
*/
|
|
64
|
+
constructor(name, description) {
|
|
65
|
+
this.description = description || "";
|
|
66
|
+
this.variadic = false;
|
|
67
|
+
this.parseArg = void 0;
|
|
68
|
+
this.defaultValue = void 0;
|
|
69
|
+
this.defaultValueDescription = void 0;
|
|
70
|
+
this.argChoices = void 0;
|
|
71
|
+
switch (name[0]) {
|
|
72
|
+
case "<":
|
|
73
|
+
this.required = true;
|
|
74
|
+
this._name = name.slice(1, -1);
|
|
75
|
+
break;
|
|
76
|
+
case "[":
|
|
77
|
+
this.required = false;
|
|
78
|
+
this._name = name.slice(1, -1);
|
|
79
|
+
break;
|
|
80
|
+
default:
|
|
81
|
+
this.required = true;
|
|
82
|
+
this._name = name;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
if (this._name.endsWith("...")) {
|
|
86
|
+
this.variadic = true;
|
|
87
|
+
this._name = this._name.slice(0, -3);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Return argument name.
|
|
92
|
+
*
|
|
93
|
+
* @return {string}
|
|
94
|
+
*/
|
|
95
|
+
name() {
|
|
96
|
+
return this._name;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* @package
|
|
100
|
+
*/
|
|
101
|
+
_collectValue(value, previous) {
|
|
102
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
103
|
+
return [value];
|
|
104
|
+
}
|
|
105
|
+
previous.push(value);
|
|
106
|
+
return previous;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Set the default value, and optionally supply the description to be displayed in the help.
|
|
110
|
+
*
|
|
111
|
+
* @param {*} value
|
|
112
|
+
* @param {string} [description]
|
|
113
|
+
* @return {Argument}
|
|
114
|
+
*/
|
|
115
|
+
default(value, description) {
|
|
116
|
+
this.defaultValue = value;
|
|
117
|
+
this.defaultValueDescription = description;
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Set the custom handler for processing CLI command arguments into argument values.
|
|
122
|
+
*
|
|
123
|
+
* @param {Function} [fn]
|
|
124
|
+
* @return {Argument}
|
|
125
|
+
*/
|
|
126
|
+
argParser(fn) {
|
|
127
|
+
this.parseArg = fn;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Only allow argument value to be one of choices.
|
|
132
|
+
*
|
|
133
|
+
* @param {string[]} values
|
|
134
|
+
* @return {Argument}
|
|
135
|
+
*/
|
|
136
|
+
choices(values) {
|
|
137
|
+
this.argChoices = values.slice();
|
|
138
|
+
this.parseArg = (arg, previous) => {
|
|
139
|
+
if (!this.argChoices.includes(arg)) {
|
|
140
|
+
throw new InvalidArgumentError2(
|
|
141
|
+
`Allowed choices are ${this.argChoices.join(", ")}.`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
if (this.variadic) {
|
|
145
|
+
return this._collectValue(arg, previous);
|
|
146
|
+
}
|
|
147
|
+
return arg;
|
|
148
|
+
};
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Make argument required.
|
|
153
|
+
*
|
|
154
|
+
* @returns {Argument}
|
|
155
|
+
*/
|
|
156
|
+
argRequired() {
|
|
157
|
+
this.required = true;
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Make argument optional.
|
|
162
|
+
*
|
|
163
|
+
* @returns {Argument}
|
|
164
|
+
*/
|
|
165
|
+
argOptional() {
|
|
166
|
+
this.required = false;
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function humanReadableArgName(arg) {
|
|
171
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
172
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
173
|
+
}
|
|
174
|
+
argument.Argument = Argument2;
|
|
175
|
+
argument.humanReadableArgName = humanReadableArgName;
|
|
176
|
+
return argument;
|
|
177
|
+
}
|
|
178
|
+
var command = {};
|
|
179
|
+
var help = {};
|
|
180
|
+
var hasRequiredHelp;
|
|
181
|
+
function requireHelp() {
|
|
182
|
+
if (hasRequiredHelp) return help;
|
|
183
|
+
hasRequiredHelp = 1;
|
|
184
|
+
const { humanReadableArgName } = requireArgument();
|
|
185
|
+
class Help2 {
|
|
186
|
+
constructor() {
|
|
187
|
+
this.helpWidth = void 0;
|
|
188
|
+
this.minWidthToWrap = 40;
|
|
189
|
+
this.sortSubcommands = false;
|
|
190
|
+
this.sortOptions = false;
|
|
191
|
+
this.showGlobalOptions = false;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* prepareContext is called by Commander after applying overrides from `Command.configureHelp()`
|
|
195
|
+
* and just before calling `formatHelp()`.
|
|
196
|
+
*
|
|
197
|
+
* Commander just uses the helpWidth and the rest is provided for optional use by more complex subclasses.
|
|
198
|
+
*
|
|
199
|
+
* @param {{ error?: boolean, helpWidth?: number, outputHasColors?: boolean }} contextOptions
|
|
200
|
+
*/
|
|
201
|
+
prepareContext(contextOptions) {
|
|
202
|
+
this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one.
|
|
206
|
+
*
|
|
207
|
+
* @param {Command} cmd
|
|
208
|
+
* @returns {Command[]}
|
|
209
|
+
*/
|
|
210
|
+
visibleCommands(cmd) {
|
|
211
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
212
|
+
const helpCommand = cmd._getHelpCommand();
|
|
213
|
+
if (helpCommand && !helpCommand._hidden) {
|
|
214
|
+
visibleCommands.push(helpCommand);
|
|
215
|
+
}
|
|
216
|
+
if (this.sortSubcommands) {
|
|
217
|
+
visibleCommands.sort((a, b) => {
|
|
218
|
+
return a.name().localeCompare(b.name());
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
return visibleCommands;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Compare options for sort.
|
|
225
|
+
*
|
|
226
|
+
* @param {Option} a
|
|
227
|
+
* @param {Option} b
|
|
228
|
+
* @returns {number}
|
|
229
|
+
*/
|
|
230
|
+
compareOptions(a, b) {
|
|
231
|
+
const getSortKey = (option2) => {
|
|
232
|
+
return option2.short ? option2.short.replace(/^-/, "") : option2.long.replace(/^--/, "");
|
|
233
|
+
};
|
|
234
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one.
|
|
238
|
+
*
|
|
239
|
+
* @param {Command} cmd
|
|
240
|
+
* @returns {Option[]}
|
|
241
|
+
*/
|
|
242
|
+
visibleOptions(cmd) {
|
|
243
|
+
const visibleOptions = cmd.options.filter((option2) => !option2.hidden);
|
|
244
|
+
const helpOption = cmd._getHelpOption();
|
|
245
|
+
if (helpOption && !helpOption.hidden) {
|
|
246
|
+
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
247
|
+
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
248
|
+
if (!removeShort && !removeLong) {
|
|
249
|
+
visibleOptions.push(helpOption);
|
|
250
|
+
} else if (helpOption.long && !removeLong) {
|
|
251
|
+
visibleOptions.push(
|
|
252
|
+
cmd.createOption(helpOption.long, helpOption.description)
|
|
253
|
+
);
|
|
254
|
+
} else if (helpOption.short && !removeShort) {
|
|
255
|
+
visibleOptions.push(
|
|
256
|
+
cmd.createOption(helpOption.short, helpOption.description)
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (this.sortOptions) {
|
|
261
|
+
visibleOptions.sort(this.compareOptions);
|
|
262
|
+
}
|
|
263
|
+
return visibleOptions;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Get an array of the visible global options. (Not including help.)
|
|
267
|
+
*
|
|
268
|
+
* @param {Command} cmd
|
|
269
|
+
* @returns {Option[]}
|
|
270
|
+
*/
|
|
271
|
+
visibleGlobalOptions(cmd) {
|
|
272
|
+
if (!this.showGlobalOptions) return [];
|
|
273
|
+
const globalOptions = [];
|
|
274
|
+
for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
275
|
+
const visibleOptions = ancestorCmd.options.filter(
|
|
276
|
+
(option2) => !option2.hidden
|
|
277
|
+
);
|
|
278
|
+
globalOptions.push(...visibleOptions);
|
|
279
|
+
}
|
|
280
|
+
if (this.sortOptions) {
|
|
281
|
+
globalOptions.sort(this.compareOptions);
|
|
282
|
+
}
|
|
283
|
+
return globalOptions;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Get an array of the arguments if any have a description.
|
|
287
|
+
*
|
|
288
|
+
* @param {Command} cmd
|
|
289
|
+
* @returns {Argument[]}
|
|
290
|
+
*/
|
|
291
|
+
visibleArguments(cmd) {
|
|
292
|
+
if (cmd._argsDescription) {
|
|
293
|
+
cmd.registeredArguments.forEach((argument2) => {
|
|
294
|
+
argument2.description = argument2.description || cmd._argsDescription[argument2.name()] || "";
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
if (cmd.registeredArguments.find((argument2) => argument2.description)) {
|
|
298
|
+
return cmd.registeredArguments;
|
|
299
|
+
}
|
|
300
|
+
return [];
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Get the command term to show in the list of subcommands.
|
|
304
|
+
*
|
|
305
|
+
* @param {Command} cmd
|
|
306
|
+
* @returns {string}
|
|
307
|
+
*/
|
|
308
|
+
subcommandTerm(cmd) {
|
|
309
|
+
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
310
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + // simplistic check for non-help option
|
|
311
|
+
(args ? " " + args : "");
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Get the option term to show in the list of options.
|
|
315
|
+
*
|
|
316
|
+
* @param {Option} option
|
|
317
|
+
* @returns {string}
|
|
318
|
+
*/
|
|
319
|
+
optionTerm(option2) {
|
|
320
|
+
return option2.flags;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Get the argument term to show in the list of arguments.
|
|
324
|
+
*
|
|
325
|
+
* @param {Argument} argument
|
|
326
|
+
* @returns {string}
|
|
327
|
+
*/
|
|
328
|
+
argumentTerm(argument2) {
|
|
329
|
+
return argument2.name();
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Get the longest command term length.
|
|
333
|
+
*
|
|
334
|
+
* @param {Command} cmd
|
|
335
|
+
* @param {Help} helper
|
|
336
|
+
* @returns {number}
|
|
337
|
+
*/
|
|
338
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
339
|
+
return helper.visibleCommands(cmd).reduce((max, command2) => {
|
|
340
|
+
return Math.max(
|
|
341
|
+
max,
|
|
342
|
+
this.displayWidth(
|
|
343
|
+
helper.styleSubcommandTerm(helper.subcommandTerm(command2))
|
|
344
|
+
)
|
|
345
|
+
);
|
|
346
|
+
}, 0);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Get the longest option term length.
|
|
350
|
+
*
|
|
351
|
+
* @param {Command} cmd
|
|
352
|
+
* @param {Help} helper
|
|
353
|
+
* @returns {number}
|
|
354
|
+
*/
|
|
355
|
+
longestOptionTermLength(cmd, helper) {
|
|
356
|
+
return helper.visibleOptions(cmd).reduce((max, option2) => {
|
|
357
|
+
return Math.max(
|
|
358
|
+
max,
|
|
359
|
+
this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option2)))
|
|
360
|
+
);
|
|
361
|
+
}, 0);
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Get the longest global option term length.
|
|
365
|
+
*
|
|
366
|
+
* @param {Command} cmd
|
|
367
|
+
* @param {Help} helper
|
|
368
|
+
* @returns {number}
|
|
369
|
+
*/
|
|
370
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
371
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option2) => {
|
|
372
|
+
return Math.max(
|
|
373
|
+
max,
|
|
374
|
+
this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option2)))
|
|
375
|
+
);
|
|
376
|
+
}, 0);
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Get the longest argument term length.
|
|
380
|
+
*
|
|
381
|
+
* @param {Command} cmd
|
|
382
|
+
* @param {Help} helper
|
|
383
|
+
* @returns {number}
|
|
384
|
+
*/
|
|
385
|
+
longestArgumentTermLength(cmd, helper) {
|
|
386
|
+
return helper.visibleArguments(cmd).reduce((max, argument2) => {
|
|
387
|
+
return Math.max(
|
|
388
|
+
max,
|
|
389
|
+
this.displayWidth(
|
|
390
|
+
helper.styleArgumentTerm(helper.argumentTerm(argument2))
|
|
391
|
+
)
|
|
392
|
+
);
|
|
393
|
+
}, 0);
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Get the command usage to be displayed at the top of the built-in help.
|
|
397
|
+
*
|
|
398
|
+
* @param {Command} cmd
|
|
399
|
+
* @returns {string}
|
|
400
|
+
*/
|
|
401
|
+
commandUsage(cmd) {
|
|
402
|
+
let cmdName = cmd._name;
|
|
403
|
+
if (cmd._aliases[0]) {
|
|
404
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
405
|
+
}
|
|
406
|
+
let ancestorCmdNames = "";
|
|
407
|
+
for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
408
|
+
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
409
|
+
}
|
|
410
|
+
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Get the description for the command.
|
|
414
|
+
*
|
|
415
|
+
* @param {Command} cmd
|
|
416
|
+
* @returns {string}
|
|
417
|
+
*/
|
|
418
|
+
commandDescription(cmd) {
|
|
419
|
+
return cmd.description();
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Get the subcommand summary to show in the list of subcommands.
|
|
423
|
+
* (Fallback to description for backwards compatibility.)
|
|
424
|
+
*
|
|
425
|
+
* @param {Command} cmd
|
|
426
|
+
* @returns {string}
|
|
427
|
+
*/
|
|
428
|
+
subcommandDescription(cmd) {
|
|
429
|
+
return cmd.summary() || cmd.description();
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Get the option description to show in the list of options.
|
|
433
|
+
*
|
|
434
|
+
* @param {Option} option
|
|
435
|
+
* @return {string}
|
|
436
|
+
*/
|
|
437
|
+
optionDescription(option2) {
|
|
438
|
+
const extraInfo = [];
|
|
439
|
+
if (option2.argChoices) {
|
|
440
|
+
extraInfo.push(
|
|
441
|
+
// use stringify to match the display of the default value
|
|
442
|
+
`choices: ${option2.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
if (option2.defaultValue !== void 0) {
|
|
446
|
+
const showDefault = option2.required || option2.optional || option2.isBoolean() && typeof option2.defaultValue === "boolean";
|
|
447
|
+
if (showDefault) {
|
|
448
|
+
extraInfo.push(
|
|
449
|
+
`default: ${option2.defaultValueDescription || JSON.stringify(option2.defaultValue)}`
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (option2.presetArg !== void 0 && option2.optional) {
|
|
454
|
+
extraInfo.push(`preset: ${JSON.stringify(option2.presetArg)}`);
|
|
455
|
+
}
|
|
456
|
+
if (option2.envVar !== void 0) {
|
|
457
|
+
extraInfo.push(`env: ${option2.envVar}`);
|
|
458
|
+
}
|
|
459
|
+
if (extraInfo.length > 0) {
|
|
460
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
461
|
+
if (option2.description) {
|
|
462
|
+
return `${option2.description} ${extraDescription}`;
|
|
463
|
+
}
|
|
464
|
+
return extraDescription;
|
|
465
|
+
}
|
|
466
|
+
return option2.description;
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Get the argument description to show in the list of arguments.
|
|
470
|
+
*
|
|
471
|
+
* @param {Argument} argument
|
|
472
|
+
* @return {string}
|
|
473
|
+
*/
|
|
474
|
+
argumentDescription(argument2) {
|
|
475
|
+
const extraInfo = [];
|
|
476
|
+
if (argument2.argChoices) {
|
|
477
|
+
extraInfo.push(
|
|
478
|
+
// use stringify to match the display of the default value
|
|
479
|
+
`choices: ${argument2.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
if (argument2.defaultValue !== void 0) {
|
|
483
|
+
extraInfo.push(
|
|
484
|
+
`default: ${argument2.defaultValueDescription || JSON.stringify(argument2.defaultValue)}`
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
if (extraInfo.length > 0) {
|
|
488
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
489
|
+
if (argument2.description) {
|
|
490
|
+
return `${argument2.description} ${extraDescription}`;
|
|
491
|
+
}
|
|
492
|
+
return extraDescription;
|
|
493
|
+
}
|
|
494
|
+
return argument2.description;
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Format a list of items, given a heading and an array of formatted items.
|
|
498
|
+
*
|
|
499
|
+
* @param {string} heading
|
|
500
|
+
* @param {string[]} items
|
|
501
|
+
* @param {Help} helper
|
|
502
|
+
* @returns string[]
|
|
503
|
+
*/
|
|
504
|
+
formatItemList(heading, items, helper) {
|
|
505
|
+
if (items.length === 0) return [];
|
|
506
|
+
return [helper.styleTitle(heading), ...items, ""];
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Group items by their help group heading.
|
|
510
|
+
*
|
|
511
|
+
* @param {Command[] | Option[]} unsortedItems
|
|
512
|
+
* @param {Command[] | Option[]} visibleItems
|
|
513
|
+
* @param {Function} getGroup
|
|
514
|
+
* @returns {Map<string, Command[] | Option[]>}
|
|
515
|
+
*/
|
|
516
|
+
groupItems(unsortedItems, visibleItems, getGroup) {
|
|
517
|
+
const result = /* @__PURE__ */ new Map();
|
|
518
|
+
unsortedItems.forEach((item) => {
|
|
519
|
+
const group = getGroup(item);
|
|
520
|
+
if (!result.has(group)) result.set(group, []);
|
|
521
|
+
});
|
|
522
|
+
visibleItems.forEach((item) => {
|
|
523
|
+
const group = getGroup(item);
|
|
524
|
+
if (!result.has(group)) {
|
|
525
|
+
result.set(group, []);
|
|
526
|
+
}
|
|
527
|
+
result.get(group).push(item);
|
|
528
|
+
});
|
|
529
|
+
return result;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Generate the built-in help text.
|
|
533
|
+
*
|
|
534
|
+
* @param {Command} cmd
|
|
535
|
+
* @param {Help} helper
|
|
536
|
+
* @returns {string}
|
|
537
|
+
*/
|
|
538
|
+
formatHelp(cmd, helper) {
|
|
539
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
540
|
+
const helpWidth = helper.helpWidth ?? 80;
|
|
541
|
+
function callFormatItem(term, description) {
|
|
542
|
+
return helper.formatItem(term, termWidth, description, helper);
|
|
543
|
+
}
|
|
544
|
+
let output = [
|
|
545
|
+
`${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`,
|
|
546
|
+
""
|
|
547
|
+
];
|
|
548
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
549
|
+
if (commandDescription.length > 0) {
|
|
550
|
+
output = output.concat([
|
|
551
|
+
helper.boxWrap(
|
|
552
|
+
helper.styleCommandDescription(commandDescription),
|
|
553
|
+
helpWidth
|
|
554
|
+
),
|
|
555
|
+
""
|
|
556
|
+
]);
|
|
557
|
+
}
|
|
558
|
+
const argumentList = helper.visibleArguments(cmd).map((argument2) => {
|
|
559
|
+
return callFormatItem(
|
|
560
|
+
helper.styleArgumentTerm(helper.argumentTerm(argument2)),
|
|
561
|
+
helper.styleArgumentDescription(helper.argumentDescription(argument2))
|
|
562
|
+
);
|
|
563
|
+
});
|
|
564
|
+
output = output.concat(
|
|
565
|
+
this.formatItemList("Arguments:", argumentList, helper)
|
|
566
|
+
);
|
|
567
|
+
const optionGroups = this.groupItems(
|
|
568
|
+
cmd.options,
|
|
569
|
+
helper.visibleOptions(cmd),
|
|
570
|
+
(option2) => option2.helpGroupHeading ?? "Options:"
|
|
571
|
+
);
|
|
572
|
+
optionGroups.forEach((options, group) => {
|
|
573
|
+
const optionList = options.map((option2) => {
|
|
574
|
+
return callFormatItem(
|
|
575
|
+
helper.styleOptionTerm(helper.optionTerm(option2)),
|
|
576
|
+
helper.styleOptionDescription(helper.optionDescription(option2))
|
|
577
|
+
);
|
|
578
|
+
});
|
|
579
|
+
output = output.concat(this.formatItemList(group, optionList, helper));
|
|
580
|
+
});
|
|
581
|
+
if (helper.showGlobalOptions) {
|
|
582
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option2) => {
|
|
583
|
+
return callFormatItem(
|
|
584
|
+
helper.styleOptionTerm(helper.optionTerm(option2)),
|
|
585
|
+
helper.styleOptionDescription(helper.optionDescription(option2))
|
|
586
|
+
);
|
|
587
|
+
});
|
|
588
|
+
output = output.concat(
|
|
589
|
+
this.formatItemList("Global Options:", globalOptionList, helper)
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
const commandGroups = this.groupItems(
|
|
593
|
+
cmd.commands,
|
|
594
|
+
helper.visibleCommands(cmd),
|
|
595
|
+
(sub) => sub.helpGroup() || "Commands:"
|
|
596
|
+
);
|
|
597
|
+
commandGroups.forEach((commands, group) => {
|
|
598
|
+
const commandList = commands.map((sub) => {
|
|
599
|
+
return callFormatItem(
|
|
600
|
+
helper.styleSubcommandTerm(helper.subcommandTerm(sub)),
|
|
601
|
+
helper.styleSubcommandDescription(helper.subcommandDescription(sub))
|
|
602
|
+
);
|
|
603
|
+
});
|
|
604
|
+
output = output.concat(this.formatItemList(group, commandList, helper));
|
|
605
|
+
});
|
|
606
|
+
return output.join("\n");
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Return display width of string, ignoring ANSI escape sequences. Used in padding and wrapping calculations.
|
|
610
|
+
*
|
|
611
|
+
* @param {string} str
|
|
612
|
+
* @returns {number}
|
|
613
|
+
*/
|
|
614
|
+
displayWidth(str) {
|
|
615
|
+
return stripColor(str).length;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Style the title for displaying in the help. Called with 'Usage:', 'Options:', etc.
|
|
619
|
+
*
|
|
620
|
+
* @param {string} str
|
|
621
|
+
* @returns {string}
|
|
622
|
+
*/
|
|
623
|
+
styleTitle(str) {
|
|
624
|
+
return str;
|
|
625
|
+
}
|
|
626
|
+
styleUsage(str) {
|
|
627
|
+
return str.split(" ").map((word) => {
|
|
628
|
+
if (word === "[options]") return this.styleOptionText(word);
|
|
629
|
+
if (word === "[command]") return this.styleSubcommandText(word);
|
|
630
|
+
if (word[0] === "[" || word[0] === "<")
|
|
631
|
+
return this.styleArgumentText(word);
|
|
632
|
+
return this.styleCommandText(word);
|
|
633
|
+
}).join(" ");
|
|
634
|
+
}
|
|
635
|
+
styleCommandDescription(str) {
|
|
636
|
+
return this.styleDescriptionText(str);
|
|
637
|
+
}
|
|
638
|
+
styleOptionDescription(str) {
|
|
639
|
+
return this.styleDescriptionText(str);
|
|
640
|
+
}
|
|
641
|
+
styleSubcommandDescription(str) {
|
|
642
|
+
return this.styleDescriptionText(str);
|
|
643
|
+
}
|
|
644
|
+
styleArgumentDescription(str) {
|
|
645
|
+
return this.styleDescriptionText(str);
|
|
646
|
+
}
|
|
647
|
+
styleDescriptionText(str) {
|
|
648
|
+
return str;
|
|
649
|
+
}
|
|
650
|
+
styleOptionTerm(str) {
|
|
651
|
+
return this.styleOptionText(str);
|
|
652
|
+
}
|
|
653
|
+
styleSubcommandTerm(str) {
|
|
654
|
+
return str.split(" ").map((word) => {
|
|
655
|
+
if (word === "[options]") return this.styleOptionText(word);
|
|
656
|
+
if (word[0] === "[" || word[0] === "<")
|
|
657
|
+
return this.styleArgumentText(word);
|
|
658
|
+
return this.styleSubcommandText(word);
|
|
659
|
+
}).join(" ");
|
|
660
|
+
}
|
|
661
|
+
styleArgumentTerm(str) {
|
|
662
|
+
return this.styleArgumentText(str);
|
|
663
|
+
}
|
|
664
|
+
styleOptionText(str) {
|
|
665
|
+
return str;
|
|
666
|
+
}
|
|
667
|
+
styleArgumentText(str) {
|
|
668
|
+
return str;
|
|
669
|
+
}
|
|
670
|
+
styleSubcommandText(str) {
|
|
671
|
+
return str;
|
|
672
|
+
}
|
|
673
|
+
styleCommandText(str) {
|
|
674
|
+
return str;
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* Calculate the pad width from the maximum term length.
|
|
678
|
+
*
|
|
679
|
+
* @param {Command} cmd
|
|
680
|
+
* @param {Help} helper
|
|
681
|
+
* @returns {number}
|
|
682
|
+
*/
|
|
683
|
+
padWidth(cmd, helper) {
|
|
684
|
+
return Math.max(
|
|
685
|
+
helper.longestOptionTermLength(cmd, helper),
|
|
686
|
+
helper.longestGlobalOptionTermLength(cmd, helper),
|
|
687
|
+
helper.longestSubcommandTermLength(cmd, helper),
|
|
688
|
+
helper.longestArgumentTermLength(cmd, helper)
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Detect manually wrapped and indented strings by checking for line break followed by whitespace.
|
|
693
|
+
*
|
|
694
|
+
* @param {string} str
|
|
695
|
+
* @returns {boolean}
|
|
696
|
+
*/
|
|
697
|
+
preformatted(str) {
|
|
698
|
+
return /\n[^\S\r\n]/.test(str);
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Format the "item", which consists of a term and description. Pad the term and wrap the description, indenting the following lines.
|
|
702
|
+
*
|
|
703
|
+
* So "TTT", 5, "DDD DDDD DD DDD" might be formatted for this.helpWidth=17 like so:
|
|
704
|
+
* TTT DDD DDDD
|
|
705
|
+
* DD DDD
|
|
706
|
+
*
|
|
707
|
+
* @param {string} term
|
|
708
|
+
* @param {number} termWidth
|
|
709
|
+
* @param {string} description
|
|
710
|
+
* @param {Help} helper
|
|
711
|
+
* @returns {string}
|
|
712
|
+
*/
|
|
713
|
+
formatItem(term, termWidth, description, helper) {
|
|
714
|
+
const itemIndent = 2;
|
|
715
|
+
const itemIndentStr = " ".repeat(itemIndent);
|
|
716
|
+
if (!description) return itemIndentStr + term;
|
|
717
|
+
const paddedTerm = term.padEnd(
|
|
718
|
+
termWidth + term.length - helper.displayWidth(term)
|
|
719
|
+
);
|
|
720
|
+
const spacerWidth = 2;
|
|
721
|
+
const helpWidth = this.helpWidth ?? 80;
|
|
722
|
+
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
|
|
723
|
+
let formattedDescription;
|
|
724
|
+
if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
|
|
725
|
+
formattedDescription = description;
|
|
726
|
+
} else {
|
|
727
|
+
const wrappedDescription = helper.boxWrap(description, remainingWidth);
|
|
728
|
+
formattedDescription = wrappedDescription.replace(
|
|
729
|
+
/\n/g,
|
|
730
|
+
"\n" + " ".repeat(termWidth + spacerWidth)
|
|
731
|
+
);
|
|
732
|
+
}
|
|
733
|
+
return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
|
|
734
|
+
${itemIndentStr}`);
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* Wrap a string at whitespace, preserving existing line breaks.
|
|
738
|
+
* Wrapping is skipped if the width is less than `minWidthToWrap`.
|
|
739
|
+
*
|
|
740
|
+
* @param {string} str
|
|
741
|
+
* @param {number} width
|
|
742
|
+
* @returns {string}
|
|
743
|
+
*/
|
|
744
|
+
boxWrap(str, width) {
|
|
745
|
+
if (width < this.minWidthToWrap) return str;
|
|
746
|
+
const rawLines = str.split(/\r\n|\n/);
|
|
747
|
+
const chunkPattern = /[\s]*[^\s]+/g;
|
|
748
|
+
const wrappedLines = [];
|
|
749
|
+
rawLines.forEach((line) => {
|
|
750
|
+
const chunks = line.match(chunkPattern);
|
|
751
|
+
if (chunks === null) {
|
|
752
|
+
wrappedLines.push("");
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
let sumChunks = [chunks.shift()];
|
|
756
|
+
let sumWidth = this.displayWidth(sumChunks[0]);
|
|
757
|
+
chunks.forEach((chunk) => {
|
|
758
|
+
const visibleWidth = this.displayWidth(chunk);
|
|
759
|
+
if (sumWidth + visibleWidth <= width) {
|
|
760
|
+
sumChunks.push(chunk);
|
|
761
|
+
sumWidth += visibleWidth;
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
wrappedLines.push(sumChunks.join(""));
|
|
765
|
+
const nextChunk = chunk.trimStart();
|
|
766
|
+
sumChunks = [nextChunk];
|
|
767
|
+
sumWidth = this.displayWidth(nextChunk);
|
|
768
|
+
});
|
|
769
|
+
wrappedLines.push(sumChunks.join(""));
|
|
770
|
+
});
|
|
771
|
+
return wrappedLines.join("\n");
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
function stripColor(str) {
|
|
775
|
+
const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
|
|
776
|
+
return str.replace(sgrPattern, "");
|
|
777
|
+
}
|
|
778
|
+
help.Help = Help2;
|
|
779
|
+
help.stripColor = stripColor;
|
|
780
|
+
return help;
|
|
781
|
+
}
|
|
782
|
+
var option = {};
|
|
783
|
+
var hasRequiredOption;
|
|
784
|
+
function requireOption() {
|
|
785
|
+
if (hasRequiredOption) return option;
|
|
786
|
+
hasRequiredOption = 1;
|
|
787
|
+
const { InvalidArgumentError: InvalidArgumentError2 } = requireError();
|
|
788
|
+
class Option2 {
|
|
789
|
+
/**
|
|
790
|
+
* Initialize a new `Option` with the given `flags` and `description`.
|
|
791
|
+
*
|
|
792
|
+
* @param {string} flags
|
|
793
|
+
* @param {string} [description]
|
|
794
|
+
*/
|
|
795
|
+
constructor(flags, description) {
|
|
796
|
+
this.flags = flags;
|
|
797
|
+
this.description = description || "";
|
|
798
|
+
this.required = flags.includes("<");
|
|
799
|
+
this.optional = flags.includes("[");
|
|
800
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
801
|
+
this.mandatory = false;
|
|
802
|
+
const optionFlags = splitOptionFlags(flags);
|
|
803
|
+
this.short = optionFlags.shortFlag;
|
|
804
|
+
this.long = optionFlags.longFlag;
|
|
805
|
+
this.negate = false;
|
|
806
|
+
if (this.long) {
|
|
807
|
+
this.negate = this.long.startsWith("--no-");
|
|
808
|
+
}
|
|
809
|
+
this.defaultValue = void 0;
|
|
810
|
+
this.defaultValueDescription = void 0;
|
|
811
|
+
this.presetArg = void 0;
|
|
812
|
+
this.envVar = void 0;
|
|
813
|
+
this.parseArg = void 0;
|
|
814
|
+
this.hidden = false;
|
|
815
|
+
this.argChoices = void 0;
|
|
816
|
+
this.conflictsWith = [];
|
|
817
|
+
this.implied = void 0;
|
|
818
|
+
this.helpGroupHeading = void 0;
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* Set the default value, and optionally supply the description to be displayed in the help.
|
|
822
|
+
*
|
|
823
|
+
* @param {*} value
|
|
824
|
+
* @param {string} [description]
|
|
825
|
+
* @return {Option}
|
|
826
|
+
*/
|
|
827
|
+
default(value, description) {
|
|
828
|
+
this.defaultValue = value;
|
|
829
|
+
this.defaultValueDescription = description;
|
|
830
|
+
return this;
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Preset to use when option used without option-argument, especially optional but also boolean and negated.
|
|
834
|
+
* The custom processing (parseArg) is called.
|
|
835
|
+
*
|
|
836
|
+
* @example
|
|
837
|
+
* new Option('--color').default('GREYSCALE').preset('RGB');
|
|
838
|
+
* new Option('--donate [amount]').preset('20').argParser(parseFloat);
|
|
839
|
+
*
|
|
840
|
+
* @param {*} arg
|
|
841
|
+
* @return {Option}
|
|
842
|
+
*/
|
|
843
|
+
preset(arg) {
|
|
844
|
+
this.presetArg = arg;
|
|
845
|
+
return this;
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Add option name(s) that conflict with this option.
|
|
849
|
+
* An error will be displayed if conflicting options are found during parsing.
|
|
850
|
+
*
|
|
851
|
+
* @example
|
|
852
|
+
* new Option('--rgb').conflicts('cmyk');
|
|
853
|
+
* new Option('--js').conflicts(['ts', 'jsx']);
|
|
854
|
+
*
|
|
855
|
+
* @param {(string | string[])} names
|
|
856
|
+
* @return {Option}
|
|
857
|
+
*/
|
|
858
|
+
conflicts(names) {
|
|
859
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
860
|
+
return this;
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* Specify implied option values for when this option is set and the implied options are not.
|
|
864
|
+
*
|
|
865
|
+
* The custom processing (parseArg) is not called on the implied values.
|
|
866
|
+
*
|
|
867
|
+
* @example
|
|
868
|
+
* program
|
|
869
|
+
* .addOption(new Option('--log', 'write logging information to file'))
|
|
870
|
+
* .addOption(new Option('--trace', 'log extra details').implies({ log: 'trace.txt' }));
|
|
871
|
+
*
|
|
872
|
+
* @param {object} impliedOptionValues
|
|
873
|
+
* @return {Option}
|
|
874
|
+
*/
|
|
875
|
+
implies(impliedOptionValues) {
|
|
876
|
+
let newImplied = impliedOptionValues;
|
|
877
|
+
if (typeof impliedOptionValues === "string") {
|
|
878
|
+
newImplied = { [impliedOptionValues]: true };
|
|
879
|
+
}
|
|
880
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
881
|
+
return this;
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Set environment variable to check for option value.
|
|
885
|
+
*
|
|
886
|
+
* An environment variable is only used if when processed the current option value is
|
|
887
|
+
* undefined, or the source of the current value is 'default' or 'config' or 'env'.
|
|
888
|
+
*
|
|
889
|
+
* @param {string} name
|
|
890
|
+
* @return {Option}
|
|
891
|
+
*/
|
|
892
|
+
env(name) {
|
|
893
|
+
this.envVar = name;
|
|
894
|
+
return this;
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Set the custom handler for processing CLI option arguments into option values.
|
|
898
|
+
*
|
|
899
|
+
* @param {Function} [fn]
|
|
900
|
+
* @return {Option}
|
|
901
|
+
*/
|
|
902
|
+
argParser(fn) {
|
|
903
|
+
this.parseArg = fn;
|
|
904
|
+
return this;
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* Whether the option is mandatory and must have a value after parsing.
|
|
908
|
+
*
|
|
909
|
+
* @param {boolean} [mandatory=true]
|
|
910
|
+
* @return {Option}
|
|
911
|
+
*/
|
|
912
|
+
makeOptionMandatory(mandatory = true) {
|
|
913
|
+
this.mandatory = !!mandatory;
|
|
914
|
+
return this;
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Hide option in help.
|
|
918
|
+
*
|
|
919
|
+
* @param {boolean} [hide=true]
|
|
920
|
+
* @return {Option}
|
|
921
|
+
*/
|
|
922
|
+
hideHelp(hide = true) {
|
|
923
|
+
this.hidden = !!hide;
|
|
924
|
+
return this;
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* @package
|
|
928
|
+
*/
|
|
929
|
+
_collectValue(value, previous) {
|
|
930
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
931
|
+
return [value];
|
|
932
|
+
}
|
|
933
|
+
previous.push(value);
|
|
934
|
+
return previous;
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* Only allow option value to be one of choices.
|
|
938
|
+
*
|
|
939
|
+
* @param {string[]} values
|
|
940
|
+
* @return {Option}
|
|
941
|
+
*/
|
|
942
|
+
choices(values) {
|
|
943
|
+
this.argChoices = values.slice();
|
|
944
|
+
this.parseArg = (arg, previous) => {
|
|
945
|
+
if (!this.argChoices.includes(arg)) {
|
|
946
|
+
throw new InvalidArgumentError2(
|
|
947
|
+
`Allowed choices are ${this.argChoices.join(", ")}.`
|
|
948
|
+
);
|
|
949
|
+
}
|
|
950
|
+
if (this.variadic) {
|
|
951
|
+
return this._collectValue(arg, previous);
|
|
952
|
+
}
|
|
953
|
+
return arg;
|
|
954
|
+
};
|
|
955
|
+
return this;
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Return option name.
|
|
959
|
+
*
|
|
960
|
+
* @return {string}
|
|
961
|
+
*/
|
|
962
|
+
name() {
|
|
963
|
+
if (this.long) {
|
|
964
|
+
return this.long.replace(/^--/, "");
|
|
965
|
+
}
|
|
966
|
+
return this.short.replace(/^-/, "");
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Return option name, in a camelcase format that can be used
|
|
970
|
+
* as an object attribute key.
|
|
971
|
+
*
|
|
972
|
+
* @return {string}
|
|
973
|
+
*/
|
|
974
|
+
attributeName() {
|
|
975
|
+
if (this.negate) {
|
|
976
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
977
|
+
}
|
|
978
|
+
return camelcase(this.name());
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
* Set the help group heading.
|
|
982
|
+
*
|
|
983
|
+
* @param {string} heading
|
|
984
|
+
* @return {Option}
|
|
985
|
+
*/
|
|
986
|
+
helpGroup(heading) {
|
|
987
|
+
this.helpGroupHeading = heading;
|
|
988
|
+
return this;
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* Check if `arg` matches the short or long flag.
|
|
992
|
+
*
|
|
993
|
+
* @param {string} arg
|
|
994
|
+
* @return {boolean}
|
|
995
|
+
* @package
|
|
996
|
+
*/
|
|
997
|
+
is(arg) {
|
|
998
|
+
return this.short === arg || this.long === arg;
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* Return whether a boolean option.
|
|
1002
|
+
*
|
|
1003
|
+
* Options are one of boolean, negated, required argument, or optional argument.
|
|
1004
|
+
*
|
|
1005
|
+
* @return {boolean}
|
|
1006
|
+
* @package
|
|
1007
|
+
*/
|
|
1008
|
+
isBoolean() {
|
|
1009
|
+
return !this.required && !this.optional && !this.negate;
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
class DualOptions {
|
|
1013
|
+
/**
|
|
1014
|
+
* @param {Option[]} options
|
|
1015
|
+
*/
|
|
1016
|
+
constructor(options) {
|
|
1017
|
+
this.positiveOptions = /* @__PURE__ */ new Map();
|
|
1018
|
+
this.negativeOptions = /* @__PURE__ */ new Map();
|
|
1019
|
+
this.dualOptions = /* @__PURE__ */ new Set();
|
|
1020
|
+
options.forEach((option2) => {
|
|
1021
|
+
if (option2.negate) {
|
|
1022
|
+
this.negativeOptions.set(option2.attributeName(), option2);
|
|
1023
|
+
} else {
|
|
1024
|
+
this.positiveOptions.set(option2.attributeName(), option2);
|
|
1025
|
+
}
|
|
1026
|
+
});
|
|
1027
|
+
this.negativeOptions.forEach((value, key) => {
|
|
1028
|
+
if (this.positiveOptions.has(key)) {
|
|
1029
|
+
this.dualOptions.add(key);
|
|
1030
|
+
}
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* Did the value come from the option, and not from possible matching dual option?
|
|
1035
|
+
*
|
|
1036
|
+
* @param {*} value
|
|
1037
|
+
* @param {Option} option
|
|
1038
|
+
* @returns {boolean}
|
|
1039
|
+
*/
|
|
1040
|
+
valueFromOption(value, option2) {
|
|
1041
|
+
const optionKey = option2.attributeName();
|
|
1042
|
+
if (!this.dualOptions.has(optionKey)) return true;
|
|
1043
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
1044
|
+
const negativeValue = preset !== void 0 ? preset : false;
|
|
1045
|
+
return option2.negate === (negativeValue === value);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
function camelcase(str) {
|
|
1049
|
+
return str.split("-").reduce((str2, word) => {
|
|
1050
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
function splitOptionFlags(flags) {
|
|
1054
|
+
let shortFlag;
|
|
1055
|
+
let longFlag;
|
|
1056
|
+
const shortFlagExp = /^-[^-]$/;
|
|
1057
|
+
const longFlagExp = /^--[^-]/;
|
|
1058
|
+
const flagParts = flags.split(/[ |,]+/).concat("guard");
|
|
1059
|
+
if (shortFlagExp.test(flagParts[0])) shortFlag = flagParts.shift();
|
|
1060
|
+
if (longFlagExp.test(flagParts[0])) longFlag = flagParts.shift();
|
|
1061
|
+
if (!shortFlag && shortFlagExp.test(flagParts[0]))
|
|
1062
|
+
shortFlag = flagParts.shift();
|
|
1063
|
+
if (!shortFlag && longFlagExp.test(flagParts[0])) {
|
|
1064
|
+
shortFlag = longFlag;
|
|
1065
|
+
longFlag = flagParts.shift();
|
|
1066
|
+
}
|
|
1067
|
+
if (flagParts[0].startsWith("-")) {
|
|
1068
|
+
const unsupportedFlag = flagParts[0];
|
|
1069
|
+
const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
|
|
1070
|
+
if (/^-[^-][^-]/.test(unsupportedFlag))
|
|
1071
|
+
throw new Error(
|
|
1072
|
+
`${baseError}
|
|
1073
|
+
- a short flag is a single dash and a single character
|
|
1074
|
+
- either use a single dash and a single character (for a short flag)
|
|
1075
|
+
- or use a double dash for a long option (and can have two, like '--ws, --workspace')`
|
|
1076
|
+
);
|
|
1077
|
+
if (shortFlagExp.test(unsupportedFlag))
|
|
1078
|
+
throw new Error(`${baseError}
|
|
1079
|
+
- too many short flags`);
|
|
1080
|
+
if (longFlagExp.test(unsupportedFlag))
|
|
1081
|
+
throw new Error(`${baseError}
|
|
1082
|
+
- too many long flags`);
|
|
1083
|
+
throw new Error(`${baseError}
|
|
1084
|
+
- unrecognised flag format`);
|
|
1085
|
+
}
|
|
1086
|
+
if (shortFlag === void 0 && longFlag === void 0)
|
|
1087
|
+
throw new Error(
|
|
1088
|
+
`option creation failed due to no flags found in '${flags}'.`
|
|
1089
|
+
);
|
|
1090
|
+
return { shortFlag, longFlag };
|
|
1091
|
+
}
|
|
1092
|
+
option.Option = Option2;
|
|
1093
|
+
option.DualOptions = DualOptions;
|
|
1094
|
+
return option;
|
|
1095
|
+
}
|
|
1096
|
+
var suggestSimilar = {};
|
|
1097
|
+
var hasRequiredSuggestSimilar;
|
|
1098
|
+
function requireSuggestSimilar() {
|
|
1099
|
+
if (hasRequiredSuggestSimilar) return suggestSimilar;
|
|
1100
|
+
hasRequiredSuggestSimilar = 1;
|
|
1101
|
+
const maxDistance = 3;
|
|
1102
|
+
function editDistance(a, b) {
|
|
1103
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
1104
|
+
return Math.max(a.length, b.length);
|
|
1105
|
+
const d = [];
|
|
1106
|
+
for (let i = 0; i <= a.length; i++) {
|
|
1107
|
+
d[i] = [i];
|
|
1108
|
+
}
|
|
1109
|
+
for (let j = 0; j <= b.length; j++) {
|
|
1110
|
+
d[0][j] = j;
|
|
1111
|
+
}
|
|
1112
|
+
for (let j = 1; j <= b.length; j++) {
|
|
1113
|
+
for (let i = 1; i <= a.length; i++) {
|
|
1114
|
+
let cost = 1;
|
|
1115
|
+
if (a[i - 1] === b[j - 1]) {
|
|
1116
|
+
cost = 0;
|
|
1117
|
+
} else {
|
|
1118
|
+
cost = 1;
|
|
1119
|
+
}
|
|
1120
|
+
d[i][j] = Math.min(
|
|
1121
|
+
d[i - 1][j] + 1,
|
|
1122
|
+
// deletion
|
|
1123
|
+
d[i][j - 1] + 1,
|
|
1124
|
+
// insertion
|
|
1125
|
+
d[i - 1][j - 1] + cost
|
|
1126
|
+
// substitution
|
|
1127
|
+
);
|
|
1128
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
1129
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
return d[a.length][b.length];
|
|
1134
|
+
}
|
|
1135
|
+
function suggestSimilar$1(word, candidates) {
|
|
1136
|
+
if (!candidates || candidates.length === 0) return "";
|
|
1137
|
+
candidates = Array.from(new Set(candidates));
|
|
1138
|
+
const searchingOptions = word.startsWith("--");
|
|
1139
|
+
if (searchingOptions) {
|
|
1140
|
+
word = word.slice(2);
|
|
1141
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
1142
|
+
}
|
|
1143
|
+
let similar = [];
|
|
1144
|
+
let bestDistance = maxDistance;
|
|
1145
|
+
const minSimilarity = 0.4;
|
|
1146
|
+
candidates.forEach((candidate) => {
|
|
1147
|
+
if (candidate.length <= 1) return;
|
|
1148
|
+
const distance = editDistance(word, candidate);
|
|
1149
|
+
const length = Math.max(word.length, candidate.length);
|
|
1150
|
+
const similarity = (length - distance) / length;
|
|
1151
|
+
if (similarity > minSimilarity) {
|
|
1152
|
+
if (distance < bestDistance) {
|
|
1153
|
+
bestDistance = distance;
|
|
1154
|
+
similar = [candidate];
|
|
1155
|
+
} else if (distance === bestDistance) {
|
|
1156
|
+
similar.push(candidate);
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
});
|
|
1160
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
1161
|
+
if (searchingOptions) {
|
|
1162
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
1163
|
+
}
|
|
1164
|
+
if (similar.length > 1) {
|
|
1165
|
+
return `
|
|
1166
|
+
(Did you mean one of ${similar.join(", ")}?)`;
|
|
1167
|
+
}
|
|
1168
|
+
if (similar.length === 1) {
|
|
1169
|
+
return `
|
|
1170
|
+
(Did you mean ${similar[0]}?)`;
|
|
1171
|
+
}
|
|
1172
|
+
return "";
|
|
1173
|
+
}
|
|
1174
|
+
suggestSimilar.suggestSimilar = suggestSimilar$1;
|
|
1175
|
+
return suggestSimilar;
|
|
1176
|
+
}
|
|
1177
|
+
var hasRequiredCommand;
|
|
1178
|
+
function requireCommand() {
|
|
1179
|
+
if (hasRequiredCommand) return command;
|
|
1180
|
+
hasRequiredCommand = 1;
|
|
1181
|
+
const EventEmitter = require$$0.EventEmitter;
|
|
1182
|
+
const childProcess = require$$1;
|
|
1183
|
+
const path = require$$2;
|
|
1184
|
+
const fs = require$$3;
|
|
1185
|
+
const process2 = require$$4;
|
|
1186
|
+
const { Argument: Argument2, humanReadableArgName } = requireArgument();
|
|
1187
|
+
const { CommanderError: CommanderError2 } = requireError();
|
|
1188
|
+
const { Help: Help2, stripColor } = requireHelp();
|
|
1189
|
+
const { Option: Option2, DualOptions } = requireOption();
|
|
1190
|
+
const { suggestSimilar: suggestSimilar2 } = requireSuggestSimilar();
|
|
1191
|
+
class Command2 extends EventEmitter {
|
|
1192
|
+
/**
|
|
1193
|
+
* Initialize a new `Command`.
|
|
1194
|
+
*
|
|
1195
|
+
* @param {string} [name]
|
|
1196
|
+
*/
|
|
1197
|
+
constructor(name) {
|
|
1198
|
+
super();
|
|
1199
|
+
this.commands = [];
|
|
1200
|
+
this.options = [];
|
|
1201
|
+
this.parent = null;
|
|
1202
|
+
this._allowUnknownOption = false;
|
|
1203
|
+
this._allowExcessArguments = false;
|
|
1204
|
+
this.registeredArguments = [];
|
|
1205
|
+
this._args = this.registeredArguments;
|
|
1206
|
+
this.args = [];
|
|
1207
|
+
this.rawArgs = [];
|
|
1208
|
+
this.processedArgs = [];
|
|
1209
|
+
this._scriptPath = null;
|
|
1210
|
+
this._name = name || "";
|
|
1211
|
+
this._optionValues = {};
|
|
1212
|
+
this._optionValueSources = {};
|
|
1213
|
+
this._storeOptionsAsProperties = false;
|
|
1214
|
+
this._actionHandler = null;
|
|
1215
|
+
this._executableHandler = false;
|
|
1216
|
+
this._executableFile = null;
|
|
1217
|
+
this._executableDir = null;
|
|
1218
|
+
this._defaultCommandName = null;
|
|
1219
|
+
this._exitCallback = null;
|
|
1220
|
+
this._aliases = [];
|
|
1221
|
+
this._combineFlagAndOptionalValue = true;
|
|
1222
|
+
this._description = "";
|
|
1223
|
+
this._summary = "";
|
|
1224
|
+
this._argsDescription = void 0;
|
|
1225
|
+
this._enablePositionalOptions = false;
|
|
1226
|
+
this._passThroughOptions = false;
|
|
1227
|
+
this._lifeCycleHooks = {};
|
|
1228
|
+
this._showHelpAfterError = false;
|
|
1229
|
+
this._showSuggestionAfterError = true;
|
|
1230
|
+
this._savedState = null;
|
|
1231
|
+
this._outputConfiguration = {
|
|
1232
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
1233
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
1234
|
+
outputError: (str, write) => write(str),
|
|
1235
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : void 0,
|
|
1236
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : void 0,
|
|
1237
|
+
getOutHasColors: () => useColor() ?? (process2.stdout.isTTY && process2.stdout.hasColors?.()),
|
|
1238
|
+
getErrHasColors: () => useColor() ?? (process2.stderr.isTTY && process2.stderr.hasColors?.()),
|
|
1239
|
+
stripColor: (str) => stripColor(str)
|
|
1240
|
+
};
|
|
1241
|
+
this._hidden = false;
|
|
1242
|
+
this._helpOption = void 0;
|
|
1243
|
+
this._addImplicitHelpCommand = void 0;
|
|
1244
|
+
this._helpCommand = void 0;
|
|
1245
|
+
this._helpConfiguration = {};
|
|
1246
|
+
this._helpGroupHeading = void 0;
|
|
1247
|
+
this._defaultCommandGroup = void 0;
|
|
1248
|
+
this._defaultOptionGroup = void 0;
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Copy settings that are useful to have in common across root command and subcommands.
|
|
1252
|
+
*
|
|
1253
|
+
* (Used internally when adding a command using `.command()` so subcommands inherit parent settings.)
|
|
1254
|
+
*
|
|
1255
|
+
* @param {Command} sourceCommand
|
|
1256
|
+
* @return {Command} `this` command for chaining
|
|
1257
|
+
*/
|
|
1258
|
+
copyInheritedSettings(sourceCommand) {
|
|
1259
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
1260
|
+
this._helpOption = sourceCommand._helpOption;
|
|
1261
|
+
this._helpCommand = sourceCommand._helpCommand;
|
|
1262
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
1263
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
1264
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
1265
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
1266
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
1267
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
1268
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
1269
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
1270
|
+
return this;
|
|
1271
|
+
}
|
|
1272
|
+
/**
|
|
1273
|
+
* @returns {Command[]}
|
|
1274
|
+
* @private
|
|
1275
|
+
*/
|
|
1276
|
+
_getCommandAndAncestors() {
|
|
1277
|
+
const result = [];
|
|
1278
|
+
for (let command2 = this; command2; command2 = command2.parent) {
|
|
1279
|
+
result.push(command2);
|
|
1280
|
+
}
|
|
1281
|
+
return result;
|
|
1282
|
+
}
|
|
1283
|
+
/**
|
|
1284
|
+
* Define a command.
|
|
1285
|
+
*
|
|
1286
|
+
* There are two styles of command: pay attention to where to put the description.
|
|
1287
|
+
*
|
|
1288
|
+
* @example
|
|
1289
|
+
* // Command implemented using action handler (description is supplied separately to `.command`)
|
|
1290
|
+
* program
|
|
1291
|
+
* .command('clone <source> [destination]')
|
|
1292
|
+
* .description('clone a repository into a newly created directory')
|
|
1293
|
+
* .action((source, destination) => {
|
|
1294
|
+
* console.log('clone command called');
|
|
1295
|
+
* });
|
|
1296
|
+
*
|
|
1297
|
+
* // Command implemented using separate executable file (description is second parameter to `.command`)
|
|
1298
|
+
* program
|
|
1299
|
+
* .command('start <service>', 'start named service')
|
|
1300
|
+
* .command('stop [service]', 'stop named service, or all if no name supplied');
|
|
1301
|
+
*
|
|
1302
|
+
* @param {string} nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
|
|
1303
|
+
* @param {(object | string)} [actionOptsOrExecDesc] - configuration options (for action), or description (for executable)
|
|
1304
|
+
* @param {object} [execOpts] - configuration options (for executable)
|
|
1305
|
+
* @return {Command} returns new command for action handler, or `this` for executable command
|
|
1306
|
+
*/
|
|
1307
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
1308
|
+
let desc = actionOptsOrExecDesc;
|
|
1309
|
+
let opts = execOpts;
|
|
1310
|
+
if (typeof desc === "object" && desc !== null) {
|
|
1311
|
+
opts = desc;
|
|
1312
|
+
desc = null;
|
|
1313
|
+
}
|
|
1314
|
+
opts = opts || {};
|
|
1315
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
1316
|
+
const cmd = this.createCommand(name);
|
|
1317
|
+
if (desc) {
|
|
1318
|
+
cmd.description(desc);
|
|
1319
|
+
cmd._executableHandler = true;
|
|
1320
|
+
}
|
|
1321
|
+
if (opts.isDefault) this._defaultCommandName = cmd._name;
|
|
1322
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
1323
|
+
cmd._executableFile = opts.executableFile || null;
|
|
1324
|
+
if (args) cmd.arguments(args);
|
|
1325
|
+
this._registerCommand(cmd);
|
|
1326
|
+
cmd.parent = this;
|
|
1327
|
+
cmd.copyInheritedSettings(this);
|
|
1328
|
+
if (desc) return this;
|
|
1329
|
+
return cmd;
|
|
1330
|
+
}
|
|
1331
|
+
/**
|
|
1332
|
+
* Factory routine to create a new unattached command.
|
|
1333
|
+
*
|
|
1334
|
+
* See .command() for creating an attached subcommand, which uses this routine to
|
|
1335
|
+
* create the command. You can override createCommand to customise subcommands.
|
|
1336
|
+
*
|
|
1337
|
+
* @param {string} [name]
|
|
1338
|
+
* @return {Command} new command
|
|
1339
|
+
*/
|
|
1340
|
+
createCommand(name) {
|
|
1341
|
+
return new Command2(name);
|
|
1342
|
+
}
|
|
1343
|
+
/**
|
|
1344
|
+
* You can customise the help with a subclass of Help by overriding createHelp,
|
|
1345
|
+
* or by overriding Help properties using configureHelp().
|
|
1346
|
+
*
|
|
1347
|
+
* @return {Help}
|
|
1348
|
+
*/
|
|
1349
|
+
createHelp() {
|
|
1350
|
+
return Object.assign(new Help2(), this.configureHelp());
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* You can customise the help by overriding Help properties using configureHelp(),
|
|
1354
|
+
* or with a subclass of Help by overriding createHelp().
|
|
1355
|
+
*
|
|
1356
|
+
* @param {object} [configuration] - configuration options
|
|
1357
|
+
* @return {(Command | object)} `this` command for chaining, or stored configuration
|
|
1358
|
+
*/
|
|
1359
|
+
configureHelp(configuration) {
|
|
1360
|
+
if (configuration === void 0) return this._helpConfiguration;
|
|
1361
|
+
this._helpConfiguration = configuration;
|
|
1362
|
+
return this;
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* The default output goes to stdout and stderr. You can customise this for special
|
|
1366
|
+
* applications. You can also customise the display of errors by overriding outputError.
|
|
1367
|
+
*
|
|
1368
|
+
* The configuration properties are all functions:
|
|
1369
|
+
*
|
|
1370
|
+
* // change how output being written, defaults to stdout and stderr
|
|
1371
|
+
* writeOut(str)
|
|
1372
|
+
* writeErr(str)
|
|
1373
|
+
* // change how output being written for errors, defaults to writeErr
|
|
1374
|
+
* outputError(str, write) // used for displaying errors and not used for displaying help
|
|
1375
|
+
* // specify width for wrapping help
|
|
1376
|
+
* getOutHelpWidth()
|
|
1377
|
+
* getErrHelpWidth()
|
|
1378
|
+
* // color support, currently only used with Help
|
|
1379
|
+
* getOutHasColors()
|
|
1380
|
+
* getErrHasColors()
|
|
1381
|
+
* stripColor() // used to remove ANSI escape codes if output does not have colors
|
|
1382
|
+
*
|
|
1383
|
+
* @param {object} [configuration] - configuration options
|
|
1384
|
+
* @return {(Command | object)} `this` command for chaining, or stored configuration
|
|
1385
|
+
*/
|
|
1386
|
+
configureOutput(configuration) {
|
|
1387
|
+
if (configuration === void 0) return this._outputConfiguration;
|
|
1388
|
+
this._outputConfiguration = {
|
|
1389
|
+
...this._outputConfiguration,
|
|
1390
|
+
...configuration
|
|
1391
|
+
};
|
|
1392
|
+
return this;
|
|
1393
|
+
}
|
|
1394
|
+
/**
|
|
1395
|
+
* Display the help or a custom message after an error occurs.
|
|
1396
|
+
*
|
|
1397
|
+
* @param {(boolean|string)} [displayHelp]
|
|
1398
|
+
* @return {Command} `this` command for chaining
|
|
1399
|
+
*/
|
|
1400
|
+
showHelpAfterError(displayHelp = true) {
|
|
1401
|
+
if (typeof displayHelp !== "string") displayHelp = !!displayHelp;
|
|
1402
|
+
this._showHelpAfterError = displayHelp;
|
|
1403
|
+
return this;
|
|
1404
|
+
}
|
|
1405
|
+
/**
|
|
1406
|
+
* Display suggestion of similar commands for unknown commands, or options for unknown options.
|
|
1407
|
+
*
|
|
1408
|
+
* @param {boolean} [displaySuggestion]
|
|
1409
|
+
* @return {Command} `this` command for chaining
|
|
1410
|
+
*/
|
|
1411
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
1412
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
1413
|
+
return this;
|
|
1414
|
+
}
|
|
1415
|
+
/**
|
|
1416
|
+
* Add a prepared subcommand.
|
|
1417
|
+
*
|
|
1418
|
+
* See .command() for creating an attached subcommand which inherits settings from its parent.
|
|
1419
|
+
*
|
|
1420
|
+
* @param {Command} cmd - new subcommand
|
|
1421
|
+
* @param {object} [opts] - configuration options
|
|
1422
|
+
* @return {Command} `this` command for chaining
|
|
1423
|
+
*/
|
|
1424
|
+
addCommand(cmd, opts) {
|
|
1425
|
+
if (!cmd._name) {
|
|
1426
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
1427
|
+
- specify the name in Command constructor or using .name()`);
|
|
1428
|
+
}
|
|
1429
|
+
opts = opts || {};
|
|
1430
|
+
if (opts.isDefault) this._defaultCommandName = cmd._name;
|
|
1431
|
+
if (opts.noHelp || opts.hidden) cmd._hidden = true;
|
|
1432
|
+
this._registerCommand(cmd);
|
|
1433
|
+
cmd.parent = this;
|
|
1434
|
+
cmd._checkForBrokenPassThrough();
|
|
1435
|
+
return this;
|
|
1436
|
+
}
|
|
1437
|
+
/**
|
|
1438
|
+
* Factory routine to create a new unattached argument.
|
|
1439
|
+
*
|
|
1440
|
+
* See .argument() for creating an attached argument, which uses this routine to
|
|
1441
|
+
* create the argument. You can override createArgument to return a custom argument.
|
|
1442
|
+
*
|
|
1443
|
+
* @param {string} name
|
|
1444
|
+
* @param {string} [description]
|
|
1445
|
+
* @return {Argument} new argument
|
|
1446
|
+
*/
|
|
1447
|
+
createArgument(name, description) {
|
|
1448
|
+
return new Argument2(name, description);
|
|
1449
|
+
}
|
|
1450
|
+
/**
|
|
1451
|
+
* Define argument syntax for command.
|
|
1452
|
+
*
|
|
1453
|
+
* The default is that the argument is required, and you can explicitly
|
|
1454
|
+
* indicate this with <> around the name. Put [] around the name for an optional argument.
|
|
1455
|
+
*
|
|
1456
|
+
* @example
|
|
1457
|
+
* program.argument('<input-file>');
|
|
1458
|
+
* program.argument('[output-file]');
|
|
1459
|
+
*
|
|
1460
|
+
* @param {string} name
|
|
1461
|
+
* @param {string} [description]
|
|
1462
|
+
* @param {(Function|*)} [parseArg] - custom argument processing function or default value
|
|
1463
|
+
* @param {*} [defaultValue]
|
|
1464
|
+
* @return {Command} `this` command for chaining
|
|
1465
|
+
*/
|
|
1466
|
+
argument(name, description, parseArg, defaultValue) {
|
|
1467
|
+
const argument2 = this.createArgument(name, description);
|
|
1468
|
+
if (typeof parseArg === "function") {
|
|
1469
|
+
argument2.default(defaultValue).argParser(parseArg);
|
|
1470
|
+
} else {
|
|
1471
|
+
argument2.default(parseArg);
|
|
1472
|
+
}
|
|
1473
|
+
this.addArgument(argument2);
|
|
1474
|
+
return this;
|
|
1475
|
+
}
|
|
1476
|
+
/**
|
|
1477
|
+
* Define argument syntax for command, adding multiple at once (without descriptions).
|
|
1478
|
+
*
|
|
1479
|
+
* See also .argument().
|
|
1480
|
+
*
|
|
1481
|
+
* @example
|
|
1482
|
+
* program.arguments('<cmd> [env]');
|
|
1483
|
+
*
|
|
1484
|
+
* @param {string} names
|
|
1485
|
+
* @return {Command} `this` command for chaining
|
|
1486
|
+
*/
|
|
1487
|
+
arguments(names) {
|
|
1488
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
1489
|
+
this.argument(detail);
|
|
1490
|
+
});
|
|
1491
|
+
return this;
|
|
1492
|
+
}
|
|
1493
|
+
/**
|
|
1494
|
+
* Define argument syntax for command, adding a prepared argument.
|
|
1495
|
+
*
|
|
1496
|
+
* @param {Argument} argument
|
|
1497
|
+
* @return {Command} `this` command for chaining
|
|
1498
|
+
*/
|
|
1499
|
+
addArgument(argument2) {
|
|
1500
|
+
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
1501
|
+
if (previousArgument?.variadic) {
|
|
1502
|
+
throw new Error(
|
|
1503
|
+
`only the last argument can be variadic '${previousArgument.name()}'`
|
|
1504
|
+
);
|
|
1505
|
+
}
|
|
1506
|
+
if (argument2.required && argument2.defaultValue !== void 0 && argument2.parseArg === void 0) {
|
|
1507
|
+
throw new Error(
|
|
1508
|
+
`a default value for a required argument is never used: '${argument2.name()}'`
|
|
1509
|
+
);
|
|
1510
|
+
}
|
|
1511
|
+
this.registeredArguments.push(argument2);
|
|
1512
|
+
return this;
|
|
1513
|
+
}
|
|
1514
|
+
/**
|
|
1515
|
+
* Customise or override default help command. By default a help command is automatically added if your command has subcommands.
|
|
1516
|
+
*
|
|
1517
|
+
* @example
|
|
1518
|
+
* program.helpCommand('help [cmd]');
|
|
1519
|
+
* program.helpCommand('help [cmd]', 'show help');
|
|
1520
|
+
* program.helpCommand(false); // suppress default help command
|
|
1521
|
+
* program.helpCommand(true); // add help command even if no subcommands
|
|
1522
|
+
*
|
|
1523
|
+
* @param {string|boolean} enableOrNameAndArgs - enable with custom name and/or arguments, or boolean to override whether added
|
|
1524
|
+
* @param {string} [description] - custom description
|
|
1525
|
+
* @return {Command} `this` command for chaining
|
|
1526
|
+
*/
|
|
1527
|
+
helpCommand(enableOrNameAndArgs, description) {
|
|
1528
|
+
if (typeof enableOrNameAndArgs === "boolean") {
|
|
1529
|
+
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
1530
|
+
if (enableOrNameAndArgs && this._defaultCommandGroup) {
|
|
1531
|
+
this._initCommandGroup(this._getHelpCommand());
|
|
1532
|
+
}
|
|
1533
|
+
return this;
|
|
1534
|
+
}
|
|
1535
|
+
const nameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
1536
|
+
const [, helpName, helpArgs] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
1537
|
+
const helpDescription = description ?? "display help for command";
|
|
1538
|
+
const helpCommand = this.createCommand(helpName);
|
|
1539
|
+
helpCommand.helpOption(false);
|
|
1540
|
+
if (helpArgs) helpCommand.arguments(helpArgs);
|
|
1541
|
+
if (helpDescription) helpCommand.description(helpDescription);
|
|
1542
|
+
this._addImplicitHelpCommand = true;
|
|
1543
|
+
this._helpCommand = helpCommand;
|
|
1544
|
+
if (enableOrNameAndArgs || description) this._initCommandGroup(helpCommand);
|
|
1545
|
+
return this;
|
|
1546
|
+
}
|
|
1547
|
+
/**
|
|
1548
|
+
* Add prepared custom help command.
|
|
1549
|
+
*
|
|
1550
|
+
* @param {(Command|string|boolean)} helpCommand - custom help command, or deprecated enableOrNameAndArgs as for `.helpCommand()`
|
|
1551
|
+
* @param {string} [deprecatedDescription] - deprecated custom description used with custom name only
|
|
1552
|
+
* @return {Command} `this` command for chaining
|
|
1553
|
+
*/
|
|
1554
|
+
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
1555
|
+
if (typeof helpCommand !== "object") {
|
|
1556
|
+
this.helpCommand(helpCommand, deprecatedDescription);
|
|
1557
|
+
return this;
|
|
1558
|
+
}
|
|
1559
|
+
this._addImplicitHelpCommand = true;
|
|
1560
|
+
this._helpCommand = helpCommand;
|
|
1561
|
+
this._initCommandGroup(helpCommand);
|
|
1562
|
+
return this;
|
|
1563
|
+
}
|
|
1564
|
+
/**
|
|
1565
|
+
* Lazy create help command.
|
|
1566
|
+
*
|
|
1567
|
+
* @return {(Command|null)}
|
|
1568
|
+
* @package
|
|
1569
|
+
*/
|
|
1570
|
+
_getHelpCommand() {
|
|
1571
|
+
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
1572
|
+
if (hasImplicitHelpCommand) {
|
|
1573
|
+
if (this._helpCommand === void 0) {
|
|
1574
|
+
this.helpCommand(void 0, void 0);
|
|
1575
|
+
}
|
|
1576
|
+
return this._helpCommand;
|
|
1577
|
+
}
|
|
1578
|
+
return null;
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Add hook for life cycle event.
|
|
1582
|
+
*
|
|
1583
|
+
* @param {string} event
|
|
1584
|
+
* @param {Function} listener
|
|
1585
|
+
* @return {Command} `this` command for chaining
|
|
1586
|
+
*/
|
|
1587
|
+
hook(event, listener) {
|
|
1588
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
1589
|
+
if (!allowedValues.includes(event)) {
|
|
1590
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
1591
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
1592
|
+
}
|
|
1593
|
+
if (this._lifeCycleHooks[event]) {
|
|
1594
|
+
this._lifeCycleHooks[event].push(listener);
|
|
1595
|
+
} else {
|
|
1596
|
+
this._lifeCycleHooks[event] = [listener];
|
|
1597
|
+
}
|
|
1598
|
+
return this;
|
|
1599
|
+
}
|
|
1600
|
+
/**
|
|
1601
|
+
* Register callback to use as replacement for calling process.exit.
|
|
1602
|
+
*
|
|
1603
|
+
* @param {Function} [fn] optional callback which will be passed a CommanderError, defaults to throwing
|
|
1604
|
+
* @return {Command} `this` command for chaining
|
|
1605
|
+
*/
|
|
1606
|
+
exitOverride(fn) {
|
|
1607
|
+
if (fn) {
|
|
1608
|
+
this._exitCallback = fn;
|
|
1609
|
+
} else {
|
|
1610
|
+
this._exitCallback = (err) => {
|
|
1611
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
1612
|
+
throw err;
|
|
1613
|
+
}
|
|
1614
|
+
};
|
|
1615
|
+
}
|
|
1616
|
+
return this;
|
|
1617
|
+
}
|
|
1618
|
+
/**
|
|
1619
|
+
* Call process.exit, and _exitCallback if defined.
|
|
1620
|
+
*
|
|
1621
|
+
* @param {number} exitCode exit code for using with process.exit
|
|
1622
|
+
* @param {string} code an id string representing the error
|
|
1623
|
+
* @param {string} message human-readable description of the error
|
|
1624
|
+
* @return never
|
|
1625
|
+
* @private
|
|
1626
|
+
*/
|
|
1627
|
+
_exit(exitCode, code, message) {
|
|
1628
|
+
if (this._exitCallback) {
|
|
1629
|
+
this._exitCallback(new CommanderError2(exitCode, code, message));
|
|
1630
|
+
}
|
|
1631
|
+
process2.exit(exitCode);
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* Register callback `fn` for the command.
|
|
1635
|
+
*
|
|
1636
|
+
* @example
|
|
1637
|
+
* program
|
|
1638
|
+
* .command('serve')
|
|
1639
|
+
* .description('start service')
|
|
1640
|
+
* .action(function() {
|
|
1641
|
+
* // do work here
|
|
1642
|
+
* });
|
|
1643
|
+
*
|
|
1644
|
+
* @param {Function} fn
|
|
1645
|
+
* @return {Command} `this` command for chaining
|
|
1646
|
+
*/
|
|
1647
|
+
action(fn) {
|
|
1648
|
+
const listener = (args) => {
|
|
1649
|
+
const expectedArgsCount = this.registeredArguments.length;
|
|
1650
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
1651
|
+
if (this._storeOptionsAsProperties) {
|
|
1652
|
+
actionArgs[expectedArgsCount] = this;
|
|
1653
|
+
} else {
|
|
1654
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
1655
|
+
}
|
|
1656
|
+
actionArgs.push(this);
|
|
1657
|
+
return fn.apply(this, actionArgs);
|
|
1658
|
+
};
|
|
1659
|
+
this._actionHandler = listener;
|
|
1660
|
+
return this;
|
|
1661
|
+
}
|
|
1662
|
+
/**
|
|
1663
|
+
* Factory routine to create a new unattached option.
|
|
1664
|
+
*
|
|
1665
|
+
* See .option() for creating an attached option, which uses this routine to
|
|
1666
|
+
* create the option. You can override createOption to return a custom option.
|
|
1667
|
+
*
|
|
1668
|
+
* @param {string} flags
|
|
1669
|
+
* @param {string} [description]
|
|
1670
|
+
* @return {Option} new option
|
|
1671
|
+
*/
|
|
1672
|
+
createOption(flags, description) {
|
|
1673
|
+
return new Option2(flags, description);
|
|
1674
|
+
}
|
|
1675
|
+
/**
|
|
1676
|
+
* Wrap parseArgs to catch 'commander.invalidArgument'.
|
|
1677
|
+
*
|
|
1678
|
+
* @param {(Option | Argument)} target
|
|
1679
|
+
* @param {string} value
|
|
1680
|
+
* @param {*} previous
|
|
1681
|
+
* @param {string} invalidArgumentMessage
|
|
1682
|
+
* @private
|
|
1683
|
+
*/
|
|
1684
|
+
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
1685
|
+
try {
|
|
1686
|
+
return target.parseArg(value, previous);
|
|
1687
|
+
} catch (err) {
|
|
1688
|
+
if (err.code === "commander.invalidArgument") {
|
|
1689
|
+
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
1690
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
1691
|
+
}
|
|
1692
|
+
throw err;
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
/**
|
|
1696
|
+
* Check for option flag conflicts.
|
|
1697
|
+
* Register option if no conflicts found, or throw on conflict.
|
|
1698
|
+
*
|
|
1699
|
+
* @param {Option} option
|
|
1700
|
+
* @private
|
|
1701
|
+
*/
|
|
1702
|
+
_registerOption(option2) {
|
|
1703
|
+
const matchingOption = option2.short && this._findOption(option2.short) || option2.long && this._findOption(option2.long);
|
|
1704
|
+
if (matchingOption) {
|
|
1705
|
+
const matchingFlag = option2.long && this._findOption(option2.long) ? option2.long : option2.short;
|
|
1706
|
+
throw new Error(`Cannot add option '${option2.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
1707
|
+
- already used by option '${matchingOption.flags}'`);
|
|
1708
|
+
}
|
|
1709
|
+
this._initOptionGroup(option2);
|
|
1710
|
+
this.options.push(option2);
|
|
1711
|
+
}
|
|
1712
|
+
/**
|
|
1713
|
+
* Check for command name and alias conflicts with existing commands.
|
|
1714
|
+
* Register command if no conflicts found, or throw on conflict.
|
|
1715
|
+
*
|
|
1716
|
+
* @param {Command} command
|
|
1717
|
+
* @private
|
|
1718
|
+
*/
|
|
1719
|
+
_registerCommand(command2) {
|
|
1720
|
+
const knownBy = (cmd) => {
|
|
1721
|
+
return [cmd.name()].concat(cmd.aliases());
|
|
1722
|
+
};
|
|
1723
|
+
const alreadyUsed = knownBy(command2).find(
|
|
1724
|
+
(name) => this._findCommand(name)
|
|
1725
|
+
);
|
|
1726
|
+
if (alreadyUsed) {
|
|
1727
|
+
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
1728
|
+
const newCmd = knownBy(command2).join("|");
|
|
1729
|
+
throw new Error(
|
|
1730
|
+
`cannot add command '${newCmd}' as already have command '${existingCmd}'`
|
|
1731
|
+
);
|
|
1732
|
+
}
|
|
1733
|
+
this._initCommandGroup(command2);
|
|
1734
|
+
this.commands.push(command2);
|
|
1735
|
+
}
|
|
1736
|
+
/**
|
|
1737
|
+
* Add an option.
|
|
1738
|
+
*
|
|
1739
|
+
* @param {Option} option
|
|
1740
|
+
* @return {Command} `this` command for chaining
|
|
1741
|
+
*/
|
|
1742
|
+
addOption(option2) {
|
|
1743
|
+
this._registerOption(option2);
|
|
1744
|
+
const oname = option2.name();
|
|
1745
|
+
const name = option2.attributeName();
|
|
1746
|
+
if (option2.negate) {
|
|
1747
|
+
const positiveLongFlag = option2.long.replace(/^--no-/, "--");
|
|
1748
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
1749
|
+
this.setOptionValueWithSource(
|
|
1750
|
+
name,
|
|
1751
|
+
option2.defaultValue === void 0 ? true : option2.defaultValue,
|
|
1752
|
+
"default"
|
|
1753
|
+
);
|
|
1754
|
+
}
|
|
1755
|
+
} else if (option2.defaultValue !== void 0) {
|
|
1756
|
+
this.setOptionValueWithSource(name, option2.defaultValue, "default");
|
|
1757
|
+
}
|
|
1758
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
1759
|
+
if (val == null && option2.presetArg !== void 0) {
|
|
1760
|
+
val = option2.presetArg;
|
|
1761
|
+
}
|
|
1762
|
+
const oldValue = this.getOptionValue(name);
|
|
1763
|
+
if (val !== null && option2.parseArg) {
|
|
1764
|
+
val = this._callParseArg(option2, val, oldValue, invalidValueMessage);
|
|
1765
|
+
} else if (val !== null && option2.variadic) {
|
|
1766
|
+
val = option2._collectValue(val, oldValue);
|
|
1767
|
+
}
|
|
1768
|
+
if (val == null) {
|
|
1769
|
+
if (option2.negate) {
|
|
1770
|
+
val = false;
|
|
1771
|
+
} else if (option2.isBoolean() || option2.optional) {
|
|
1772
|
+
val = true;
|
|
1773
|
+
} else {
|
|
1774
|
+
val = "";
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
1778
|
+
};
|
|
1779
|
+
this.on("option:" + oname, (val) => {
|
|
1780
|
+
const invalidValueMessage = `error: option '${option2.flags}' argument '${val}' is invalid.`;
|
|
1781
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
1782
|
+
});
|
|
1783
|
+
if (option2.envVar) {
|
|
1784
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
1785
|
+
const invalidValueMessage = `error: option '${option2.flags}' value '${val}' from env '${option2.envVar}' is invalid.`;
|
|
1786
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
1787
|
+
});
|
|
1788
|
+
}
|
|
1789
|
+
return this;
|
|
1790
|
+
}
|
|
1791
|
+
/**
|
|
1792
|
+
* Internal implementation shared by .option() and .requiredOption()
|
|
1793
|
+
*
|
|
1794
|
+
* @return {Command} `this` command for chaining
|
|
1795
|
+
* @private
|
|
1796
|
+
*/
|
|
1797
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
1798
|
+
if (typeof flags === "object" && flags instanceof Option2) {
|
|
1799
|
+
throw new Error(
|
|
1800
|
+
"To add an Option object use addOption() instead of option() or requiredOption()"
|
|
1801
|
+
);
|
|
1802
|
+
}
|
|
1803
|
+
const option2 = this.createOption(flags, description);
|
|
1804
|
+
option2.makeOptionMandatory(!!config.mandatory);
|
|
1805
|
+
if (typeof fn === "function") {
|
|
1806
|
+
option2.default(defaultValue).argParser(fn);
|
|
1807
|
+
} else if (fn instanceof RegExp) {
|
|
1808
|
+
const regex = fn;
|
|
1809
|
+
fn = (val, def) => {
|
|
1810
|
+
const m = regex.exec(val);
|
|
1811
|
+
return m ? m[0] : def;
|
|
1812
|
+
};
|
|
1813
|
+
option2.default(defaultValue).argParser(fn);
|
|
1814
|
+
} else {
|
|
1815
|
+
option2.default(fn);
|
|
1816
|
+
}
|
|
1817
|
+
return this.addOption(option2);
|
|
1818
|
+
}
|
|
1819
|
+
/**
|
|
1820
|
+
* Define option with `flags`, `description`, and optional argument parsing function or `defaultValue` or both.
|
|
1821
|
+
*
|
|
1822
|
+
* The `flags` string contains the short and/or long flags, separated by comma, a pipe or space. A required
|
|
1823
|
+
* option-argument is indicated by `<>` and an optional option-argument by `[]`.
|
|
1824
|
+
*
|
|
1825
|
+
* See the README for more details, and see also addOption() and requiredOption().
|
|
1826
|
+
*
|
|
1827
|
+
* @example
|
|
1828
|
+
* program
|
|
1829
|
+
* .option('-p, --pepper', 'add pepper')
|
|
1830
|
+
* .option('--pt, --pizza-type <TYPE>', 'type of pizza') // required option-argument
|
|
1831
|
+
* .option('-c, --cheese [CHEESE]', 'add extra cheese', 'mozzarella') // optional option-argument with default
|
|
1832
|
+
* .option('-t, --tip <VALUE>', 'add tip to purchase cost', parseFloat) // custom parse function
|
|
1833
|
+
*
|
|
1834
|
+
* @param {string} flags
|
|
1835
|
+
* @param {string} [description]
|
|
1836
|
+
* @param {(Function|*)} [parseArg] - custom option processing function or default value
|
|
1837
|
+
* @param {*} [defaultValue]
|
|
1838
|
+
* @return {Command} `this` command for chaining
|
|
1839
|
+
*/
|
|
1840
|
+
option(flags, description, parseArg, defaultValue) {
|
|
1841
|
+
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
1842
|
+
}
|
|
1843
|
+
/**
|
|
1844
|
+
* Add a required option which must have a value after parsing. This usually means
|
|
1845
|
+
* the option must be specified on the command line. (Otherwise the same as .option().)
|
|
1846
|
+
*
|
|
1847
|
+
* The `flags` string contains the short and/or long flags, separated by comma, a pipe or space.
|
|
1848
|
+
*
|
|
1849
|
+
* @param {string} flags
|
|
1850
|
+
* @param {string} [description]
|
|
1851
|
+
* @param {(Function|*)} [parseArg] - custom option processing function or default value
|
|
1852
|
+
* @param {*} [defaultValue]
|
|
1853
|
+
* @return {Command} `this` command for chaining
|
|
1854
|
+
*/
|
|
1855
|
+
requiredOption(flags, description, parseArg, defaultValue) {
|
|
1856
|
+
return this._optionEx(
|
|
1857
|
+
{ mandatory: true },
|
|
1858
|
+
flags,
|
|
1859
|
+
description,
|
|
1860
|
+
parseArg,
|
|
1861
|
+
defaultValue
|
|
1862
|
+
);
|
|
1863
|
+
}
|
|
1864
|
+
/**
|
|
1865
|
+
* Alter parsing of short flags with optional values.
|
|
1866
|
+
*
|
|
1867
|
+
* @example
|
|
1868
|
+
* // for `.option('-f,--flag [value]'):
|
|
1869
|
+
* program.combineFlagAndOptionalValue(true); // `-f80` is treated like `--flag=80`, this is the default behaviour
|
|
1870
|
+
* program.combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
|
|
1871
|
+
*
|
|
1872
|
+
* @param {boolean} [combine] - if `true` or omitted, an optional value can be specified directly after the flag.
|
|
1873
|
+
* @return {Command} `this` command for chaining
|
|
1874
|
+
*/
|
|
1875
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
1876
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
1877
|
+
return this;
|
|
1878
|
+
}
|
|
1879
|
+
/**
|
|
1880
|
+
* Allow unknown options on the command line.
|
|
1881
|
+
*
|
|
1882
|
+
* @param {boolean} [allowUnknown] - if `true` or omitted, no error will be thrown for unknown options.
|
|
1883
|
+
* @return {Command} `this` command for chaining
|
|
1884
|
+
*/
|
|
1885
|
+
allowUnknownOption(allowUnknown = true) {
|
|
1886
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
1887
|
+
return this;
|
|
1888
|
+
}
|
|
1889
|
+
/**
|
|
1890
|
+
* Allow excess command-arguments on the command line. Pass false to make excess arguments an error.
|
|
1891
|
+
*
|
|
1892
|
+
* @param {boolean} [allowExcess] - if `true` or omitted, no error will be thrown for excess arguments.
|
|
1893
|
+
* @return {Command} `this` command for chaining
|
|
1894
|
+
*/
|
|
1895
|
+
allowExcessArguments(allowExcess = true) {
|
|
1896
|
+
this._allowExcessArguments = !!allowExcess;
|
|
1897
|
+
return this;
|
|
1898
|
+
}
|
|
1899
|
+
/**
|
|
1900
|
+
* Enable positional options. Positional means global options are specified before subcommands which lets
|
|
1901
|
+
* subcommands reuse the same option names, and also enables subcommands to turn on passThroughOptions.
|
|
1902
|
+
* The default behaviour is non-positional and global options may appear anywhere on the command line.
|
|
1903
|
+
*
|
|
1904
|
+
* @param {boolean} [positional]
|
|
1905
|
+
* @return {Command} `this` command for chaining
|
|
1906
|
+
*/
|
|
1907
|
+
enablePositionalOptions(positional = true) {
|
|
1908
|
+
this._enablePositionalOptions = !!positional;
|
|
1909
|
+
return this;
|
|
1910
|
+
}
|
|
1911
|
+
/**
|
|
1912
|
+
* Pass through options that come after command-arguments rather than treat them as command-options,
|
|
1913
|
+
* so actual command-options come before command-arguments. Turning this on for a subcommand requires
|
|
1914
|
+
* positional options to have been enabled on the program (parent commands).
|
|
1915
|
+
* The default behaviour is non-positional and options may appear before or after command-arguments.
|
|
1916
|
+
*
|
|
1917
|
+
* @param {boolean} [passThrough] for unknown options.
|
|
1918
|
+
* @return {Command} `this` command for chaining
|
|
1919
|
+
*/
|
|
1920
|
+
passThroughOptions(passThrough = true) {
|
|
1921
|
+
this._passThroughOptions = !!passThrough;
|
|
1922
|
+
this._checkForBrokenPassThrough();
|
|
1923
|
+
return this;
|
|
1924
|
+
}
|
|
1925
|
+
/**
|
|
1926
|
+
* @private
|
|
1927
|
+
*/
|
|
1928
|
+
_checkForBrokenPassThrough() {
|
|
1929
|
+
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
1930
|
+
throw new Error(
|
|
1931
|
+
`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`
|
|
1932
|
+
);
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
/**
|
|
1936
|
+
* Whether to store option values as properties on command object,
|
|
1937
|
+
* or store separately (specify false). In both cases the option values can be accessed using .opts().
|
|
1938
|
+
*
|
|
1939
|
+
* @param {boolean} [storeAsProperties=true]
|
|
1940
|
+
* @return {Command} `this` command for chaining
|
|
1941
|
+
*/
|
|
1942
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
1943
|
+
if (this.options.length) {
|
|
1944
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
1945
|
+
}
|
|
1946
|
+
if (Object.keys(this._optionValues).length) {
|
|
1947
|
+
throw new Error(
|
|
1948
|
+
"call .storeOptionsAsProperties() before setting option values"
|
|
1949
|
+
);
|
|
1950
|
+
}
|
|
1951
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1952
|
+
return this;
|
|
1953
|
+
}
|
|
1954
|
+
/**
|
|
1955
|
+
* Retrieve option value.
|
|
1956
|
+
*
|
|
1957
|
+
* @param {string} key
|
|
1958
|
+
* @return {object} value
|
|
1959
|
+
*/
|
|
1960
|
+
getOptionValue(key) {
|
|
1961
|
+
if (this._storeOptionsAsProperties) {
|
|
1962
|
+
return this[key];
|
|
1963
|
+
}
|
|
1964
|
+
return this._optionValues[key];
|
|
1965
|
+
}
|
|
1966
|
+
/**
|
|
1967
|
+
* Store option value.
|
|
1968
|
+
*
|
|
1969
|
+
* @param {string} key
|
|
1970
|
+
* @param {object} value
|
|
1971
|
+
* @return {Command} `this` command for chaining
|
|
1972
|
+
*/
|
|
1973
|
+
setOptionValue(key, value) {
|
|
1974
|
+
return this.setOptionValueWithSource(key, value, void 0);
|
|
1975
|
+
}
|
|
1976
|
+
/**
|
|
1977
|
+
* Store option value and where the value came from.
|
|
1978
|
+
*
|
|
1979
|
+
* @param {string} key
|
|
1980
|
+
* @param {object} value
|
|
1981
|
+
* @param {string} source - expected values are default/config/env/cli/implied
|
|
1982
|
+
* @return {Command} `this` command for chaining
|
|
1983
|
+
*/
|
|
1984
|
+
setOptionValueWithSource(key, value, source) {
|
|
1985
|
+
if (this._storeOptionsAsProperties) {
|
|
1986
|
+
this[key] = value;
|
|
1987
|
+
} else {
|
|
1988
|
+
this._optionValues[key] = value;
|
|
1989
|
+
}
|
|
1990
|
+
this._optionValueSources[key] = source;
|
|
1991
|
+
return this;
|
|
1992
|
+
}
|
|
1993
|
+
/**
|
|
1994
|
+
* Get source of option value.
|
|
1995
|
+
* Expected values are default | config | env | cli | implied
|
|
1996
|
+
*
|
|
1997
|
+
* @param {string} key
|
|
1998
|
+
* @return {string}
|
|
1999
|
+
*/
|
|
2000
|
+
getOptionValueSource(key) {
|
|
2001
|
+
return this._optionValueSources[key];
|
|
2002
|
+
}
|
|
2003
|
+
/**
|
|
2004
|
+
* Get source of option value. See also .optsWithGlobals().
|
|
2005
|
+
* Expected values are default | config | env | cli | implied
|
|
2006
|
+
*
|
|
2007
|
+
* @param {string} key
|
|
2008
|
+
* @return {string}
|
|
2009
|
+
*/
|
|
2010
|
+
getOptionValueSourceWithGlobals(key) {
|
|
2011
|
+
let source;
|
|
2012
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
2013
|
+
if (cmd.getOptionValueSource(key) !== void 0) {
|
|
2014
|
+
source = cmd.getOptionValueSource(key);
|
|
2015
|
+
}
|
|
2016
|
+
});
|
|
2017
|
+
return source;
|
|
2018
|
+
}
|
|
2019
|
+
/**
|
|
2020
|
+
* Get user arguments from implied or explicit arguments.
|
|
2021
|
+
* Side-effects: set _scriptPath if args included script. Used for default program name, and subcommand searches.
|
|
2022
|
+
*
|
|
2023
|
+
* @private
|
|
2024
|
+
*/
|
|
2025
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
2026
|
+
if (argv !== void 0 && !Array.isArray(argv)) {
|
|
2027
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
2028
|
+
}
|
|
2029
|
+
parseOptions = parseOptions || {};
|
|
2030
|
+
if (argv === void 0 && parseOptions.from === void 0) {
|
|
2031
|
+
if (process2.versions?.electron) {
|
|
2032
|
+
parseOptions.from = "electron";
|
|
2033
|
+
}
|
|
2034
|
+
const execArgv = process2.execArgv ?? [];
|
|
2035
|
+
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
2036
|
+
parseOptions.from = "eval";
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
if (argv === void 0) {
|
|
2040
|
+
argv = process2.argv;
|
|
2041
|
+
}
|
|
2042
|
+
this.rawArgs = argv.slice();
|
|
2043
|
+
let userArgs;
|
|
2044
|
+
switch (parseOptions.from) {
|
|
2045
|
+
case void 0:
|
|
2046
|
+
case "node":
|
|
2047
|
+
this._scriptPath = argv[1];
|
|
2048
|
+
userArgs = argv.slice(2);
|
|
2049
|
+
break;
|
|
2050
|
+
case "electron":
|
|
2051
|
+
if (process2.defaultApp) {
|
|
2052
|
+
this._scriptPath = argv[1];
|
|
2053
|
+
userArgs = argv.slice(2);
|
|
2054
|
+
} else {
|
|
2055
|
+
userArgs = argv.slice(1);
|
|
2056
|
+
}
|
|
2057
|
+
break;
|
|
2058
|
+
case "user":
|
|
2059
|
+
userArgs = argv.slice(0);
|
|
2060
|
+
break;
|
|
2061
|
+
case "eval":
|
|
2062
|
+
userArgs = argv.slice(1);
|
|
2063
|
+
break;
|
|
2064
|
+
default:
|
|
2065
|
+
throw new Error(
|
|
2066
|
+
`unexpected parse option { from: '${parseOptions.from}' }`
|
|
2067
|
+
);
|
|
2068
|
+
}
|
|
2069
|
+
if (!this._name && this._scriptPath)
|
|
2070
|
+
this.nameFromFilename(this._scriptPath);
|
|
2071
|
+
this._name = this._name || "program";
|
|
2072
|
+
return userArgs;
|
|
2073
|
+
}
|
|
2074
|
+
/**
|
|
2075
|
+
* Parse `argv`, setting options and invoking commands when defined.
|
|
2076
|
+
*
|
|
2077
|
+
* Use parseAsync instead of parse if any of your action handlers are async.
|
|
2078
|
+
*
|
|
2079
|
+
* Call with no parameters to parse `process.argv`. Detects Electron and special node options like `node --eval`. Easy mode!
|
|
2080
|
+
*
|
|
2081
|
+
* Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:
|
|
2082
|
+
* - `'node'`: default, `argv[0]` is the application and `argv[1]` is the script being run, with user arguments after that
|
|
2083
|
+
* - `'electron'`: `argv[0]` is the application and `argv[1]` varies depending on whether the electron application is packaged
|
|
2084
|
+
* - `'user'`: just user arguments
|
|
2085
|
+
*
|
|
2086
|
+
* @example
|
|
2087
|
+
* program.parse(); // parse process.argv and auto-detect electron and special node flags
|
|
2088
|
+
* program.parse(process.argv); // assume argv[0] is app and argv[1] is script
|
|
2089
|
+
* program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|
2090
|
+
*
|
|
2091
|
+
* @param {string[]} [argv] - optional, defaults to process.argv
|
|
2092
|
+
* @param {object} [parseOptions] - optionally specify style of options with from: node/user/electron
|
|
2093
|
+
* @param {string} [parseOptions.from] - where the args are from: 'node', 'user', 'electron'
|
|
2094
|
+
* @return {Command} `this` command for chaining
|
|
2095
|
+
*/
|
|
2096
|
+
parse(argv, parseOptions) {
|
|
2097
|
+
this._prepareForParse();
|
|
2098
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
2099
|
+
this._parseCommand([], userArgs);
|
|
2100
|
+
return this;
|
|
2101
|
+
}
|
|
2102
|
+
/**
|
|
2103
|
+
* Parse `argv`, setting options and invoking commands when defined.
|
|
2104
|
+
*
|
|
2105
|
+
* Call with no parameters to parse `process.argv`. Detects Electron and special node options like `node --eval`. Easy mode!
|
|
2106
|
+
*
|
|
2107
|
+
* Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:
|
|
2108
|
+
* - `'node'`: default, `argv[0]` is the application and `argv[1]` is the script being run, with user arguments after that
|
|
2109
|
+
* - `'electron'`: `argv[0]` is the application and `argv[1]` varies depending on whether the electron application is packaged
|
|
2110
|
+
* - `'user'`: just user arguments
|
|
2111
|
+
*
|
|
2112
|
+
* @example
|
|
2113
|
+
* await program.parseAsync(); // parse process.argv and auto-detect electron and special node flags
|
|
2114
|
+
* await program.parseAsync(process.argv); // assume argv[0] is app and argv[1] is script
|
|
2115
|
+
* await program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|
2116
|
+
*
|
|
2117
|
+
* @param {string[]} [argv]
|
|
2118
|
+
* @param {object} [parseOptions]
|
|
2119
|
+
* @param {string} parseOptions.from - where the args are from: 'node', 'user', 'electron'
|
|
2120
|
+
* @return {Promise}
|
|
2121
|
+
*/
|
|
2122
|
+
async parseAsync(argv, parseOptions) {
|
|
2123
|
+
this._prepareForParse();
|
|
2124
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
2125
|
+
await this._parseCommand([], userArgs);
|
|
2126
|
+
return this;
|
|
2127
|
+
}
|
|
2128
|
+
_prepareForParse() {
|
|
2129
|
+
if (this._savedState === null) {
|
|
2130
|
+
this.saveStateBeforeParse();
|
|
2131
|
+
} else {
|
|
2132
|
+
this.restoreStateBeforeParse();
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
/**
|
|
2136
|
+
* Called the first time parse is called to save state and allow a restore before subsequent calls to parse.
|
|
2137
|
+
* Not usually called directly, but available for subclasses to save their custom state.
|
|
2138
|
+
*
|
|
2139
|
+
* This is called in a lazy way. Only commands used in parsing chain will have state saved.
|
|
2140
|
+
*/
|
|
2141
|
+
saveStateBeforeParse() {
|
|
2142
|
+
this._savedState = {
|
|
2143
|
+
// name is stable if supplied by author, but may be unspecified for root command and deduced during parsing
|
|
2144
|
+
_name: this._name,
|
|
2145
|
+
// option values before parse have default values (including false for negated options)
|
|
2146
|
+
// shallow clones
|
|
2147
|
+
_optionValues: { ...this._optionValues },
|
|
2148
|
+
_optionValueSources: { ...this._optionValueSources }
|
|
2149
|
+
};
|
|
2150
|
+
}
|
|
2151
|
+
/**
|
|
2152
|
+
* Restore state before parse for calls after the first.
|
|
2153
|
+
* Not usually called directly, but available for subclasses to save their custom state.
|
|
2154
|
+
*
|
|
2155
|
+
* This is called in a lazy way. Only commands used in parsing chain will have state restored.
|
|
2156
|
+
*/
|
|
2157
|
+
restoreStateBeforeParse() {
|
|
2158
|
+
if (this._storeOptionsAsProperties)
|
|
2159
|
+
throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
|
|
2160
|
+
- either make a new Command for each call to parse, or stop storing options as properties`);
|
|
2161
|
+
this._name = this._savedState._name;
|
|
2162
|
+
this._scriptPath = null;
|
|
2163
|
+
this.rawArgs = [];
|
|
2164
|
+
this._optionValues = { ...this._savedState._optionValues };
|
|
2165
|
+
this._optionValueSources = { ...this._savedState._optionValueSources };
|
|
2166
|
+
this.args = [];
|
|
2167
|
+
this.processedArgs = [];
|
|
2168
|
+
}
|
|
2169
|
+
/**
|
|
2170
|
+
* Throw if expected executable is missing. Add lots of help for author.
|
|
2171
|
+
*
|
|
2172
|
+
* @param {string} executableFile
|
|
2173
|
+
* @param {string} executableDir
|
|
2174
|
+
* @param {string} subcommandName
|
|
2175
|
+
*/
|
|
2176
|
+
_checkForMissingExecutable(executableFile, executableDir, subcommandName) {
|
|
2177
|
+
if (fs.existsSync(executableFile)) return;
|
|
2178
|
+
const executableDirMessage = executableDir ? `searched for local subcommand relative to directory '${executableDir}'` : "no directory for search for local subcommand, use .executableDir() to supply a custom directory";
|
|
2179
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
2180
|
+
- if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
2181
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
2182
|
+
- ${executableDirMessage}`;
|
|
2183
|
+
throw new Error(executableMissing);
|
|
2184
|
+
}
|
|
2185
|
+
/**
|
|
2186
|
+
* Execute a sub-command executable.
|
|
2187
|
+
*
|
|
2188
|
+
* @private
|
|
2189
|
+
*/
|
|
2190
|
+
_executeSubCommand(subcommand, args) {
|
|
2191
|
+
args = args.slice();
|
|
2192
|
+
let launchWithNode = false;
|
|
2193
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
2194
|
+
function findFile(baseDir, baseName) {
|
|
2195
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
2196
|
+
if (fs.existsSync(localBin)) return localBin;
|
|
2197
|
+
if (sourceExt.includes(path.extname(baseName))) return void 0;
|
|
2198
|
+
const foundExt = sourceExt.find(
|
|
2199
|
+
(ext) => fs.existsSync(`${localBin}${ext}`)
|
|
2200
|
+
);
|
|
2201
|
+
if (foundExt) return `${localBin}${foundExt}`;
|
|
2202
|
+
return void 0;
|
|
2203
|
+
}
|
|
2204
|
+
this._checkForMissingMandatoryOptions();
|
|
2205
|
+
this._checkForConflictingOptions();
|
|
2206
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
2207
|
+
let executableDir = this._executableDir || "";
|
|
2208
|
+
if (this._scriptPath) {
|
|
2209
|
+
let resolvedScriptPath;
|
|
2210
|
+
try {
|
|
2211
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
2212
|
+
} catch {
|
|
2213
|
+
resolvedScriptPath = this._scriptPath;
|
|
2214
|
+
}
|
|
2215
|
+
executableDir = path.resolve(
|
|
2216
|
+
path.dirname(resolvedScriptPath),
|
|
2217
|
+
executableDir
|
|
2218
|
+
);
|
|
2219
|
+
}
|
|
2220
|
+
if (executableDir) {
|
|
2221
|
+
let localFile = findFile(executableDir, executableFile);
|
|
2222
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
2223
|
+
const legacyName = path.basename(
|
|
2224
|
+
this._scriptPath,
|
|
2225
|
+
path.extname(this._scriptPath)
|
|
2226
|
+
);
|
|
2227
|
+
if (legacyName !== this._name) {
|
|
2228
|
+
localFile = findFile(
|
|
2229
|
+
executableDir,
|
|
2230
|
+
`${legacyName}-${subcommand._name}`
|
|
2231
|
+
);
|
|
2232
|
+
}
|
|
2233
|
+
}
|
|
2234
|
+
executableFile = localFile || executableFile;
|
|
2235
|
+
}
|
|
2236
|
+
launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
2237
|
+
let proc;
|
|
2238
|
+
if (process2.platform !== "win32") {
|
|
2239
|
+
if (launchWithNode) {
|
|
2240
|
+
args.unshift(executableFile);
|
|
2241
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
2242
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
2243
|
+
} else {
|
|
2244
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
2245
|
+
}
|
|
2246
|
+
} else {
|
|
2247
|
+
this._checkForMissingExecutable(
|
|
2248
|
+
executableFile,
|
|
2249
|
+
executableDir,
|
|
2250
|
+
subcommand._name
|
|
2251
|
+
);
|
|
2252
|
+
args.unshift(executableFile);
|
|
2253
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
2254
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
2255
|
+
}
|
|
2256
|
+
if (!proc.killed) {
|
|
2257
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
2258
|
+
signals.forEach((signal) => {
|
|
2259
|
+
process2.on(signal, () => {
|
|
2260
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
2261
|
+
proc.kill(signal);
|
|
2262
|
+
}
|
|
2263
|
+
});
|
|
2264
|
+
});
|
|
2265
|
+
}
|
|
2266
|
+
const exitCallback = this._exitCallback;
|
|
2267
|
+
proc.on("close", (code) => {
|
|
2268
|
+
code = code ?? 1;
|
|
2269
|
+
if (!exitCallback) {
|
|
2270
|
+
process2.exit(code);
|
|
2271
|
+
} else {
|
|
2272
|
+
exitCallback(
|
|
2273
|
+
new CommanderError2(
|
|
2274
|
+
code,
|
|
2275
|
+
"commander.executeSubCommandAsync",
|
|
2276
|
+
"(close)"
|
|
2277
|
+
)
|
|
2278
|
+
);
|
|
2279
|
+
}
|
|
2280
|
+
});
|
|
2281
|
+
proc.on("error", (err) => {
|
|
2282
|
+
if (err.code === "ENOENT") {
|
|
2283
|
+
this._checkForMissingExecutable(
|
|
2284
|
+
executableFile,
|
|
2285
|
+
executableDir,
|
|
2286
|
+
subcommand._name
|
|
2287
|
+
);
|
|
2288
|
+
} else if (err.code === "EACCES") {
|
|
2289
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
2290
|
+
}
|
|
2291
|
+
if (!exitCallback) {
|
|
2292
|
+
process2.exit(1);
|
|
2293
|
+
} else {
|
|
2294
|
+
const wrappedError = new CommanderError2(
|
|
2295
|
+
1,
|
|
2296
|
+
"commander.executeSubCommandAsync",
|
|
2297
|
+
"(error)"
|
|
2298
|
+
);
|
|
2299
|
+
wrappedError.nestedError = err;
|
|
2300
|
+
exitCallback(wrappedError);
|
|
2301
|
+
}
|
|
2302
|
+
});
|
|
2303
|
+
this.runningCommand = proc;
|
|
2304
|
+
}
|
|
2305
|
+
/**
|
|
2306
|
+
* @private
|
|
2307
|
+
*/
|
|
2308
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
2309
|
+
const subCommand = this._findCommand(commandName);
|
|
2310
|
+
if (!subCommand) this.help({ error: true });
|
|
2311
|
+
subCommand._prepareForParse();
|
|
2312
|
+
let promiseChain;
|
|
2313
|
+
promiseChain = this._chainOrCallSubCommandHook(
|
|
2314
|
+
promiseChain,
|
|
2315
|
+
subCommand,
|
|
2316
|
+
"preSubcommand"
|
|
2317
|
+
);
|
|
2318
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
2319
|
+
if (subCommand._executableHandler) {
|
|
2320
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
2321
|
+
} else {
|
|
2322
|
+
return subCommand._parseCommand(operands, unknown);
|
|
2323
|
+
}
|
|
2324
|
+
});
|
|
2325
|
+
return promiseChain;
|
|
2326
|
+
}
|
|
2327
|
+
/**
|
|
2328
|
+
* Invoke help directly if possible, or dispatch if necessary.
|
|
2329
|
+
* e.g. help foo
|
|
2330
|
+
*
|
|
2331
|
+
* @private
|
|
2332
|
+
*/
|
|
2333
|
+
_dispatchHelpCommand(subcommandName) {
|
|
2334
|
+
if (!subcommandName) {
|
|
2335
|
+
this.help();
|
|
2336
|
+
}
|
|
2337
|
+
const subCommand = this._findCommand(subcommandName);
|
|
2338
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
2339
|
+
subCommand.help();
|
|
2340
|
+
}
|
|
2341
|
+
return this._dispatchSubcommand(
|
|
2342
|
+
subcommandName,
|
|
2343
|
+
[],
|
|
2344
|
+
[this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]
|
|
2345
|
+
);
|
|
2346
|
+
}
|
|
2347
|
+
/**
|
|
2348
|
+
* Check this.args against expected this.registeredArguments.
|
|
2349
|
+
*
|
|
2350
|
+
* @private
|
|
2351
|
+
*/
|
|
2352
|
+
_checkNumberOfArguments() {
|
|
2353
|
+
this.registeredArguments.forEach((arg, i) => {
|
|
2354
|
+
if (arg.required && this.args[i] == null) {
|
|
2355
|
+
this.missingArgument(arg.name());
|
|
2356
|
+
}
|
|
2357
|
+
});
|
|
2358
|
+
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
2359
|
+
return;
|
|
2360
|
+
}
|
|
2361
|
+
if (this.args.length > this.registeredArguments.length) {
|
|
2362
|
+
this._excessArguments(this.args);
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
/**
|
|
2366
|
+
* Process this.args using this.registeredArguments and save as this.processedArgs!
|
|
2367
|
+
*
|
|
2368
|
+
* @private
|
|
2369
|
+
*/
|
|
2370
|
+
_processArguments() {
|
|
2371
|
+
const myParseArg = (argument2, value, previous) => {
|
|
2372
|
+
let parsedValue = value;
|
|
2373
|
+
if (value !== null && argument2.parseArg) {
|
|
2374
|
+
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument2.name()}'.`;
|
|
2375
|
+
parsedValue = this._callParseArg(
|
|
2376
|
+
argument2,
|
|
2377
|
+
value,
|
|
2378
|
+
previous,
|
|
2379
|
+
invalidValueMessage
|
|
2380
|
+
);
|
|
2381
|
+
}
|
|
2382
|
+
return parsedValue;
|
|
2383
|
+
};
|
|
2384
|
+
this._checkNumberOfArguments();
|
|
2385
|
+
const processedArgs = [];
|
|
2386
|
+
this.registeredArguments.forEach((declaredArg, index) => {
|
|
2387
|
+
let value = declaredArg.defaultValue;
|
|
2388
|
+
if (declaredArg.variadic) {
|
|
2389
|
+
if (index < this.args.length) {
|
|
2390
|
+
value = this.args.slice(index);
|
|
2391
|
+
if (declaredArg.parseArg) {
|
|
2392
|
+
value = value.reduce((processed, v) => {
|
|
2393
|
+
return myParseArg(declaredArg, v, processed);
|
|
2394
|
+
}, declaredArg.defaultValue);
|
|
2395
|
+
}
|
|
2396
|
+
} else if (value === void 0) {
|
|
2397
|
+
value = [];
|
|
2398
|
+
}
|
|
2399
|
+
} else if (index < this.args.length) {
|
|
2400
|
+
value = this.args[index];
|
|
2401
|
+
if (declaredArg.parseArg) {
|
|
2402
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
2403
|
+
}
|
|
2404
|
+
}
|
|
2405
|
+
processedArgs[index] = value;
|
|
2406
|
+
});
|
|
2407
|
+
this.processedArgs = processedArgs;
|
|
2408
|
+
}
|
|
2409
|
+
/**
|
|
2410
|
+
* Once we have a promise we chain, but call synchronously until then.
|
|
2411
|
+
*
|
|
2412
|
+
* @param {(Promise|undefined)} promise
|
|
2413
|
+
* @param {Function} fn
|
|
2414
|
+
* @return {(Promise|undefined)}
|
|
2415
|
+
* @private
|
|
2416
|
+
*/
|
|
2417
|
+
_chainOrCall(promise, fn) {
|
|
2418
|
+
if (promise?.then && typeof promise.then === "function") {
|
|
2419
|
+
return promise.then(() => fn());
|
|
2420
|
+
}
|
|
2421
|
+
return fn();
|
|
2422
|
+
}
|
|
2423
|
+
/**
|
|
2424
|
+
*
|
|
2425
|
+
* @param {(Promise|undefined)} promise
|
|
2426
|
+
* @param {string} event
|
|
2427
|
+
* @return {(Promise|undefined)}
|
|
2428
|
+
* @private
|
|
2429
|
+
*/
|
|
2430
|
+
_chainOrCallHooks(promise, event) {
|
|
2431
|
+
let result = promise;
|
|
2432
|
+
const hooks = [];
|
|
2433
|
+
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== void 0).forEach((hookedCommand) => {
|
|
2434
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
2435
|
+
hooks.push({ hookedCommand, callback });
|
|
2436
|
+
});
|
|
2437
|
+
});
|
|
2438
|
+
if (event === "postAction") {
|
|
2439
|
+
hooks.reverse();
|
|
2440
|
+
}
|
|
2441
|
+
hooks.forEach((hookDetail) => {
|
|
2442
|
+
result = this._chainOrCall(result, () => {
|
|
2443
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
2444
|
+
});
|
|
2445
|
+
});
|
|
2446
|
+
return result;
|
|
2447
|
+
}
|
|
2448
|
+
/**
|
|
2449
|
+
*
|
|
2450
|
+
* @param {(Promise|undefined)} promise
|
|
2451
|
+
* @param {Command} subCommand
|
|
2452
|
+
* @param {string} event
|
|
2453
|
+
* @return {(Promise|undefined)}
|
|
2454
|
+
* @private
|
|
2455
|
+
*/
|
|
2456
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
2457
|
+
let result = promise;
|
|
2458
|
+
if (this._lifeCycleHooks[event] !== void 0) {
|
|
2459
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
2460
|
+
result = this._chainOrCall(result, () => {
|
|
2461
|
+
return hook(this, subCommand);
|
|
2462
|
+
});
|
|
2463
|
+
});
|
|
2464
|
+
}
|
|
2465
|
+
return result;
|
|
2466
|
+
}
|
|
2467
|
+
/**
|
|
2468
|
+
* Process arguments in context of this command.
|
|
2469
|
+
* Returns action result, in case it is a promise.
|
|
2470
|
+
*
|
|
2471
|
+
* @private
|
|
2472
|
+
*/
|
|
2473
|
+
_parseCommand(operands, unknown) {
|
|
2474
|
+
const parsed = this.parseOptions(unknown);
|
|
2475
|
+
this._parseOptionsEnv();
|
|
2476
|
+
this._parseOptionsImplied();
|
|
2477
|
+
operands = operands.concat(parsed.operands);
|
|
2478
|
+
unknown = parsed.unknown;
|
|
2479
|
+
this.args = operands.concat(unknown);
|
|
2480
|
+
if (operands && this._findCommand(operands[0])) {
|
|
2481
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
2482
|
+
}
|
|
2483
|
+
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
2484
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
2485
|
+
}
|
|
2486
|
+
if (this._defaultCommandName) {
|
|
2487
|
+
this._outputHelpIfRequested(unknown);
|
|
2488
|
+
return this._dispatchSubcommand(
|
|
2489
|
+
this._defaultCommandName,
|
|
2490
|
+
operands,
|
|
2491
|
+
unknown
|
|
2492
|
+
);
|
|
2493
|
+
}
|
|
2494
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
2495
|
+
this.help({ error: true });
|
|
2496
|
+
}
|
|
2497
|
+
this._outputHelpIfRequested(parsed.unknown);
|
|
2498
|
+
this._checkForMissingMandatoryOptions();
|
|
2499
|
+
this._checkForConflictingOptions();
|
|
2500
|
+
const checkForUnknownOptions = () => {
|
|
2501
|
+
if (parsed.unknown.length > 0) {
|
|
2502
|
+
this.unknownOption(parsed.unknown[0]);
|
|
2503
|
+
}
|
|
2504
|
+
};
|
|
2505
|
+
const commandEvent = `command:${this.name()}`;
|
|
2506
|
+
if (this._actionHandler) {
|
|
2507
|
+
checkForUnknownOptions();
|
|
2508
|
+
this._processArguments();
|
|
2509
|
+
let promiseChain;
|
|
2510
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
2511
|
+
promiseChain = this._chainOrCall(
|
|
2512
|
+
promiseChain,
|
|
2513
|
+
() => this._actionHandler(this.processedArgs)
|
|
2514
|
+
);
|
|
2515
|
+
if (this.parent) {
|
|
2516
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
2517
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
2518
|
+
});
|
|
2519
|
+
}
|
|
2520
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
2521
|
+
return promiseChain;
|
|
2522
|
+
}
|
|
2523
|
+
if (this.parent?.listenerCount(commandEvent)) {
|
|
2524
|
+
checkForUnknownOptions();
|
|
2525
|
+
this._processArguments();
|
|
2526
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
2527
|
+
} else if (operands.length) {
|
|
2528
|
+
if (this._findCommand("*")) {
|
|
2529
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
2530
|
+
}
|
|
2531
|
+
if (this.listenerCount("command:*")) {
|
|
2532
|
+
this.emit("command:*", operands, unknown);
|
|
2533
|
+
} else if (this.commands.length) {
|
|
2534
|
+
this.unknownCommand();
|
|
2535
|
+
} else {
|
|
2536
|
+
checkForUnknownOptions();
|
|
2537
|
+
this._processArguments();
|
|
2538
|
+
}
|
|
2539
|
+
} else if (this.commands.length) {
|
|
2540
|
+
checkForUnknownOptions();
|
|
2541
|
+
this.help({ error: true });
|
|
2542
|
+
} else {
|
|
2543
|
+
checkForUnknownOptions();
|
|
2544
|
+
this._processArguments();
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
/**
|
|
2548
|
+
* Find matching command.
|
|
2549
|
+
*
|
|
2550
|
+
* @private
|
|
2551
|
+
* @return {Command | undefined}
|
|
2552
|
+
*/
|
|
2553
|
+
_findCommand(name) {
|
|
2554
|
+
if (!name) return void 0;
|
|
2555
|
+
return this.commands.find(
|
|
2556
|
+
(cmd) => cmd._name === name || cmd._aliases.includes(name)
|
|
2557
|
+
);
|
|
2558
|
+
}
|
|
2559
|
+
/**
|
|
2560
|
+
* Return an option matching `arg` if any.
|
|
2561
|
+
*
|
|
2562
|
+
* @param {string} arg
|
|
2563
|
+
* @return {Option}
|
|
2564
|
+
* @package
|
|
2565
|
+
*/
|
|
2566
|
+
_findOption(arg) {
|
|
2567
|
+
return this.options.find((option2) => option2.is(arg));
|
|
2568
|
+
}
|
|
2569
|
+
/**
|
|
2570
|
+
* Display an error message if a mandatory option does not have a value.
|
|
2571
|
+
* Called after checking for help flags in leaf subcommand.
|
|
2572
|
+
*
|
|
2573
|
+
* @private
|
|
2574
|
+
*/
|
|
2575
|
+
_checkForMissingMandatoryOptions() {
|
|
2576
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
2577
|
+
cmd.options.forEach((anOption) => {
|
|
2578
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === void 0) {
|
|
2579
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
2580
|
+
}
|
|
2581
|
+
});
|
|
2582
|
+
});
|
|
2583
|
+
}
|
|
2584
|
+
/**
|
|
2585
|
+
* Display an error message if conflicting options are used together in this.
|
|
2586
|
+
*
|
|
2587
|
+
* @private
|
|
2588
|
+
*/
|
|
2589
|
+
_checkForConflictingLocalOptions() {
|
|
2590
|
+
const definedNonDefaultOptions = this.options.filter((option2) => {
|
|
2591
|
+
const optionKey = option2.attributeName();
|
|
2592
|
+
if (this.getOptionValue(optionKey) === void 0) {
|
|
2593
|
+
return false;
|
|
2594
|
+
}
|
|
2595
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
2596
|
+
});
|
|
2597
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter(
|
|
2598
|
+
(option2) => option2.conflictsWith.length > 0
|
|
2599
|
+
);
|
|
2600
|
+
optionsWithConflicting.forEach((option2) => {
|
|
2601
|
+
const conflictingAndDefined = definedNonDefaultOptions.find(
|
|
2602
|
+
(defined) => option2.conflictsWith.includes(defined.attributeName())
|
|
2603
|
+
);
|
|
2604
|
+
if (conflictingAndDefined) {
|
|
2605
|
+
this._conflictingOption(option2, conflictingAndDefined);
|
|
2606
|
+
}
|
|
2607
|
+
});
|
|
2608
|
+
}
|
|
2609
|
+
/**
|
|
2610
|
+
* Display an error message if conflicting options are used together.
|
|
2611
|
+
* Called after checking for help flags in leaf subcommand.
|
|
2612
|
+
*
|
|
2613
|
+
* @private
|
|
2614
|
+
*/
|
|
2615
|
+
_checkForConflictingOptions() {
|
|
2616
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
2617
|
+
cmd._checkForConflictingLocalOptions();
|
|
2618
|
+
});
|
|
2619
|
+
}
|
|
2620
|
+
/**
|
|
2621
|
+
* Parse options from `argv` removing known options,
|
|
2622
|
+
* and return argv split into operands and unknown arguments.
|
|
2623
|
+
*
|
|
2624
|
+
* Side effects: modifies command by storing options. Does not reset state if called again.
|
|
2625
|
+
*
|
|
2626
|
+
* Examples:
|
|
2627
|
+
*
|
|
2628
|
+
* argv => operands, unknown
|
|
2629
|
+
* --known kkk op => [op], []
|
|
2630
|
+
* op --known kkk => [op], []
|
|
2631
|
+
* sub --unknown uuu op => [sub], [--unknown uuu op]
|
|
2632
|
+
* sub -- --unknown uuu op => [sub --unknown uuu op], []
|
|
2633
|
+
*
|
|
2634
|
+
* @param {string[]} args
|
|
2635
|
+
* @return {{operands: string[], unknown: string[]}}
|
|
2636
|
+
*/
|
|
2637
|
+
parseOptions(args) {
|
|
2638
|
+
const operands = [];
|
|
2639
|
+
const unknown = [];
|
|
2640
|
+
let dest = operands;
|
|
2641
|
+
function maybeOption(arg) {
|
|
2642
|
+
return arg.length > 1 && arg[0] === "-";
|
|
2643
|
+
}
|
|
2644
|
+
const negativeNumberArg = (arg) => {
|
|
2645
|
+
if (!/^-(\d+|\d*\.\d+)(e[+-]?\d+)?$/.test(arg)) return false;
|
|
2646
|
+
return !this._getCommandAndAncestors().some(
|
|
2647
|
+
(cmd) => cmd.options.map((opt) => opt.short).some((short) => /^-\d$/.test(short))
|
|
2648
|
+
);
|
|
2649
|
+
};
|
|
2650
|
+
let activeVariadicOption = null;
|
|
2651
|
+
let activeGroup = null;
|
|
2652
|
+
let i = 0;
|
|
2653
|
+
while (i < args.length || activeGroup) {
|
|
2654
|
+
const arg = activeGroup ?? args[i++];
|
|
2655
|
+
activeGroup = null;
|
|
2656
|
+
if (arg === "--") {
|
|
2657
|
+
if (dest === unknown) dest.push(arg);
|
|
2658
|
+
dest.push(...args.slice(i));
|
|
2659
|
+
break;
|
|
2660
|
+
}
|
|
2661
|
+
if (activeVariadicOption && (!maybeOption(arg) || negativeNumberArg(arg))) {
|
|
2662
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
2663
|
+
continue;
|
|
2664
|
+
}
|
|
2665
|
+
activeVariadicOption = null;
|
|
2666
|
+
if (maybeOption(arg)) {
|
|
2667
|
+
const option2 = this._findOption(arg);
|
|
2668
|
+
if (option2) {
|
|
2669
|
+
if (option2.required) {
|
|
2670
|
+
const value = args[i++];
|
|
2671
|
+
if (value === void 0) this.optionMissingArgument(option2);
|
|
2672
|
+
this.emit(`option:${option2.name()}`, value);
|
|
2673
|
+
} else if (option2.optional) {
|
|
2674
|
+
let value = null;
|
|
2675
|
+
if (i < args.length && (!maybeOption(args[i]) || negativeNumberArg(args[i]))) {
|
|
2676
|
+
value = args[i++];
|
|
2677
|
+
}
|
|
2678
|
+
this.emit(`option:${option2.name()}`, value);
|
|
2679
|
+
} else {
|
|
2680
|
+
this.emit(`option:${option2.name()}`);
|
|
2681
|
+
}
|
|
2682
|
+
activeVariadicOption = option2.variadic ? option2 : null;
|
|
2683
|
+
continue;
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
2687
|
+
const option2 = this._findOption(`-${arg[1]}`);
|
|
2688
|
+
if (option2) {
|
|
2689
|
+
if (option2.required || option2.optional && this._combineFlagAndOptionalValue) {
|
|
2690
|
+
this.emit(`option:${option2.name()}`, arg.slice(2));
|
|
2691
|
+
} else {
|
|
2692
|
+
this.emit(`option:${option2.name()}`);
|
|
2693
|
+
activeGroup = `-${arg.slice(2)}`;
|
|
2694
|
+
}
|
|
2695
|
+
continue;
|
|
2696
|
+
}
|
|
2697
|
+
}
|
|
2698
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
2699
|
+
const index = arg.indexOf("=");
|
|
2700
|
+
const option2 = this._findOption(arg.slice(0, index));
|
|
2701
|
+
if (option2 && (option2.required || option2.optional)) {
|
|
2702
|
+
this.emit(`option:${option2.name()}`, arg.slice(index + 1));
|
|
2703
|
+
continue;
|
|
2704
|
+
}
|
|
2705
|
+
}
|
|
2706
|
+
if (dest === operands && maybeOption(arg) && !(this.commands.length === 0 && negativeNumberArg(arg))) {
|
|
2707
|
+
dest = unknown;
|
|
2708
|
+
}
|
|
2709
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
2710
|
+
if (this._findCommand(arg)) {
|
|
2711
|
+
operands.push(arg);
|
|
2712
|
+
unknown.push(...args.slice(i));
|
|
2713
|
+
break;
|
|
2714
|
+
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
2715
|
+
operands.push(arg, ...args.slice(i));
|
|
2716
|
+
break;
|
|
2717
|
+
} else if (this._defaultCommandName) {
|
|
2718
|
+
unknown.push(arg, ...args.slice(i));
|
|
2719
|
+
break;
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
if (this._passThroughOptions) {
|
|
2723
|
+
dest.push(arg, ...args.slice(i));
|
|
2724
|
+
break;
|
|
2725
|
+
}
|
|
2726
|
+
dest.push(arg);
|
|
2727
|
+
}
|
|
2728
|
+
return { operands, unknown };
|
|
2729
|
+
}
|
|
2730
|
+
/**
|
|
2731
|
+
* Return an object containing local option values as key-value pairs.
|
|
2732
|
+
*
|
|
2733
|
+
* @return {object}
|
|
2734
|
+
*/
|
|
2735
|
+
opts() {
|
|
2736
|
+
if (this._storeOptionsAsProperties) {
|
|
2737
|
+
const result = {};
|
|
2738
|
+
const len = this.options.length;
|
|
2739
|
+
for (let i = 0; i < len; i++) {
|
|
2740
|
+
const key = this.options[i].attributeName();
|
|
2741
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
2742
|
+
}
|
|
2743
|
+
return result;
|
|
2744
|
+
}
|
|
2745
|
+
return this._optionValues;
|
|
2746
|
+
}
|
|
2747
|
+
/**
|
|
2748
|
+
* Return an object containing merged local and global option values as key-value pairs.
|
|
2749
|
+
*
|
|
2750
|
+
* @return {object}
|
|
2751
|
+
*/
|
|
2752
|
+
optsWithGlobals() {
|
|
2753
|
+
return this._getCommandAndAncestors().reduce(
|
|
2754
|
+
(combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()),
|
|
2755
|
+
{}
|
|
2756
|
+
);
|
|
2757
|
+
}
|
|
2758
|
+
/**
|
|
2759
|
+
* Display error message and exit (or call exitOverride).
|
|
2760
|
+
*
|
|
2761
|
+
* @param {string} message
|
|
2762
|
+
* @param {object} [errorOptions]
|
|
2763
|
+
* @param {string} [errorOptions.code] - an id string representing the error
|
|
2764
|
+
* @param {number} [errorOptions.exitCode] - used with process.exit
|
|
2765
|
+
*/
|
|
2766
|
+
error(message, errorOptions) {
|
|
2767
|
+
this._outputConfiguration.outputError(
|
|
2768
|
+
`${message}
|
|
2769
|
+
`,
|
|
2770
|
+
this._outputConfiguration.writeErr
|
|
2771
|
+
);
|
|
2772
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
2773
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
2774
|
+
`);
|
|
2775
|
+
} else if (this._showHelpAfterError) {
|
|
2776
|
+
this._outputConfiguration.writeErr("\n");
|
|
2777
|
+
this.outputHelp({ error: true });
|
|
2778
|
+
}
|
|
2779
|
+
const config = errorOptions || {};
|
|
2780
|
+
const exitCode = config.exitCode || 1;
|
|
2781
|
+
const code = config.code || "commander.error";
|
|
2782
|
+
this._exit(exitCode, code, message);
|
|
2783
|
+
}
|
|
2784
|
+
/**
|
|
2785
|
+
* Apply any option related environment variables, if option does
|
|
2786
|
+
* not have a value from cli or client code.
|
|
2787
|
+
*
|
|
2788
|
+
* @private
|
|
2789
|
+
*/
|
|
2790
|
+
_parseOptionsEnv() {
|
|
2791
|
+
this.options.forEach((option2) => {
|
|
2792
|
+
if (option2.envVar && option2.envVar in process2.env) {
|
|
2793
|
+
const optionKey = option2.attributeName();
|
|
2794
|
+
if (this.getOptionValue(optionKey) === void 0 || ["default", "config", "env"].includes(
|
|
2795
|
+
this.getOptionValueSource(optionKey)
|
|
2796
|
+
)) {
|
|
2797
|
+
if (option2.required || option2.optional) {
|
|
2798
|
+
this.emit(`optionEnv:${option2.name()}`, process2.env[option2.envVar]);
|
|
2799
|
+
} else {
|
|
2800
|
+
this.emit(`optionEnv:${option2.name()}`);
|
|
2801
|
+
}
|
|
2802
|
+
}
|
|
2803
|
+
}
|
|
2804
|
+
});
|
|
2805
|
+
}
|
|
2806
|
+
/**
|
|
2807
|
+
* Apply any implied option values, if option is undefined or default value.
|
|
2808
|
+
*
|
|
2809
|
+
* @private
|
|
2810
|
+
*/
|
|
2811
|
+
_parseOptionsImplied() {
|
|
2812
|
+
const dualHelper = new DualOptions(this.options);
|
|
2813
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
2814
|
+
return this.getOptionValue(optionKey) !== void 0 && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
2815
|
+
};
|
|
2816
|
+
this.options.filter(
|
|
2817
|
+
(option2) => option2.implied !== void 0 && hasCustomOptionValue(option2.attributeName()) && dualHelper.valueFromOption(
|
|
2818
|
+
this.getOptionValue(option2.attributeName()),
|
|
2819
|
+
option2
|
|
2820
|
+
)
|
|
2821
|
+
).forEach((option2) => {
|
|
2822
|
+
Object.keys(option2.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
2823
|
+
this.setOptionValueWithSource(
|
|
2824
|
+
impliedKey,
|
|
2825
|
+
option2.implied[impliedKey],
|
|
2826
|
+
"implied"
|
|
2827
|
+
);
|
|
2828
|
+
});
|
|
2829
|
+
});
|
|
2830
|
+
}
|
|
2831
|
+
/**
|
|
2832
|
+
* Argument `name` is missing.
|
|
2833
|
+
*
|
|
2834
|
+
* @param {string} name
|
|
2835
|
+
* @private
|
|
2836
|
+
*/
|
|
2837
|
+
missingArgument(name) {
|
|
2838
|
+
const message = `error: missing required argument '${name}'`;
|
|
2839
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
2840
|
+
}
|
|
2841
|
+
/**
|
|
2842
|
+
* `Option` is missing an argument.
|
|
2843
|
+
*
|
|
2844
|
+
* @param {Option} option
|
|
2845
|
+
* @private
|
|
2846
|
+
*/
|
|
2847
|
+
optionMissingArgument(option2) {
|
|
2848
|
+
const message = `error: option '${option2.flags}' argument missing`;
|
|
2849
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
2850
|
+
}
|
|
2851
|
+
/**
|
|
2852
|
+
* `Option` does not have a value, and is a mandatory option.
|
|
2853
|
+
*
|
|
2854
|
+
* @param {Option} option
|
|
2855
|
+
* @private
|
|
2856
|
+
*/
|
|
2857
|
+
missingMandatoryOptionValue(option2) {
|
|
2858
|
+
const message = `error: required option '${option2.flags}' not specified`;
|
|
2859
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
2860
|
+
}
|
|
2861
|
+
/**
|
|
2862
|
+
* `Option` conflicts with another option.
|
|
2863
|
+
*
|
|
2864
|
+
* @param {Option} option
|
|
2865
|
+
* @param {Option} conflictingOption
|
|
2866
|
+
* @private
|
|
2867
|
+
*/
|
|
2868
|
+
_conflictingOption(option2, conflictingOption) {
|
|
2869
|
+
const findBestOptionFromValue = (option3) => {
|
|
2870
|
+
const optionKey = option3.attributeName();
|
|
2871
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
2872
|
+
const negativeOption = this.options.find(
|
|
2873
|
+
(target) => target.negate && optionKey === target.attributeName()
|
|
2874
|
+
);
|
|
2875
|
+
const positiveOption = this.options.find(
|
|
2876
|
+
(target) => !target.negate && optionKey === target.attributeName()
|
|
2877
|
+
);
|
|
2878
|
+
if (negativeOption && (negativeOption.presetArg === void 0 && optionValue === false || negativeOption.presetArg !== void 0 && optionValue === negativeOption.presetArg)) {
|
|
2879
|
+
return negativeOption;
|
|
2880
|
+
}
|
|
2881
|
+
return positiveOption || option3;
|
|
2882
|
+
};
|
|
2883
|
+
const getErrorMessage = (option3) => {
|
|
2884
|
+
const bestOption = findBestOptionFromValue(option3);
|
|
2885
|
+
const optionKey = bestOption.attributeName();
|
|
2886
|
+
const source = this.getOptionValueSource(optionKey);
|
|
2887
|
+
if (source === "env") {
|
|
2888
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
2889
|
+
}
|
|
2890
|
+
return `option '${bestOption.flags}'`;
|
|
2891
|
+
};
|
|
2892
|
+
const message = `error: ${getErrorMessage(option2)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
2893
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
2894
|
+
}
|
|
2895
|
+
/**
|
|
2896
|
+
* Unknown option `flag`.
|
|
2897
|
+
*
|
|
2898
|
+
* @param {string} flag
|
|
2899
|
+
* @private
|
|
2900
|
+
*/
|
|
2901
|
+
unknownOption(flag) {
|
|
2902
|
+
if (this._allowUnknownOption) return;
|
|
2903
|
+
let suggestion = "";
|
|
2904
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
2905
|
+
let candidateFlags = [];
|
|
2906
|
+
let command2 = this;
|
|
2907
|
+
do {
|
|
2908
|
+
const moreFlags = command2.createHelp().visibleOptions(command2).filter((option2) => option2.long).map((option2) => option2.long);
|
|
2909
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
2910
|
+
command2 = command2.parent;
|
|
2911
|
+
} while (command2 && !command2._enablePositionalOptions);
|
|
2912
|
+
suggestion = suggestSimilar2(flag, candidateFlags);
|
|
2913
|
+
}
|
|
2914
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
2915
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
2916
|
+
}
|
|
2917
|
+
/**
|
|
2918
|
+
* Excess arguments, more than expected.
|
|
2919
|
+
*
|
|
2920
|
+
* @param {string[]} receivedArgs
|
|
2921
|
+
* @private
|
|
2922
|
+
*/
|
|
2923
|
+
_excessArguments(receivedArgs) {
|
|
2924
|
+
if (this._allowExcessArguments) return;
|
|
2925
|
+
const expected = this.registeredArguments.length;
|
|
2926
|
+
const s = expected === 1 ? "" : "s";
|
|
2927
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
2928
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
2929
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
2930
|
+
}
|
|
2931
|
+
/**
|
|
2932
|
+
* Unknown command.
|
|
2933
|
+
*
|
|
2934
|
+
* @private
|
|
2935
|
+
*/
|
|
2936
|
+
unknownCommand() {
|
|
2937
|
+
const unknownName = this.args[0];
|
|
2938
|
+
let suggestion = "";
|
|
2939
|
+
if (this._showSuggestionAfterError) {
|
|
2940
|
+
const candidateNames = [];
|
|
2941
|
+
this.createHelp().visibleCommands(this).forEach((command2) => {
|
|
2942
|
+
candidateNames.push(command2.name());
|
|
2943
|
+
if (command2.alias()) candidateNames.push(command2.alias());
|
|
2944
|
+
});
|
|
2945
|
+
suggestion = suggestSimilar2(unknownName, candidateNames);
|
|
2946
|
+
}
|
|
2947
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
2948
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
2949
|
+
}
|
|
2950
|
+
/**
|
|
2951
|
+
* Get or set the program version.
|
|
2952
|
+
*
|
|
2953
|
+
* This method auto-registers the "-V, --version" option which will print the version number.
|
|
2954
|
+
*
|
|
2955
|
+
* You can optionally supply the flags and description to override the defaults.
|
|
2956
|
+
*
|
|
2957
|
+
* @param {string} [str]
|
|
2958
|
+
* @param {string} [flags]
|
|
2959
|
+
* @param {string} [description]
|
|
2960
|
+
* @return {(this | string | undefined)} `this` command for chaining, or version string if no arguments
|
|
2961
|
+
*/
|
|
2962
|
+
version(str, flags, description) {
|
|
2963
|
+
if (str === void 0) return this._version;
|
|
2964
|
+
this._version = str;
|
|
2965
|
+
flags = flags || "-V, --version";
|
|
2966
|
+
description = description || "output the version number";
|
|
2967
|
+
const versionOption = this.createOption(flags, description);
|
|
2968
|
+
this._versionOptionName = versionOption.attributeName();
|
|
2969
|
+
this._registerOption(versionOption);
|
|
2970
|
+
this.on("option:" + versionOption.name(), () => {
|
|
2971
|
+
this._outputConfiguration.writeOut(`${str}
|
|
2972
|
+
`);
|
|
2973
|
+
this._exit(0, "commander.version", str);
|
|
2974
|
+
});
|
|
2975
|
+
return this;
|
|
2976
|
+
}
|
|
2977
|
+
/**
|
|
2978
|
+
* Set the description.
|
|
2979
|
+
*
|
|
2980
|
+
* @param {string} [str]
|
|
2981
|
+
* @param {object} [argsDescription]
|
|
2982
|
+
* @return {(string|Command)}
|
|
2983
|
+
*/
|
|
2984
|
+
description(str, argsDescription) {
|
|
2985
|
+
if (str === void 0 && argsDescription === void 0)
|
|
2986
|
+
return this._description;
|
|
2987
|
+
this._description = str;
|
|
2988
|
+
if (argsDescription) {
|
|
2989
|
+
this._argsDescription = argsDescription;
|
|
2990
|
+
}
|
|
2991
|
+
return this;
|
|
2992
|
+
}
|
|
2993
|
+
/**
|
|
2994
|
+
* Set the summary. Used when listed as subcommand of parent.
|
|
2995
|
+
*
|
|
2996
|
+
* @param {string} [str]
|
|
2997
|
+
* @return {(string|Command)}
|
|
2998
|
+
*/
|
|
2999
|
+
summary(str) {
|
|
3000
|
+
if (str === void 0) return this._summary;
|
|
3001
|
+
this._summary = str;
|
|
3002
|
+
return this;
|
|
3003
|
+
}
|
|
3004
|
+
/**
|
|
3005
|
+
* Set an alias for the command.
|
|
3006
|
+
*
|
|
3007
|
+
* You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.
|
|
3008
|
+
*
|
|
3009
|
+
* @param {string} [alias]
|
|
3010
|
+
* @return {(string|Command)}
|
|
3011
|
+
*/
|
|
3012
|
+
alias(alias) {
|
|
3013
|
+
if (alias === void 0) return this._aliases[0];
|
|
3014
|
+
let command2 = this;
|
|
3015
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
3016
|
+
command2 = this.commands[this.commands.length - 1];
|
|
3017
|
+
}
|
|
3018
|
+
if (alias === command2._name)
|
|
3019
|
+
throw new Error("Command alias can't be the same as its name");
|
|
3020
|
+
const matchingCommand = this.parent?._findCommand(alias);
|
|
3021
|
+
if (matchingCommand) {
|
|
3022
|
+
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
3023
|
+
throw new Error(
|
|
3024
|
+
`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`
|
|
3025
|
+
);
|
|
3026
|
+
}
|
|
3027
|
+
command2._aliases.push(alias);
|
|
3028
|
+
return this;
|
|
3029
|
+
}
|
|
3030
|
+
/**
|
|
3031
|
+
* Set aliases for the command.
|
|
3032
|
+
*
|
|
3033
|
+
* Only the first alias is shown in the auto-generated help.
|
|
3034
|
+
*
|
|
3035
|
+
* @param {string[]} [aliases]
|
|
3036
|
+
* @return {(string[]|Command)}
|
|
3037
|
+
*/
|
|
3038
|
+
aliases(aliases) {
|
|
3039
|
+
if (aliases === void 0) return this._aliases;
|
|
3040
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
3041
|
+
return this;
|
|
3042
|
+
}
|
|
3043
|
+
/**
|
|
3044
|
+
* Set / get the command usage `str`.
|
|
3045
|
+
*
|
|
3046
|
+
* @param {string} [str]
|
|
3047
|
+
* @return {(string|Command)}
|
|
3048
|
+
*/
|
|
3049
|
+
usage(str) {
|
|
3050
|
+
if (str === void 0) {
|
|
3051
|
+
if (this._usage) return this._usage;
|
|
3052
|
+
const args = this.registeredArguments.map((arg) => {
|
|
3053
|
+
return humanReadableArgName(arg);
|
|
3054
|
+
});
|
|
3055
|
+
return [].concat(
|
|
3056
|
+
this.options.length || this._helpOption !== null ? "[options]" : [],
|
|
3057
|
+
this.commands.length ? "[command]" : [],
|
|
3058
|
+
this.registeredArguments.length ? args : []
|
|
3059
|
+
).join(" ");
|
|
3060
|
+
}
|
|
3061
|
+
this._usage = str;
|
|
3062
|
+
return this;
|
|
3063
|
+
}
|
|
3064
|
+
/**
|
|
3065
|
+
* Get or set the name of the command.
|
|
3066
|
+
*
|
|
3067
|
+
* @param {string} [str]
|
|
3068
|
+
* @return {(string|Command)}
|
|
3069
|
+
*/
|
|
3070
|
+
name(str) {
|
|
3071
|
+
if (str === void 0) return this._name;
|
|
3072
|
+
this._name = str;
|
|
3073
|
+
return this;
|
|
3074
|
+
}
|
|
3075
|
+
/**
|
|
3076
|
+
* Set/get the help group heading for this subcommand in parent command's help.
|
|
3077
|
+
*
|
|
3078
|
+
* @param {string} [heading]
|
|
3079
|
+
* @return {Command | string}
|
|
3080
|
+
*/
|
|
3081
|
+
helpGroup(heading) {
|
|
3082
|
+
if (heading === void 0) return this._helpGroupHeading ?? "";
|
|
3083
|
+
this._helpGroupHeading = heading;
|
|
3084
|
+
return this;
|
|
3085
|
+
}
|
|
3086
|
+
/**
|
|
3087
|
+
* Set/get the default help group heading for subcommands added to this command.
|
|
3088
|
+
* (This does not override a group set directly on the subcommand using .helpGroup().)
|
|
3089
|
+
*
|
|
3090
|
+
* @example
|
|
3091
|
+
* program.commandsGroup('Development Commands:);
|
|
3092
|
+
* program.command('watch')...
|
|
3093
|
+
* program.command('lint')...
|
|
3094
|
+
* ...
|
|
3095
|
+
*
|
|
3096
|
+
* @param {string} [heading]
|
|
3097
|
+
* @returns {Command | string}
|
|
3098
|
+
*/
|
|
3099
|
+
commandsGroup(heading) {
|
|
3100
|
+
if (heading === void 0) return this._defaultCommandGroup ?? "";
|
|
3101
|
+
this._defaultCommandGroup = heading;
|
|
3102
|
+
return this;
|
|
3103
|
+
}
|
|
3104
|
+
/**
|
|
3105
|
+
* Set/get the default help group heading for options added to this command.
|
|
3106
|
+
* (This does not override a group set directly on the option using .helpGroup().)
|
|
3107
|
+
*
|
|
3108
|
+
* @example
|
|
3109
|
+
* program
|
|
3110
|
+
* .optionsGroup('Development Options:')
|
|
3111
|
+
* .option('-d, --debug', 'output extra debugging')
|
|
3112
|
+
* .option('-p, --profile', 'output profiling information')
|
|
3113
|
+
*
|
|
3114
|
+
* @param {string} [heading]
|
|
3115
|
+
* @returns {Command | string}
|
|
3116
|
+
*/
|
|
3117
|
+
optionsGroup(heading) {
|
|
3118
|
+
if (heading === void 0) return this._defaultOptionGroup ?? "";
|
|
3119
|
+
this._defaultOptionGroup = heading;
|
|
3120
|
+
return this;
|
|
3121
|
+
}
|
|
3122
|
+
/**
|
|
3123
|
+
* @param {Option} option
|
|
3124
|
+
* @private
|
|
3125
|
+
*/
|
|
3126
|
+
_initOptionGroup(option2) {
|
|
3127
|
+
if (this._defaultOptionGroup && !option2.helpGroupHeading)
|
|
3128
|
+
option2.helpGroup(this._defaultOptionGroup);
|
|
3129
|
+
}
|
|
3130
|
+
/**
|
|
3131
|
+
* @param {Command} cmd
|
|
3132
|
+
* @private
|
|
3133
|
+
*/
|
|
3134
|
+
_initCommandGroup(cmd) {
|
|
3135
|
+
if (this._defaultCommandGroup && !cmd.helpGroup())
|
|
3136
|
+
cmd.helpGroup(this._defaultCommandGroup);
|
|
3137
|
+
}
|
|
3138
|
+
/**
|
|
3139
|
+
* Set the name of the command from script filename, such as process.argv[1],
|
|
3140
|
+
* or require.main.filename, or __filename.
|
|
3141
|
+
*
|
|
3142
|
+
* (Used internally and public although not documented in README.)
|
|
3143
|
+
*
|
|
3144
|
+
* @example
|
|
3145
|
+
* program.nameFromFilename(require.main.filename);
|
|
3146
|
+
*
|
|
3147
|
+
* @param {string} filename
|
|
3148
|
+
* @return {Command}
|
|
3149
|
+
*/
|
|
3150
|
+
nameFromFilename(filename) {
|
|
3151
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
3152
|
+
return this;
|
|
3153
|
+
}
|
|
3154
|
+
/**
|
|
3155
|
+
* Get or set the directory for searching for executable subcommands of this command.
|
|
3156
|
+
*
|
|
3157
|
+
* @example
|
|
3158
|
+
* program.executableDir(__dirname);
|
|
3159
|
+
* // or
|
|
3160
|
+
* program.executableDir('subcommands');
|
|
3161
|
+
*
|
|
3162
|
+
* @param {string} [path]
|
|
3163
|
+
* @return {(string|null|Command)}
|
|
3164
|
+
*/
|
|
3165
|
+
executableDir(path2) {
|
|
3166
|
+
if (path2 === void 0) return this._executableDir;
|
|
3167
|
+
this._executableDir = path2;
|
|
3168
|
+
return this;
|
|
3169
|
+
}
|
|
3170
|
+
/**
|
|
3171
|
+
* Return program help documentation.
|
|
3172
|
+
*
|
|
3173
|
+
* @param {{ error: boolean }} [contextOptions] - pass {error:true} to wrap for stderr instead of stdout
|
|
3174
|
+
* @return {string}
|
|
3175
|
+
*/
|
|
3176
|
+
helpInformation(contextOptions) {
|
|
3177
|
+
const helper = this.createHelp();
|
|
3178
|
+
const context = this._getOutputContext(contextOptions);
|
|
3179
|
+
helper.prepareContext({
|
|
3180
|
+
error: context.error,
|
|
3181
|
+
helpWidth: context.helpWidth,
|
|
3182
|
+
outputHasColors: context.hasColors
|
|
3183
|
+
});
|
|
3184
|
+
const text = helper.formatHelp(this, helper);
|
|
3185
|
+
if (context.hasColors) return text;
|
|
3186
|
+
return this._outputConfiguration.stripColor(text);
|
|
3187
|
+
}
|
|
3188
|
+
/**
|
|
3189
|
+
* @typedef HelpContext
|
|
3190
|
+
* @type {object}
|
|
3191
|
+
* @property {boolean} error
|
|
3192
|
+
* @property {number} helpWidth
|
|
3193
|
+
* @property {boolean} hasColors
|
|
3194
|
+
* @property {function} write - includes stripColor if needed
|
|
3195
|
+
*
|
|
3196
|
+
* @returns {HelpContext}
|
|
3197
|
+
* @private
|
|
3198
|
+
*/
|
|
3199
|
+
_getOutputContext(contextOptions) {
|
|
3200
|
+
contextOptions = contextOptions || {};
|
|
3201
|
+
const error2 = !!contextOptions.error;
|
|
3202
|
+
let baseWrite;
|
|
3203
|
+
let hasColors;
|
|
3204
|
+
let helpWidth;
|
|
3205
|
+
if (error2) {
|
|
3206
|
+
baseWrite = (str) => this._outputConfiguration.writeErr(str);
|
|
3207
|
+
hasColors = this._outputConfiguration.getErrHasColors();
|
|
3208
|
+
helpWidth = this._outputConfiguration.getErrHelpWidth();
|
|
3209
|
+
} else {
|
|
3210
|
+
baseWrite = (str) => this._outputConfiguration.writeOut(str);
|
|
3211
|
+
hasColors = this._outputConfiguration.getOutHasColors();
|
|
3212
|
+
helpWidth = this._outputConfiguration.getOutHelpWidth();
|
|
3213
|
+
}
|
|
3214
|
+
const write = (str) => {
|
|
3215
|
+
if (!hasColors) str = this._outputConfiguration.stripColor(str);
|
|
3216
|
+
return baseWrite(str);
|
|
3217
|
+
};
|
|
3218
|
+
return { error: error2, write, hasColors, helpWidth };
|
|
3219
|
+
}
|
|
3220
|
+
/**
|
|
3221
|
+
* Output help information for this command.
|
|
3222
|
+
*
|
|
3223
|
+
* Outputs built-in help, and custom text added using `.addHelpText()`.
|
|
3224
|
+
*
|
|
3225
|
+
* @param {{ error: boolean } | Function} [contextOptions] - pass {error:true} to write to stderr instead of stdout
|
|
3226
|
+
*/
|
|
3227
|
+
outputHelp(contextOptions) {
|
|
3228
|
+
let deprecatedCallback;
|
|
3229
|
+
if (typeof contextOptions === "function") {
|
|
3230
|
+
deprecatedCallback = contextOptions;
|
|
3231
|
+
contextOptions = void 0;
|
|
3232
|
+
}
|
|
3233
|
+
const outputContext = this._getOutputContext(contextOptions);
|
|
3234
|
+
const eventContext = {
|
|
3235
|
+
error: outputContext.error,
|
|
3236
|
+
write: outputContext.write,
|
|
3237
|
+
command: this
|
|
3238
|
+
};
|
|
3239
|
+
this._getCommandAndAncestors().reverse().forEach((command2) => command2.emit("beforeAllHelp", eventContext));
|
|
3240
|
+
this.emit("beforeHelp", eventContext);
|
|
3241
|
+
let helpInformation = this.helpInformation({ error: outputContext.error });
|
|
3242
|
+
if (deprecatedCallback) {
|
|
3243
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
3244
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
3245
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
3246
|
+
}
|
|
3247
|
+
}
|
|
3248
|
+
outputContext.write(helpInformation);
|
|
3249
|
+
if (this._getHelpOption()?.long) {
|
|
3250
|
+
this.emit(this._getHelpOption().long);
|
|
3251
|
+
}
|
|
3252
|
+
this.emit("afterHelp", eventContext);
|
|
3253
|
+
this._getCommandAndAncestors().forEach(
|
|
3254
|
+
(command2) => command2.emit("afterAllHelp", eventContext)
|
|
3255
|
+
);
|
|
3256
|
+
}
|
|
3257
|
+
/**
|
|
3258
|
+
* You can pass in flags and a description to customise the built-in help option.
|
|
3259
|
+
* Pass in false to disable the built-in help option.
|
|
3260
|
+
*
|
|
3261
|
+
* @example
|
|
3262
|
+
* program.helpOption('-?, --help' 'show help'); // customise
|
|
3263
|
+
* program.helpOption(false); // disable
|
|
3264
|
+
*
|
|
3265
|
+
* @param {(string | boolean)} flags
|
|
3266
|
+
* @param {string} [description]
|
|
3267
|
+
* @return {Command} `this` command for chaining
|
|
3268
|
+
*/
|
|
3269
|
+
helpOption(flags, description) {
|
|
3270
|
+
if (typeof flags === "boolean") {
|
|
3271
|
+
if (flags) {
|
|
3272
|
+
if (this._helpOption === null) this._helpOption = void 0;
|
|
3273
|
+
if (this._defaultOptionGroup) {
|
|
3274
|
+
this._initOptionGroup(this._getHelpOption());
|
|
3275
|
+
}
|
|
3276
|
+
} else {
|
|
3277
|
+
this._helpOption = null;
|
|
3278
|
+
}
|
|
3279
|
+
return this;
|
|
3280
|
+
}
|
|
3281
|
+
this._helpOption = this.createOption(
|
|
3282
|
+
flags ?? "-h, --help",
|
|
3283
|
+
description ?? "display help for command"
|
|
3284
|
+
);
|
|
3285
|
+
if (flags || description) this._initOptionGroup(this._helpOption);
|
|
3286
|
+
return this;
|
|
3287
|
+
}
|
|
3288
|
+
/**
|
|
3289
|
+
* Lazy create help option.
|
|
3290
|
+
* Returns null if has been disabled with .helpOption(false).
|
|
3291
|
+
*
|
|
3292
|
+
* @returns {(Option | null)} the help option
|
|
3293
|
+
* @package
|
|
3294
|
+
*/
|
|
3295
|
+
_getHelpOption() {
|
|
3296
|
+
if (this._helpOption === void 0) {
|
|
3297
|
+
this.helpOption(void 0, void 0);
|
|
3298
|
+
}
|
|
3299
|
+
return this._helpOption;
|
|
3300
|
+
}
|
|
3301
|
+
/**
|
|
3302
|
+
* Supply your own option to use for the built-in help option.
|
|
3303
|
+
* This is an alternative to using helpOption() to customise the flags and description etc.
|
|
3304
|
+
*
|
|
3305
|
+
* @param {Option} option
|
|
3306
|
+
* @return {Command} `this` command for chaining
|
|
3307
|
+
*/
|
|
3308
|
+
addHelpOption(option2) {
|
|
3309
|
+
this._helpOption = option2;
|
|
3310
|
+
this._initOptionGroup(option2);
|
|
3311
|
+
return this;
|
|
3312
|
+
}
|
|
3313
|
+
/**
|
|
3314
|
+
* Output help information and exit.
|
|
3315
|
+
*
|
|
3316
|
+
* Outputs built-in help, and custom text added using `.addHelpText()`.
|
|
3317
|
+
*
|
|
3318
|
+
* @param {{ error: boolean }} [contextOptions] - pass {error:true} to write to stderr instead of stdout
|
|
3319
|
+
*/
|
|
3320
|
+
help(contextOptions) {
|
|
3321
|
+
this.outputHelp(contextOptions);
|
|
3322
|
+
let exitCode = Number(process2.exitCode ?? 0);
|
|
3323
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
3324
|
+
exitCode = 1;
|
|
3325
|
+
}
|
|
3326
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
3327
|
+
}
|
|
3328
|
+
/**
|
|
3329
|
+
* // Do a little typing to coordinate emit and listener for the help text events.
|
|
3330
|
+
* @typedef HelpTextEventContext
|
|
3331
|
+
* @type {object}
|
|
3332
|
+
* @property {boolean} error
|
|
3333
|
+
* @property {Command} command
|
|
3334
|
+
* @property {function} write
|
|
3335
|
+
*/
|
|
3336
|
+
/**
|
|
3337
|
+
* Add additional text to be displayed with the built-in help.
|
|
3338
|
+
*
|
|
3339
|
+
* Position is 'before' or 'after' to affect just this command,
|
|
3340
|
+
* and 'beforeAll' or 'afterAll' to affect this command and all its subcommands.
|
|
3341
|
+
*
|
|
3342
|
+
* @param {string} position - before or after built-in help
|
|
3343
|
+
* @param {(string | Function)} text - string to add, or a function returning a string
|
|
3344
|
+
* @return {Command} `this` command for chaining
|
|
3345
|
+
*/
|
|
3346
|
+
addHelpText(position, text) {
|
|
3347
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
3348
|
+
if (!allowedValues.includes(position)) {
|
|
3349
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
3350
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
3351
|
+
}
|
|
3352
|
+
const helpEvent = `${position}Help`;
|
|
3353
|
+
this.on(helpEvent, (context) => {
|
|
3354
|
+
let helpStr;
|
|
3355
|
+
if (typeof text === "function") {
|
|
3356
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
3357
|
+
} else {
|
|
3358
|
+
helpStr = text;
|
|
3359
|
+
}
|
|
3360
|
+
if (helpStr) {
|
|
3361
|
+
context.write(`${helpStr}
|
|
3362
|
+
`);
|
|
3363
|
+
}
|
|
3364
|
+
});
|
|
3365
|
+
return this;
|
|
3366
|
+
}
|
|
3367
|
+
/**
|
|
3368
|
+
* Output help information if help flags specified
|
|
3369
|
+
*
|
|
3370
|
+
* @param {Array} args - array of options to search for help flags
|
|
3371
|
+
* @private
|
|
3372
|
+
*/
|
|
3373
|
+
_outputHelpIfRequested(args) {
|
|
3374
|
+
const helpOption = this._getHelpOption();
|
|
3375
|
+
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
3376
|
+
if (helpRequested) {
|
|
3377
|
+
this.outputHelp();
|
|
3378
|
+
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
3379
|
+
}
|
|
3380
|
+
}
|
|
3381
|
+
}
|
|
3382
|
+
function incrementNodeInspectorPort(args) {
|
|
3383
|
+
return args.map((arg) => {
|
|
3384
|
+
if (!arg.startsWith("--inspect")) {
|
|
3385
|
+
return arg;
|
|
3386
|
+
}
|
|
3387
|
+
let debugOption;
|
|
3388
|
+
let debugHost = "127.0.0.1";
|
|
3389
|
+
let debugPort = "9229";
|
|
3390
|
+
let match;
|
|
3391
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
3392
|
+
debugOption = match[1];
|
|
3393
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
3394
|
+
debugOption = match[1];
|
|
3395
|
+
if (/^\d+$/.test(match[3])) {
|
|
3396
|
+
debugPort = match[3];
|
|
3397
|
+
} else {
|
|
3398
|
+
debugHost = match[3];
|
|
3399
|
+
}
|
|
3400
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
3401
|
+
debugOption = match[1];
|
|
3402
|
+
debugHost = match[3];
|
|
3403
|
+
debugPort = match[4];
|
|
3404
|
+
}
|
|
3405
|
+
if (debugOption && debugPort !== "0") {
|
|
3406
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
3407
|
+
}
|
|
3408
|
+
return arg;
|
|
3409
|
+
});
|
|
3410
|
+
}
|
|
3411
|
+
function useColor() {
|
|
3412
|
+
if (process2.env.NO_COLOR || process2.env.FORCE_COLOR === "0" || process2.env.FORCE_COLOR === "false")
|
|
3413
|
+
return false;
|
|
3414
|
+
if (process2.env.FORCE_COLOR || process2.env.CLICOLOR_FORCE !== void 0)
|
|
3415
|
+
return true;
|
|
3416
|
+
return void 0;
|
|
3417
|
+
}
|
|
3418
|
+
command.Command = Command2;
|
|
3419
|
+
command.useColor = useColor;
|
|
3420
|
+
return command;
|
|
3421
|
+
}
|
|
3422
|
+
var hasRequiredCommander;
|
|
3423
|
+
function requireCommander() {
|
|
3424
|
+
if (hasRequiredCommander) return commander$1;
|
|
3425
|
+
hasRequiredCommander = 1;
|
|
3426
|
+
const { Argument: Argument2 } = requireArgument();
|
|
3427
|
+
const { Command: Command2 } = requireCommand();
|
|
3428
|
+
const { CommanderError: CommanderError2, InvalidArgumentError: InvalidArgumentError2 } = requireError();
|
|
3429
|
+
const { Help: Help2 } = requireHelp();
|
|
3430
|
+
const { Option: Option2 } = requireOption();
|
|
3431
|
+
commander$1.program = new Command2();
|
|
3432
|
+
commander$1.createCommand = (name) => new Command2(name);
|
|
3433
|
+
commander$1.createOption = (flags, description) => new Option2(flags, description);
|
|
3434
|
+
commander$1.createArgument = (name, description) => new Argument2(name, description);
|
|
3435
|
+
commander$1.Command = Command2;
|
|
3436
|
+
commander$1.Option = Option2;
|
|
3437
|
+
commander$1.Argument = Argument2;
|
|
3438
|
+
commander$1.Help = Help2;
|
|
3439
|
+
commander$1.CommanderError = CommanderError2;
|
|
3440
|
+
commander$1.InvalidArgumentError = InvalidArgumentError2;
|
|
3441
|
+
commander$1.InvalidOptionArgumentError = InvalidArgumentError2;
|
|
3442
|
+
return commander$1;
|
|
3443
|
+
}
|
|
3444
|
+
var commanderExports = requireCommander();
|
|
3445
|
+
const commander = /* @__PURE__ */ getDefaultExportFromCjs(commanderExports);
|
|
3446
|
+
const {
|
|
3447
|
+
program,
|
|
3448
|
+
createCommand,
|
|
3449
|
+
createArgument,
|
|
3450
|
+
createOption,
|
|
3451
|
+
CommanderError,
|
|
3452
|
+
InvalidArgumentError,
|
|
3453
|
+
InvalidOptionArgumentError,
|
|
3454
|
+
// deprecated old name
|
|
3455
|
+
Command,
|
|
3456
|
+
Argument,
|
|
3457
|
+
Option,
|
|
3458
|
+
Help
|
|
3459
|
+
} = commander;
|
|
3460
|
+
program.description("Encrypts an EFI binary using a hash derived from user‑defined data (smbios, disk, ...)").showHelpAfterError(true).option("-c, --config-file <configFile>", "configuration file").option("-i, --input-file <inputFile>", "path to the input efi file to embed").option("-o, --output-file <outputFile>", "path to the output efi file to write").option("-s, --smbios <smbios>", "path to the input smbios dump file").option("-b, --build-folder <buildFolder>", "folder where to build the code").option("--skip-gen-code", "skip generating code").option("--skip-extract", "skip extracting source code").option("--skip-make", "skip calling make").action(async (options) => {
|
|
3461
|
+
try {
|
|
3462
|
+
let config = {};
|
|
3463
|
+
if (options.configFile) {
|
|
3464
|
+
const require2 = createRequire(join(process.cwd(), "file"));
|
|
3465
|
+
const module = require2(resolve(options.configFile));
|
|
3466
|
+
config = "default" in module ? config = module.default : module;
|
|
3467
|
+
delete options.configFile;
|
|
3468
|
+
}
|
|
3469
|
+
Object.assign(config, options);
|
|
3470
|
+
await build(config);
|
|
3471
|
+
} catch (error2) {
|
|
3472
|
+
program.error(`${error2}`);
|
|
3473
|
+
}
|
|
3474
|
+
}).parse();
|