@bemoje/cli 1.0.2 → 1.0.4
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/cjs/index.cjs +31 -0
- package/cjs/lib/Command.cjs +453 -0
- package/cjs/lib/CommandHelpAdapter.cjs +130 -0
- package/cjs/lib/CommanderHelpAdapter.cjs +120 -0
- package/cjs/lib/Help.cjs +490 -0
- package/cjs/lib/internal/lazyProp.cjs +41 -0
- package/cjs/lib/renderHelp.cjs +33 -0
- package/{index.js → esm/index.mjs} +1 -1
- package/{lib/Command.js → esm/lib/Command.mjs} +29 -35
- package/{lib/CommandHelpAdapter.js → esm/lib/CommandHelpAdapter.mjs} +1 -3
- package/{lib/CommanderHelpAdapter.js → esm/lib/CommanderHelpAdapter.mjs} +1 -3
- package/{lib/Help.js → esm/lib/Help.mjs} +11 -22
- package/{lib/internal/lazyProp.js → esm/lib/internal/lazyProp.mjs} +1 -4
- package/{lib/renderHelp.js → esm/lib/renderHelp.mjs} +1 -4
- package/package.json +4 -10
- /package/{index.d.ts → typings/index.d.ts} +0 -0
- /package/{lib → typings/lib}/Command.d.ts +0 -0
- /package/{lib → typings/lib}/CommandHelpAdapter.d.ts +0 -0
- /package/{lib → typings/lib}/CommanderHelpAdapter.d.ts +0 -0
- /package/{lib → typings/lib}/Help.d.ts +0 -0
- /package/{lib → typings/lib}/internal/lazyProp.d.ts +0 -0
- /package/{lib → typings/lib}/renderHelp.d.ts +0 -0
package/cjs/index.cjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var src_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(src_exports);
|
|
18
|
+
__reExport(src_exports, require("./lib/Command"), module.exports);
|
|
19
|
+
__reExport(src_exports, require("./lib/CommandHelpAdapter"), module.exports);
|
|
20
|
+
__reExport(src_exports, require("./lib/CommanderHelpAdapter"), module.exports);
|
|
21
|
+
__reExport(src_exports, require("./lib/Help"), module.exports);
|
|
22
|
+
__reExport(src_exports, require("./lib/renderHelp"), module.exports);
|
|
23
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
24
|
+
0 && (module.exports = {
|
|
25
|
+
...require("./lib/Command"),
|
|
26
|
+
...require("./lib/CommandHelpAdapter"),
|
|
27
|
+
...require("./lib/CommanderHelpAdapter"),
|
|
28
|
+
...require("./lib/Help"),
|
|
29
|
+
...require("./lib/renderHelp")
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var Command_exports = {};
|
|
20
|
+
__export(Command_exports, {
|
|
21
|
+
Command: () => Command
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(Command_exports);
|
|
24
|
+
var import_node_util = require("node:util");
|
|
25
|
+
var import_CommandHelpAdapter = require("./CommandHelpAdapter");
|
|
26
|
+
class Command {
|
|
27
|
+
/** Command name used for invocation */
|
|
28
|
+
name;
|
|
29
|
+
/** Optional version string */
|
|
30
|
+
version;
|
|
31
|
+
/** Alternative names for this command */
|
|
32
|
+
aliases;
|
|
33
|
+
/** Brief single-line description */
|
|
34
|
+
summary;
|
|
35
|
+
/** Full command description */
|
|
36
|
+
description;
|
|
37
|
+
/** Whether command should be hidden from help */
|
|
38
|
+
hidden;
|
|
39
|
+
/** Group name for organizing commands in help */
|
|
40
|
+
group;
|
|
41
|
+
/** Parent command if this is a subcommand */
|
|
42
|
+
parent;
|
|
43
|
+
/** Child subcommands */
|
|
44
|
+
commands;
|
|
45
|
+
/** Positional arguments */
|
|
46
|
+
arguments;
|
|
47
|
+
/** Named options/flags */
|
|
48
|
+
options;
|
|
49
|
+
/** Help system configuration */
|
|
50
|
+
helpConfiguration;
|
|
51
|
+
constructor(name = "", parent = null) {
|
|
52
|
+
this.name = name;
|
|
53
|
+
this.parent = parent;
|
|
54
|
+
this.aliases = [];
|
|
55
|
+
this.description = "";
|
|
56
|
+
this.commands = [];
|
|
57
|
+
this.arguments = [];
|
|
58
|
+
this.options = [];
|
|
59
|
+
this.helpConfiguration = { showGlobalOptions: true, sortOptions: true, sortSubcommands: true };
|
|
60
|
+
Object.defineProperty(this, "parent", { enumerable: false });
|
|
61
|
+
}
|
|
62
|
+
/** Updates multiple command properties at once */
|
|
63
|
+
setState(state) {
|
|
64
|
+
Object.assign(this, state);
|
|
65
|
+
if (state.commands) {
|
|
66
|
+
state.commands = state.commands.map((cmd) => new Command(cmd.name, this).setState(cmd));
|
|
67
|
+
}
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
/** Serializes command to JSON, maintaining compatibility with previous state-based structure */
|
|
71
|
+
toJSON() {
|
|
72
|
+
const result = {
|
|
73
|
+
name: this.name,
|
|
74
|
+
version: this.version,
|
|
75
|
+
aliases: this.aliases,
|
|
76
|
+
summary: this.summary,
|
|
77
|
+
description: this.description,
|
|
78
|
+
hidden: this.hidden,
|
|
79
|
+
group: this.group,
|
|
80
|
+
commands: this.commands,
|
|
81
|
+
arguments: this.arguments,
|
|
82
|
+
options: this.options,
|
|
83
|
+
helpConfiguration: this.helpConfiguration
|
|
84
|
+
};
|
|
85
|
+
Object.defineProperty(result, "parent", {
|
|
86
|
+
value: this.parent,
|
|
87
|
+
writable: true,
|
|
88
|
+
enumerable: false,
|
|
89
|
+
configurable: true
|
|
90
|
+
});
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
/** Sets the command name */
|
|
94
|
+
setName(name) {
|
|
95
|
+
this.name = name;
|
|
96
|
+
}
|
|
97
|
+
/** Sets command aliases, flattening nested arrays */
|
|
98
|
+
setAliases(...aliases) {
|
|
99
|
+
this.aliases = aliases.flat();
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
/** Adds aliases to existing ones */
|
|
103
|
+
addAliases(...aliases) {
|
|
104
|
+
this.aliases.push(...aliases.flat());
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
/** Sets the command version */
|
|
108
|
+
setVersion(version) {
|
|
109
|
+
this.version = version;
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
/** Sets the command summary */
|
|
113
|
+
setSummary(summary) {
|
|
114
|
+
this.summary = summary;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
/** Sets command description, joining multiple lines */
|
|
118
|
+
setDescription(...lines) {
|
|
119
|
+
this.description = lines.join("\n");
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
/** Sets whether command is hidden from help */
|
|
123
|
+
setHidden(hidden = true) {
|
|
124
|
+
this.hidden = hidden;
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
/** Sets the command group for help organization */
|
|
128
|
+
setGroup(group) {
|
|
129
|
+
this.group = group;
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
/** Sets the parent command */
|
|
133
|
+
setParent(parent) {
|
|
134
|
+
this.parent = parent;
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/** Extends existing help configuration with new settings */
|
|
138
|
+
extendHelpConfiguration(config) {
|
|
139
|
+
this.helpConfiguration = { ...this.helpConfiguration, ...config };
|
|
140
|
+
return this;
|
|
141
|
+
}
|
|
142
|
+
/** Sets help configuration, using defaults if not provided */
|
|
143
|
+
setHelpConfiguration(config) {
|
|
144
|
+
this.helpConfiguration = config ? { ...config } : { showGlobalOptions: true, sortOptions: true, sortSubcommands: true };
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
/** Creates and adds a subcommand */
|
|
148
|
+
subcommand(name) {
|
|
149
|
+
const sub = new Command(name, this);
|
|
150
|
+
this.commands.push(sub);
|
|
151
|
+
return sub;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Adds positional argument with type inference and CLI ordering validation.
|
|
155
|
+
*/
|
|
156
|
+
argument(usage, description, options = {}) {
|
|
157
|
+
const match = usage.match(/^<(.*?)>$|^\[(.*?)\]$/);
|
|
158
|
+
if (!match) throw new Error(`Invalid argument format: ${usage}`);
|
|
159
|
+
const nameMatch = match[1] || match[2];
|
|
160
|
+
const name = nameMatch.replace(/\.\.\.$/, "");
|
|
161
|
+
this.assertArgumentNameNotInUse(name);
|
|
162
|
+
if (usage.startsWith("<")) {
|
|
163
|
+
if (nameMatch.endsWith("...")) {
|
|
164
|
+
this.assertNoMultipleVariadicArguments();
|
|
165
|
+
this.arguments.push({
|
|
166
|
+
name,
|
|
167
|
+
description,
|
|
168
|
+
required: true,
|
|
169
|
+
multiple: true,
|
|
170
|
+
...options
|
|
171
|
+
});
|
|
172
|
+
} else {
|
|
173
|
+
this.assertNoOptionalOrVariadicArguments();
|
|
174
|
+
this.arguments.push({
|
|
175
|
+
name,
|
|
176
|
+
description,
|
|
177
|
+
required: true,
|
|
178
|
+
multiple: false,
|
|
179
|
+
...options
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
} else if (usage.startsWith("[")) {
|
|
183
|
+
if (nameMatch.endsWith("...")) {
|
|
184
|
+
this.assertNoMultipleVariadicArguments();
|
|
185
|
+
this.arguments.push({
|
|
186
|
+
name,
|
|
187
|
+
description,
|
|
188
|
+
required: false,
|
|
189
|
+
multiple: true,
|
|
190
|
+
defaultValue: options.defaultValue ?? [],
|
|
191
|
+
...options
|
|
192
|
+
});
|
|
193
|
+
} else {
|
|
194
|
+
this.assertNoVariadicArgument();
|
|
195
|
+
this.arguments.push({
|
|
196
|
+
name,
|
|
197
|
+
description,
|
|
198
|
+
required: false,
|
|
199
|
+
multiple: false,
|
|
200
|
+
...options
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Adds command-line option with type inference. Parses format: `-s, --long [<value>|[value]|<value...>|[value...]]`
|
|
208
|
+
*/
|
|
209
|
+
option(usage, description, opts = {}) {
|
|
210
|
+
const match = usage.match(/^-(.+?), --([a-zA-Z][\w-]*)(?:\s*(<(.+?)>|\[(.+?)\]))?$/);
|
|
211
|
+
if (!match) throw new Error(`Invalid option format: ${usage}`);
|
|
212
|
+
const short = match[1];
|
|
213
|
+
this.assertOptionShortNameIsValid(short);
|
|
214
|
+
this.assertOptionShortNameNotInUse(short);
|
|
215
|
+
const name = match[2];
|
|
216
|
+
const argName = (match[4] || match[5])?.replace(/\.\.\.$/, "");
|
|
217
|
+
this.assertOptionNameNotInUse(name);
|
|
218
|
+
if (!argName) {
|
|
219
|
+
this.options.push({
|
|
220
|
+
type: "boolean",
|
|
221
|
+
short,
|
|
222
|
+
name,
|
|
223
|
+
description,
|
|
224
|
+
required: false,
|
|
225
|
+
multiple: false,
|
|
226
|
+
...opts
|
|
227
|
+
});
|
|
228
|
+
} else if (usage.endsWith(">")) {
|
|
229
|
+
if (usage.endsWith("...>")) {
|
|
230
|
+
this.options.push({
|
|
231
|
+
type: "string",
|
|
232
|
+
short,
|
|
233
|
+
name,
|
|
234
|
+
argName,
|
|
235
|
+
description,
|
|
236
|
+
required: true,
|
|
237
|
+
multiple: true,
|
|
238
|
+
...opts
|
|
239
|
+
});
|
|
240
|
+
} else {
|
|
241
|
+
this.options.push({
|
|
242
|
+
type: "string",
|
|
243
|
+
short,
|
|
244
|
+
name,
|
|
245
|
+
argName,
|
|
246
|
+
description,
|
|
247
|
+
required: true,
|
|
248
|
+
multiple: false,
|
|
249
|
+
...opts
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
} else if (usage.endsWith("]")) {
|
|
253
|
+
if (usage.endsWith("...]")) {
|
|
254
|
+
this.options.push({
|
|
255
|
+
type: "string",
|
|
256
|
+
short,
|
|
257
|
+
name,
|
|
258
|
+
argName,
|
|
259
|
+
description,
|
|
260
|
+
required: false,
|
|
261
|
+
multiple: true,
|
|
262
|
+
defaultValue: opts.defaultValue ?? [],
|
|
263
|
+
...opts
|
|
264
|
+
});
|
|
265
|
+
} else {
|
|
266
|
+
this.options.push({
|
|
267
|
+
type: "string",
|
|
268
|
+
short,
|
|
269
|
+
name,
|
|
270
|
+
argName,
|
|
271
|
+
description,
|
|
272
|
+
required: false,
|
|
273
|
+
multiple: false,
|
|
274
|
+
...opts
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return this;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Parses command-line arguments with subcommand support and type-safe validation.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```typescript
|
|
285
|
+
* const result = cmd.parse(['input.txt', '-v', '--format', 'json'])
|
|
286
|
+
* // { arguments: ['input.txt'], options: { verbose: true, format: 'json' } }
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
parse(argv = process.argv.slice(2), globalOptions = []) {
|
|
290
|
+
const maybeSubArg = (0, import_node_util.parseArgs)({
|
|
291
|
+
args: argv,
|
|
292
|
+
allowPositionals: true,
|
|
293
|
+
tokens: false,
|
|
294
|
+
strict: false,
|
|
295
|
+
allowNegative: true
|
|
296
|
+
}).positionals[0];
|
|
297
|
+
const sub = this.findCommand(maybeSubArg);
|
|
298
|
+
if (sub) {
|
|
299
|
+
return sub.parse(
|
|
300
|
+
argv?.filter((a) => a !== maybeSubArg),
|
|
301
|
+
[...globalOptions, ...this.options]
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
const parsed = (0, import_node_util.parseArgs)({
|
|
305
|
+
args: argv,
|
|
306
|
+
options: Object.fromEntries(
|
|
307
|
+
[...globalOptions, ...this.options].map((o) => {
|
|
308
|
+
return [o.name, o];
|
|
309
|
+
})
|
|
310
|
+
),
|
|
311
|
+
allowPositionals: true,
|
|
312
|
+
tokens: true,
|
|
313
|
+
strict: true,
|
|
314
|
+
allowNegative: true
|
|
315
|
+
});
|
|
316
|
+
for (let i = 0; i < parsed.tokens.length; i++) {
|
|
317
|
+
const token = parsed.tokens[i];
|
|
318
|
+
if (token.kind === "option") {
|
|
319
|
+
const optionDescriptor = this.options.find((o) => o.name === token.name);
|
|
320
|
+
if (optionDescriptor && optionDescriptor.multiple && optionDescriptor.type === "string") {
|
|
321
|
+
const values = [token.value];
|
|
322
|
+
let j = i + 1;
|
|
323
|
+
while (j < parsed.tokens.length && parsed.tokens[j].kind === "positional") {
|
|
324
|
+
const positionalToken = parsed.tokens[j];
|
|
325
|
+
if (positionalToken.kind === "positional") {
|
|
326
|
+
values.push(positionalToken.value);
|
|
327
|
+
const posIndex = parsed.positionals.indexOf(positionalToken.value);
|
|
328
|
+
if (posIndex !== -1) {
|
|
329
|
+
parsed.positionals.splice(posIndex, 1);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
j++;
|
|
333
|
+
}
|
|
334
|
+
Reflect.set(
|
|
335
|
+
parsed.values,
|
|
336
|
+
token.name,
|
|
337
|
+
values.filter((v) => v !== void 0)
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const parsedArguments = this.arguments.map((arg, index) => {
|
|
343
|
+
if (arg.multiple) {
|
|
344
|
+
const remainingArgs = parsed.positionals.slice(index);
|
|
345
|
+
return remainingArgs.length > 0 ? remainingArgs : arg.defaultValue ?? [];
|
|
346
|
+
} else {
|
|
347
|
+
return parsed.positionals[index] ?? arg.defaultValue;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
for (const option of this.options) {
|
|
351
|
+
if (!(option.name in parsed.values) && "defaultValue" in option) {
|
|
352
|
+
Reflect.set(parsed.values, option.name, option.defaultValue);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
const self = this;
|
|
356
|
+
return {
|
|
357
|
+
get command() {
|
|
358
|
+
return self;
|
|
359
|
+
},
|
|
360
|
+
arguments: parsedArguments,
|
|
361
|
+
options: { ...parsed.values }
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
/** Validates CLI argument ordering */
|
|
365
|
+
assertNoOptionalOrVariadicArguments() {
|
|
366
|
+
if (this.arguments.some((arg) => !arg.required)) {
|
|
367
|
+
throw new Error("Cannot add required argument after optional or variadic arguments");
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
/** Validates optional args don't follow variadic args */
|
|
371
|
+
assertNoVariadicArgument() {
|
|
372
|
+
if (this.arguments.some((arg) => arg.multiple)) {
|
|
373
|
+
throw new Error("Cannot add optional argument after variadic argument");
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/** Ensures only one variadic argument per command */
|
|
377
|
+
assertNoMultipleVariadicArguments() {
|
|
378
|
+
if (this.arguments.some((arg) => arg.multiple)) {
|
|
379
|
+
throw new Error("Cannot add more than one variadic argument");
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/** Ensures unique argument names across arguments and options */
|
|
383
|
+
assertArgumentNameNotInUse(name) {
|
|
384
|
+
if (this.arguments.some((arg) => arg.name === name)) {
|
|
385
|
+
throw new Error(`Argument name already in use: ${name}`);
|
|
386
|
+
}
|
|
387
|
+
if (this.options.some((opt) => opt.name === name)) {
|
|
388
|
+
throw new Error(`Argument name already in use: ${name}`);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/** Validates option short names are single alphanumeric characters */
|
|
392
|
+
assertOptionShortNameIsValid(short) {
|
|
393
|
+
const isSingleAlphaNumericChar = /^[a-zA-Z0-9]$/.test(short);
|
|
394
|
+
if (!isSingleAlphaNumericChar) {
|
|
395
|
+
throw new Error(`Expected short name to be a single alpha-numeric character. Got: ${short}`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
/** Validates option short names are unique across command hierarchy */
|
|
399
|
+
assertOptionShortNameNotInUse(short) {
|
|
400
|
+
for (const opt of this.options) {
|
|
401
|
+
if (opt.short === short) {
|
|
402
|
+
throw new Error(`Option short name already in use: -${short}`);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
this.parent?.assertOptionShortNameNotInUse(short);
|
|
406
|
+
}
|
|
407
|
+
/** Validates option names are unique across command hierarchy */
|
|
408
|
+
assertOptionNameNotInUse(name) {
|
|
409
|
+
if (this.options.some((opt) => opt.name === name)) {
|
|
410
|
+
throw new Error(`Option name already in use: --${name}`);
|
|
411
|
+
}
|
|
412
|
+
this.parent?.assertOptionNameNotInUse(name);
|
|
413
|
+
}
|
|
414
|
+
/** Returns command and all ancestor commands in hierarchy */
|
|
415
|
+
getCommandAndAncestors() {
|
|
416
|
+
const result = [];
|
|
417
|
+
let command = this;
|
|
418
|
+
for (; command; command = command.parent) {
|
|
419
|
+
result.push(command);
|
|
420
|
+
}
|
|
421
|
+
return result;
|
|
422
|
+
}
|
|
423
|
+
/** Returns all ancestor commands excluding this command */
|
|
424
|
+
getAncestors() {
|
|
425
|
+
return this.getCommandAndAncestors().slice(1);
|
|
426
|
+
}
|
|
427
|
+
/** Returns all options from this command and ancestors */
|
|
428
|
+
getOptionsInclAncestors() {
|
|
429
|
+
return this.getCommandAndAncestors().flatMap((cmd) => cmd.options);
|
|
430
|
+
}
|
|
431
|
+
/** Finds subcommand by name or alias */
|
|
432
|
+
findCommand(name) {
|
|
433
|
+
if (!name) return void 0;
|
|
434
|
+
return this.commands.find((cmd) => cmd.name === name || cmd.aliases.includes(name));
|
|
435
|
+
}
|
|
436
|
+
/** Finds option by short or long name */
|
|
437
|
+
findOption(arg) {
|
|
438
|
+
return this.options.find((option) => option.short === arg || option.name === arg);
|
|
439
|
+
}
|
|
440
|
+
/** Returns a view that is compliant with the CommandHelp interface */
|
|
441
|
+
createHelpAdapter() {
|
|
442
|
+
return new import_CommandHelpAdapter.CommandHelpAdapter(this);
|
|
443
|
+
}
|
|
444
|
+
/** Renders formatted help text using provided help definition */
|
|
445
|
+
renderHelp(help) {
|
|
446
|
+
return this.createHelpAdapter().renderHelp(help);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
450
|
+
0 && (module.exports = {
|
|
451
|
+
Command
|
|
452
|
+
});
|
|
453
|
+
//# sourceMappingURL=Command.cjs.map
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
30
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
31
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
32
|
+
if (decorator = decorators[i])
|
|
33
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
34
|
+
if (kind && result) __defProp(target, key, result);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
var CommandHelpAdapter_exports = {};
|
|
38
|
+
__export(CommandHelpAdapter_exports, {
|
|
39
|
+
CommandHelpAdapter: () => CommandHelpAdapter
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(CommandHelpAdapter_exports);
|
|
42
|
+
var import_lazyProp = __toESM(require("./internal/lazyProp"), 1);
|
|
43
|
+
var import_Help = require("./Help");
|
|
44
|
+
var import_renderHelp = require("./renderHelp");
|
|
45
|
+
const _CommandHelpAdapter = class _CommandHelpAdapter {
|
|
46
|
+
constructor(cmd) {
|
|
47
|
+
this.cmd = cmd;
|
|
48
|
+
}
|
|
49
|
+
renderHelp(help = new import_Help.Help()) {
|
|
50
|
+
return (0, import_renderHelp.renderHelp)(this, help);
|
|
51
|
+
}
|
|
52
|
+
get name() {
|
|
53
|
+
return this.cmd.name;
|
|
54
|
+
}
|
|
55
|
+
get aliases() {
|
|
56
|
+
return this.cmd.aliases;
|
|
57
|
+
}
|
|
58
|
+
get summary() {
|
|
59
|
+
return this.cmd.summary ?? (this.cmd.description.includes("\n") ? this.cmd.description.split("\n")[0] : void 0);
|
|
60
|
+
}
|
|
61
|
+
get description() {
|
|
62
|
+
return this.cmd.description;
|
|
63
|
+
}
|
|
64
|
+
get hidden() {
|
|
65
|
+
return this.cmd.hidden;
|
|
66
|
+
}
|
|
67
|
+
get usage() {
|
|
68
|
+
return [
|
|
69
|
+
...this.cmd.options.length ? ["[options]"] : [],
|
|
70
|
+
...this.cmd.commands.length ? ["[command]"] : [],
|
|
71
|
+
...this.cmd.arguments.map((arg) => this.renderArgumentFlags(arg))
|
|
72
|
+
].join(" ");
|
|
73
|
+
}
|
|
74
|
+
get group() {
|
|
75
|
+
return this.cmd.group;
|
|
76
|
+
}
|
|
77
|
+
get commands() {
|
|
78
|
+
return this.cmd.commands.map((c) => new _CommandHelpAdapter(c));
|
|
79
|
+
}
|
|
80
|
+
get options() {
|
|
81
|
+
return this.cmd.options.map((opt) => ({
|
|
82
|
+
...opt,
|
|
83
|
+
flags: this.renderOptionFlags(opt),
|
|
84
|
+
long: opt.name,
|
|
85
|
+
optional: !opt.required,
|
|
86
|
+
negate: false,
|
|
87
|
+
variadic: opt.multiple
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
get arguments() {
|
|
91
|
+
return this.cmd.arguments.map((arg) => ({
|
|
92
|
+
...arg,
|
|
93
|
+
variadic: arg.multiple
|
|
94
|
+
}));
|
|
95
|
+
}
|
|
96
|
+
get parent() {
|
|
97
|
+
return this.cmd.parent ? new _CommandHelpAdapter(this.cmd.parent) : null;
|
|
98
|
+
}
|
|
99
|
+
get helpConfiguration() {
|
|
100
|
+
return { ...this.cmd.helpConfiguration };
|
|
101
|
+
}
|
|
102
|
+
renderArgumentFlags(arg) {
|
|
103
|
+
return arg.required ? arg.multiple ? `<${arg.name}...>` : `<${arg.name}>` : arg.multiple ? `[${arg.name}...]` : `[${arg.name}]`;
|
|
104
|
+
}
|
|
105
|
+
renderOptionFlags(opt) {
|
|
106
|
+
const flags = `-${opt.short}, --${opt.name}`;
|
|
107
|
+
return opt.type === "boolean" ? flags : opt.required ? opt.multiple ? flags + ` <${opt.argName}...>` : flags + ` <${opt.argName}>` : opt.multiple ? flags + ` [${opt.argName}...]` : flags + ` [${opt.argName}]`;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
__decorateClass([
|
|
111
|
+
import_lazyProp.default
|
|
112
|
+
], _CommandHelpAdapter.prototype, "usage", 1);
|
|
113
|
+
__decorateClass([
|
|
114
|
+
import_lazyProp.default
|
|
115
|
+
], _CommandHelpAdapter.prototype, "commands", 1);
|
|
116
|
+
__decorateClass([
|
|
117
|
+
import_lazyProp.default
|
|
118
|
+
], _CommandHelpAdapter.prototype, "arguments", 1);
|
|
119
|
+
__decorateClass([
|
|
120
|
+
import_lazyProp.default
|
|
121
|
+
], _CommandHelpAdapter.prototype, "parent", 1);
|
|
122
|
+
__decorateClass([
|
|
123
|
+
import_lazyProp.default
|
|
124
|
+
], _CommandHelpAdapter.prototype, "helpConfiguration", 1);
|
|
125
|
+
let CommandHelpAdapter = _CommandHelpAdapter;
|
|
126
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
127
|
+
0 && (module.exports = {
|
|
128
|
+
CommandHelpAdapter
|
|
129
|
+
});
|
|
130
|
+
//# sourceMappingURL=CommandHelpAdapter.cjs.map
|