@guanghechen/commander 4.7.6 → 4.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +8 -1
- package/lib/cjs/browser.cjs +2715 -1454
- package/lib/cjs/node.cjs +2715 -1454
- package/lib/esm/browser.mjs +2716 -1454
- package/lib/esm/node.mjs +2716 -1454
- package/lib/schema/preset.schema.json +152 -0
- package/lib/types/browser.d.ts +190 -125
- package/lib/types/node.d.ts +190 -125
- package/package.json +1 -1
package/lib/esm/browser.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { parse } from '@guanghechen/env';
|
|
2
1
|
import { Reporter } from '@guanghechen/reporter';
|
|
2
|
+
import { parse } from '@guanghechen/env';
|
|
3
3
|
|
|
4
4
|
const WINDOWS_DRIVE_ABSOLUTE_REGEX = /^[a-zA-Z]:[\\/]/;
|
|
5
5
|
function isAbsolutePath(filepath) {
|
|
@@ -79,608 +79,1164 @@ function setDefaultCommandRuntime(runtime) {
|
|
|
79
79
|
defaultRuntime = runtime;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
const TERMINAL_STYLE = {
|
|
83
|
-
bold: '\x1b[1m',
|
|
84
|
-
italic: '\x1b[3m',
|
|
85
|
-
underline: '\x1b[4m',
|
|
86
|
-
cyan: '\x1b[36m',
|
|
87
|
-
dim: '\x1b[2m',
|
|
88
|
-
reset: '\x1b[0m',
|
|
89
|
-
};
|
|
90
|
-
function styleText(text, ...styles) {
|
|
91
|
-
return `${styles.join('')}${text}${TERMINAL_STYLE.reset}`;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const BUILTIN_LOG_LEVELS = ['debug', 'info', 'hint', 'warn', 'error'];
|
|
95
|
-
function resolveReporterLogLevel(raw) {
|
|
96
|
-
const normalized = raw.trim().toLowerCase();
|
|
97
|
-
return BUILTIN_LOG_LEVELS.find(level => level === normalized);
|
|
98
|
-
}
|
|
99
|
-
function setReporterLevel(ctx, level) {
|
|
100
|
-
const reporter = ctx.reporter;
|
|
101
|
-
reporter?.setLevel?.(level);
|
|
102
|
-
}
|
|
103
|
-
function setReporterFlight(ctx, flight) {
|
|
104
|
-
const reporter = ctx.reporter;
|
|
105
|
-
reporter?.setFlight?.(flight);
|
|
106
|
-
}
|
|
107
|
-
const logLevelOption = {
|
|
108
|
-
long: 'logLevel',
|
|
109
|
-
type: 'string',
|
|
110
|
-
args: 'required',
|
|
111
|
-
desc: 'Set log level',
|
|
112
|
-
default: 'info',
|
|
113
|
-
choices: [...BUILTIN_LOG_LEVELS],
|
|
114
|
-
coerce: (raw) => {
|
|
115
|
-
const level = resolveReporterLogLevel(raw);
|
|
116
|
-
if (level === undefined) {
|
|
117
|
-
throw new Error(`Invalid log level: ${raw}`);
|
|
118
|
-
}
|
|
119
|
-
return level;
|
|
120
|
-
},
|
|
121
|
-
apply: (value, ctx) => {
|
|
122
|
-
setReporterLevel(ctx, value);
|
|
123
|
-
},
|
|
124
|
-
};
|
|
125
|
-
const logDateOption = {
|
|
126
|
-
long: 'logDate',
|
|
127
|
-
type: 'boolean',
|
|
128
|
-
args: 'none',
|
|
129
|
-
desc: 'Enable log timestamp',
|
|
130
|
-
default: true,
|
|
131
|
-
apply: (value, ctx) => {
|
|
132
|
-
setReporterFlight(ctx, { date: Boolean(value) });
|
|
133
|
-
},
|
|
134
|
-
};
|
|
135
|
-
const logColorfulOption = {
|
|
136
|
-
long: 'logColorful',
|
|
137
|
-
type: 'boolean',
|
|
138
|
-
args: 'none',
|
|
139
|
-
desc: 'Enable colorful log output',
|
|
140
|
-
default: true,
|
|
141
|
-
apply: (value, ctx) => {
|
|
142
|
-
setReporterFlight(ctx, { color: Boolean(value) });
|
|
143
|
-
},
|
|
144
|
-
};
|
|
145
|
-
const silentOption = {
|
|
146
|
-
long: 'silent',
|
|
147
|
-
type: 'boolean',
|
|
148
|
-
args: 'none',
|
|
149
|
-
desc: 'Suppress non-error output',
|
|
150
|
-
default: false,
|
|
151
|
-
apply: (value, ctx) => {
|
|
152
|
-
if (value) {
|
|
153
|
-
setReporterLevel(ctx, 'error');
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
};
|
|
157
|
-
|
|
158
82
|
class CommanderError extends Error {
|
|
159
83
|
kind;
|
|
160
84
|
commandPath;
|
|
161
|
-
|
|
85
|
+
meta;
|
|
86
|
+
constructor(kind, message, commandPath, meta) {
|
|
162
87
|
super(message);
|
|
163
88
|
this.name = 'CommanderError';
|
|
164
89
|
this.kind = kind;
|
|
165
90
|
this.commandPath = commandPath;
|
|
91
|
+
this.meta = meta;
|
|
92
|
+
}
|
|
93
|
+
withIssue(issue) {
|
|
94
|
+
return this.withIssues([issue]);
|
|
95
|
+
}
|
|
96
|
+
withIssues(issues) {
|
|
97
|
+
if (issues.length === 0) {
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
const nextMeta = {
|
|
101
|
+
commandPath: this.meta?.commandPath ?? this.commandPath,
|
|
102
|
+
token: this.meta?.token,
|
|
103
|
+
option: this.meta?.option,
|
|
104
|
+
argument: this.meta?.argument,
|
|
105
|
+
issues: [...(this.meta?.issues ?? []), ...issues],
|
|
106
|
+
};
|
|
107
|
+
return new CommanderError(this.kind, this.message, this.commandPath, nextMeta);
|
|
166
108
|
}
|
|
167
109
|
format() {
|
|
110
|
+
const issues = this.meta?.issues ?? [];
|
|
111
|
+
if (issues.length > 0) {
|
|
112
|
+
const primary = issues.find(issue => issue.kind === 'error') ?? issues[0];
|
|
113
|
+
const lines = [`Error: ${primary.reason.message}`];
|
|
114
|
+
for (const issue of issues) {
|
|
115
|
+
if (issue.kind !== 'hint')
|
|
116
|
+
continue;
|
|
117
|
+
const message = issue.reason.message.startsWith('Hint:')
|
|
118
|
+
? issue.reason.message
|
|
119
|
+
: `Hint: ${issue.reason.message}`;
|
|
120
|
+
lines.push(message);
|
|
121
|
+
}
|
|
122
|
+
lines.push(`Run "${this.commandPath} --help" for usage.`);
|
|
123
|
+
return lines.join('\n');
|
|
124
|
+
}
|
|
168
125
|
return `Error: ${this.message}\nRun "${this.commandPath} --help" for usage.`;
|
|
169
126
|
}
|
|
170
127
|
}
|
|
171
128
|
|
|
172
|
-
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const PRESET_SELECTOR_DELIMITER = ':';
|
|
177
|
-
function kebabToCamelCase(str) {
|
|
178
|
-
return str.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
179
|
-
}
|
|
180
|
-
function camelToKebabCase(str) {
|
|
181
|
-
return str.replace(/[A-Z]/g, m => '-' + m.toLowerCase());
|
|
182
|
-
}
|
|
183
|
-
const ANSI_ESCAPE_REGEX = new RegExp(String.raw `\\x1B\\[[0-?]*[ -/]*[@-~]`, 'g');
|
|
184
|
-
const DECIMAL_INTEGER_REGEX = /^\d(?:_?\d)*$/;
|
|
185
|
-
const DECIMAL_FRACTION_REGEX = /^\d(?:_?\d)*$/;
|
|
186
|
-
const DECIMAL_EXPONENT_REGEX = /^[eE][+-]?\d(?:_?\d)*$/;
|
|
187
|
-
const BINARY_LITERAL_REGEX = /^0[bB][01](?:_?[01])*$/;
|
|
188
|
-
const OCTAL_LITERAL_REGEX = /^0[oO][0-7](?:_?[0-7])*$/;
|
|
189
|
-
const HEX_LITERAL_REGEX = /^0[xX][0-9a-fA-F](?:_?[0-9a-fA-F])*$/;
|
|
190
|
-
const PRESET_PROFILE_NAME_REGEX = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
191
|
-
const PRESET_VARIANT_NAME_REGEX = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
192
|
-
function stripAnsi(value) {
|
|
193
|
-
return value.replace(ANSI_ESCAPE_REGEX, '');
|
|
194
|
-
}
|
|
195
|
-
function isCombiningMark(codePoint) {
|
|
196
|
-
return ((codePoint >= 0x0300 && codePoint <= 0x036f) ||
|
|
197
|
-
(codePoint >= 0x1ab0 && codePoint <= 0x1aff) ||
|
|
198
|
-
(codePoint >= 0x1dc0 && codePoint <= 0x1dff) ||
|
|
199
|
-
(codePoint >= 0x20d0 && codePoint <= 0x20ff) ||
|
|
200
|
-
(codePoint >= 0xfe20 && codePoint <= 0xfe2f));
|
|
201
|
-
}
|
|
202
|
-
function isWideCodePoint(codePoint) {
|
|
203
|
-
if (codePoint < 0x1100) {
|
|
204
|
-
return false;
|
|
129
|
+
async function runCommandAction(params) {
|
|
130
|
+
const { action, actionParams, commandPath } = params;
|
|
131
|
+
if (action === undefined) {
|
|
132
|
+
return;
|
|
205
133
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
(
|
|
211
|
-
|
|
212
|
-
(codePoint >= 0xa960 && codePoint <= 0xa97c) ||
|
|
213
|
-
(codePoint >= 0xac00 && codePoint <= 0xd7a3) ||
|
|
214
|
-
(codePoint >= 0xf900 && codePoint <= 0xfaff) ||
|
|
215
|
-
(codePoint >= 0xfe10 && codePoint <= 0xfe19) ||
|
|
216
|
-
(codePoint >= 0xfe30 && codePoint <= 0xfe6b) ||
|
|
217
|
-
(codePoint >= 0xff01 && codePoint <= 0xff60) ||
|
|
218
|
-
(codePoint >= 0xffe0 && codePoint <= 0xffe6) ||
|
|
219
|
-
(codePoint >= 0x1b000 && codePoint <= 0x1b001) ||
|
|
220
|
-
(codePoint >= 0x1f200 && codePoint <= 0x1f251) ||
|
|
221
|
-
(codePoint >= 0x20000 && codePoint <= 0x3fffd));
|
|
222
|
-
}
|
|
223
|
-
function getDisplayWidth(value) {
|
|
224
|
-
const normalized = stripAnsi(value).normalize('NFC');
|
|
225
|
-
let width = 0;
|
|
226
|
-
for (const char of normalized) {
|
|
227
|
-
const codePoint = char.codePointAt(0);
|
|
228
|
-
if (codePoint === undefined || isCombiningMark(codePoint)) {
|
|
229
|
-
continue;
|
|
134
|
+
try {
|
|
135
|
+
await action(actionParams);
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
if (err instanceof CommanderError) {
|
|
139
|
+
throw err;
|
|
230
140
|
}
|
|
231
|
-
|
|
141
|
+
const issue = {
|
|
142
|
+
kind: 'error',
|
|
143
|
+
stage: 'run',
|
|
144
|
+
scope: 'action',
|
|
145
|
+
reason: {
|
|
146
|
+
code: 'action_failed',
|
|
147
|
+
message: err instanceof Error ? err.message : 'action failed',
|
|
148
|
+
details: err instanceof Error
|
|
149
|
+
? {
|
|
150
|
+
errorName: err.name,
|
|
151
|
+
errorMessage: err.message,
|
|
152
|
+
}
|
|
153
|
+
: {
|
|
154
|
+
errorValue: String(err),
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
throw new CommanderError('ActionFailed', err instanceof Error ? err.message : 'action failed', commandPath).withIssue(issue);
|
|
232
159
|
}
|
|
233
|
-
return width;
|
|
234
160
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
161
|
+
|
|
162
|
+
const BUILTIN_HELP_OPTION = {
|
|
163
|
+
long: 'help',
|
|
164
|
+
type: 'boolean',
|
|
165
|
+
args: 'none',
|
|
166
|
+
desc: 'Show help information',
|
|
167
|
+
};
|
|
168
|
+
const BUILTIN_VERSION_OPTION = {
|
|
169
|
+
long: 'version',
|
|
170
|
+
type: 'boolean',
|
|
171
|
+
args: 'none',
|
|
172
|
+
desc: 'Show version number',
|
|
173
|
+
};
|
|
174
|
+
function errorKindToIssueScope(kind) {
|
|
175
|
+
switch (kind) {
|
|
176
|
+
case 'UnknownSubcommand':
|
|
177
|
+
return 'command';
|
|
178
|
+
case 'MissingRequiredArgument':
|
|
179
|
+
case 'TooManyArguments':
|
|
180
|
+
case 'UnexpectedArgument':
|
|
181
|
+
return 'argument';
|
|
182
|
+
case 'ActionFailed':
|
|
183
|
+
return 'action';
|
|
184
|
+
case 'ConfigurationError':
|
|
185
|
+
return 'runtime';
|
|
186
|
+
default:
|
|
187
|
+
return 'option';
|
|
239
188
|
}
|
|
240
|
-
return value + ' '.repeat(targetWidth - width);
|
|
241
189
|
}
|
|
242
|
-
function
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
190
|
+
function createBuiltinOptionState(enabled) {
|
|
191
|
+
return {
|
|
192
|
+
version: enabled,
|
|
193
|
+
color: enabled,
|
|
194
|
+
devmode: enabled,
|
|
195
|
+
logLevel: enabled,
|
|
196
|
+
silent: enabled,
|
|
197
|
+
logDate: enabled,
|
|
198
|
+
logColorful: enabled,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function normalizeBuiltinConfig(builtin) {
|
|
202
|
+
const resolved = {
|
|
203
|
+
option: createBuiltinOptionState(true),
|
|
204
|
+
};
|
|
205
|
+
if (builtin === undefined) {
|
|
206
|
+
return resolved;
|
|
257
207
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
208
|
+
if (builtin === true) {
|
|
209
|
+
return {
|
|
210
|
+
option: createBuiltinOptionState(true),
|
|
211
|
+
};
|
|
262
212
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
return false;
|
|
213
|
+
if (builtin === false) {
|
|
214
|
+
return {
|
|
215
|
+
option: createBuiltinOptionState(false),
|
|
216
|
+
};
|
|
268
217
|
}
|
|
269
|
-
if (
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
return false;
|
|
218
|
+
if (builtin.option !== undefined) {
|
|
219
|
+
if (builtin.option === false) {
|
|
220
|
+
resolved.option = createBuiltinOptionState(false);
|
|
273
221
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
222
|
+
else if (builtin.option === true) {
|
|
223
|
+
resolved.option = createBuiltinOptionState(true);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
if (builtin.option.version !== undefined)
|
|
227
|
+
resolved.option.version = builtin.option.version;
|
|
228
|
+
if (builtin.option.color !== undefined)
|
|
229
|
+
resolved.option.color = builtin.option.color;
|
|
230
|
+
if (builtin.option.devmode !== undefined)
|
|
231
|
+
resolved.option.devmode = builtin.option.devmode;
|
|
232
|
+
if (builtin.option.logLevel !== undefined) {
|
|
233
|
+
resolved.option.logLevel = builtin.option.logLevel;
|
|
234
|
+
}
|
|
235
|
+
if (builtin.option.silent !== undefined)
|
|
236
|
+
resolved.option.silent = builtin.option.silent;
|
|
237
|
+
if (builtin.option.logDate !== undefined)
|
|
238
|
+
resolved.option.logDate = builtin.option.logDate;
|
|
239
|
+
if (builtin.option.logColorful !== undefined) {
|
|
240
|
+
resolved.option.logColorful = builtin.option.logColorful;
|
|
241
|
+
}
|
|
279
242
|
}
|
|
280
|
-
return intPart.length > 0 || fracPart.length > 0;
|
|
281
243
|
}
|
|
282
|
-
return
|
|
244
|
+
return resolved;
|
|
283
245
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
246
|
+
|
|
247
|
+
function createCommandContext(params) {
|
|
248
|
+
const { leafCommand, chain, cmds, envs, reporter } = params;
|
|
249
|
+
const envSnapshot = { ...envs };
|
|
250
|
+
return {
|
|
251
|
+
cmd: leafCommand,
|
|
252
|
+
chain,
|
|
253
|
+
envs: envSnapshot,
|
|
254
|
+
controls: { help: false, version: false },
|
|
255
|
+
sources: {
|
|
256
|
+
preset: {
|
|
257
|
+
state: 'none',
|
|
258
|
+
argv: [],
|
|
259
|
+
envs: {},
|
|
260
|
+
},
|
|
261
|
+
user: {
|
|
262
|
+
cmds: [...cmds],
|
|
263
|
+
argv: [],
|
|
264
|
+
envs: envSnapshot,
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
reporter,
|
|
268
|
+
};
|
|
294
269
|
}
|
|
295
|
-
function
|
|
296
|
-
return
|
|
270
|
+
function freezeInputSources(sources) {
|
|
271
|
+
return Object.freeze({
|
|
272
|
+
preset: Object.freeze({
|
|
273
|
+
state: sources.preset.state,
|
|
274
|
+
argv: Object.freeze([...sources.preset.argv]),
|
|
275
|
+
envs: Object.freeze({ ...sources.preset.envs }),
|
|
276
|
+
meta: sources.preset.meta === undefined ? undefined : Object.freeze({ ...sources.preset.meta }),
|
|
277
|
+
}),
|
|
278
|
+
user: Object.freeze({
|
|
279
|
+
cmds: Object.freeze([...sources.user.cmds]),
|
|
280
|
+
argv: Object.freeze([...sources.user.argv]),
|
|
281
|
+
envs: Object.freeze({ ...sources.user.envs }),
|
|
282
|
+
}),
|
|
283
|
+
});
|
|
297
284
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
285
|
+
|
|
286
|
+
function buildHelpSubcommands(params) {
|
|
287
|
+
const { entries, getDescription } = params;
|
|
288
|
+
return entries.map(entry => ({
|
|
289
|
+
name: entry.name,
|
|
290
|
+
aliases: entry.aliases,
|
|
291
|
+
desc: getDescription(entry.command),
|
|
292
|
+
}));
|
|
293
|
+
}
|
|
294
|
+
function buildCompletionMeta(params) {
|
|
295
|
+
const { name, desc, mergedOptions, arguments_, supportsBuiltinVersion, builtinHelpOption, builtinVersionOption, subcommands, resolveSubcommandMeta, } = params;
|
|
296
|
+
const optionMap = new Map();
|
|
297
|
+
for (const option of mergedOptions) {
|
|
298
|
+
optionMap.set(option.long, option);
|
|
299
|
+
}
|
|
300
|
+
optionMap.set('help', builtinHelpOption);
|
|
301
|
+
if (supportsBuiltinVersion) {
|
|
302
|
+
optionMap.set('version', builtinVersionOption);
|
|
303
|
+
}
|
|
304
|
+
const options = Array.from(optionMap.values()).map(option => ({
|
|
305
|
+
long: option.long,
|
|
306
|
+
short: option.short,
|
|
307
|
+
desc: option.desc,
|
|
308
|
+
type: option.type,
|
|
309
|
+
args: option.args,
|
|
310
|
+
choices: option.choices?.map(choice => String(choice)),
|
|
311
|
+
}));
|
|
312
|
+
const argumentsMeta = arguments_.map(argument => ({
|
|
313
|
+
name: argument.name,
|
|
314
|
+
kind: argument.kind,
|
|
315
|
+
type: argument.type,
|
|
316
|
+
choices: argument.type === 'choice' ? argument.choices?.map(choice => String(choice)) : undefined,
|
|
317
|
+
}));
|
|
318
|
+
return {
|
|
319
|
+
name,
|
|
320
|
+
desc,
|
|
321
|
+
aliases: [],
|
|
322
|
+
options,
|
|
323
|
+
arguments: argumentsMeta,
|
|
324
|
+
subcommands: subcommands.map(entry => {
|
|
325
|
+
const subMeta = resolveSubcommandMeta(entry.command);
|
|
326
|
+
return {
|
|
327
|
+
...subMeta,
|
|
328
|
+
name: entry.name,
|
|
329
|
+
aliases: entry.aliases,
|
|
330
|
+
};
|
|
331
|
+
}),
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function resolvePresetFileAbsolutePath(params) {
|
|
336
|
+
const { runtime, filepath, baseDirectory } = params;
|
|
337
|
+
if (runtime.isAbsolute(filepath)) {
|
|
338
|
+
return filepath;
|
|
307
339
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
340
|
+
return runtime.resolve(baseDirectory ?? runtime.cwd(), filepath);
|
|
341
|
+
}
|
|
342
|
+
async function readPresetFile(params) {
|
|
343
|
+
const { runtime, file, commandPath } = params;
|
|
344
|
+
try {
|
|
345
|
+
const stats = await runtime.stat(file.absolutePath);
|
|
346
|
+
if (stats.isDirectory()) {
|
|
347
|
+
throw new Error('target is a directory');
|
|
348
|
+
}
|
|
349
|
+
return await runtime.readFile(file.absolutePath);
|
|
350
|
+
}
|
|
351
|
+
catch (error) {
|
|
352
|
+
const ioError = error;
|
|
353
|
+
if (!file.explicit && ioError.code === 'ENOENT') {
|
|
354
|
+
return undefined;
|
|
314
355
|
}
|
|
315
|
-
|
|
356
|
+
throw new CommanderError('ConfigurationError', `failed to read preset file "${file.displayPath}": ${error.message}`, commandPath);
|
|
316
357
|
}
|
|
317
|
-
return prev[right.length];
|
|
318
358
|
}
|
|
319
|
-
function
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
if (namePart.includes('_')) {
|
|
324
|
-
throw new CommanderError('InvalidOptionFormat', `invalid option "${arg}": use '-' instead of '_'`, commandPath);
|
|
359
|
+
function parsePresetEnvsContent(params) {
|
|
360
|
+
const { content, file, commandPath } = params;
|
|
361
|
+
try {
|
|
362
|
+
return parse(content);
|
|
325
363
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
throw new CommanderError('InvalidNegativeOption', `invalid negative option syntax "${arg}"`, commandPath);
|
|
364
|
+
catch (error) {
|
|
365
|
+
throw new CommanderError('ConfigurationError', `failed to parse preset env file "${file.displayPath}": ${error.message}`, commandPath);
|
|
329
366
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
367
|
+
}
|
|
368
|
+
async function buildPresetProfileInputs(params) {
|
|
369
|
+
const { runtime, commandPath, resolvedProfile, validatePresetOptionTokens } = params;
|
|
370
|
+
const presetArgv = [];
|
|
371
|
+
if (resolvedProfile !== undefined && resolvedProfile.optsArgv.length > 0) {
|
|
372
|
+
validatePresetOptionTokens(resolvedProfile.optsArgv, resolvedProfile.optsSourceLabel, commandPath);
|
|
373
|
+
presetArgv.push(...resolvedProfile.optsArgv);
|
|
374
|
+
}
|
|
375
|
+
const presetEnvs = {};
|
|
376
|
+
if (resolvedProfile !== undefined) {
|
|
377
|
+
if (resolvedProfile.profileEnvFileSource !== undefined) {
|
|
378
|
+
const content = await readPresetFile({
|
|
379
|
+
runtime,
|
|
380
|
+
file: resolvedProfile.profileEnvFileSource,
|
|
381
|
+
commandPath,
|
|
382
|
+
});
|
|
383
|
+
if (content !== undefined) {
|
|
384
|
+
Object.assign(presetEnvs, parsePresetEnvsContent({
|
|
385
|
+
content,
|
|
386
|
+
file: resolvedProfile.profileEnvFileSource,
|
|
387
|
+
commandPath,
|
|
388
|
+
}));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
Object.assign(presetEnvs, resolvedProfile.profileInlineEnvs);
|
|
392
|
+
if (resolvedProfile.variantEnvFileSource !== undefined) {
|
|
393
|
+
const content = await readPresetFile({
|
|
394
|
+
runtime,
|
|
395
|
+
file: resolvedProfile.variantEnvFileSource,
|
|
396
|
+
commandPath,
|
|
397
|
+
});
|
|
398
|
+
if (content !== undefined) {
|
|
399
|
+
Object.assign(presetEnvs, parsePresetEnvsContent({
|
|
400
|
+
content,
|
|
401
|
+
file: resolvedProfile.variantEnvFileSource,
|
|
402
|
+
commandPath,
|
|
403
|
+
}));
|
|
404
|
+
}
|
|
336
405
|
}
|
|
337
|
-
|
|
338
|
-
return {
|
|
339
|
-
original: arg,
|
|
340
|
-
resolved: `--${camelName}=false`,
|
|
341
|
-
name: camelName,
|
|
342
|
-
type: 'long',
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
if (!LONG_OPTION_REGEX.test(lowerName)) {
|
|
346
|
-
throw new CommanderError('InvalidOptionFormat', `invalid option format "${arg}"`, commandPath);
|
|
406
|
+
Object.assign(presetEnvs, resolvedProfile.variantInlineEnvs);
|
|
347
407
|
}
|
|
348
|
-
|
|
349
|
-
return {
|
|
350
|
-
original: arg,
|
|
351
|
-
resolved: `--${camelName}${valuePart}`,
|
|
352
|
-
name: camelName,
|
|
353
|
-
type: 'long',
|
|
354
|
-
};
|
|
408
|
+
return { presetArgv, presetEnvs };
|
|
355
409
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
}
|
|
360
|
-
const flags = arg.slice(1);
|
|
361
|
-
return flags.split('').map(flag => ({
|
|
362
|
-
original: `-${flag}`,
|
|
363
|
-
resolved: `-${flag}`,
|
|
364
|
-
name: flag,
|
|
365
|
-
type: 'short',
|
|
366
|
-
}));
|
|
410
|
+
|
|
411
|
+
function getExitCode(error) {
|
|
412
|
+
return error.kind === 'ActionFailed' ? 1 : 2;
|
|
367
413
|
}
|
|
368
|
-
function
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
414
|
+
function renderTermination(params) {
|
|
415
|
+
const { termination, argv, envs, route, controlScan, findCommandByPath, resolveHelpCommand, resolveHelpColor, formatHelpForDisplay, print, } = params;
|
|
416
|
+
if (termination.kind === 'version') {
|
|
417
|
+
print(termination.version);
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
const routeResult = route(argv);
|
|
421
|
+
const leafCommand = routeResult.chain[routeResult.chain.length - 1];
|
|
422
|
+
const controlScanResult = controlScan(routeResult.remaining, leafCommand);
|
|
423
|
+
const helpCommand = findCommandByPath(termination.targetCommandPath) ??
|
|
424
|
+
resolveHelpCommand(leafCommand, controlScanResult.helpTarget);
|
|
425
|
+
const helpColor = resolveHelpColor(helpCommand, controlScanResult.remaining, envs);
|
|
426
|
+
print(formatHelpForDisplay(helpCommand, helpColor));
|
|
427
|
+
}
|
|
428
|
+
function handleRunOutcome(params) {
|
|
429
|
+
const { outcome, argv, envs, route, controlScan, findCommandByPath, resolveHelpCommand, resolveHelpColor, formatHelpForDisplay, print, printError, exit, normalizeControlRunError, } = params;
|
|
430
|
+
if (outcome.kind === 'parsed') {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
if (outcome.kind === 'terminated') {
|
|
434
|
+
try {
|
|
435
|
+
renderTermination({
|
|
436
|
+
termination: outcome.termination,
|
|
437
|
+
argv,
|
|
438
|
+
envs,
|
|
439
|
+
route,
|
|
440
|
+
controlScan,
|
|
441
|
+
findCommandByPath,
|
|
442
|
+
resolveHelpCommand,
|
|
443
|
+
resolveHelpColor,
|
|
444
|
+
formatHelpForDisplay,
|
|
445
|
+
print,
|
|
446
|
+
});
|
|
447
|
+
return;
|
|
384
448
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
449
|
+
catch (error) {
|
|
450
|
+
if (error instanceof CommanderError) {
|
|
451
|
+
const normalizedError = normalizeControlRunError(error);
|
|
452
|
+
printError(normalizedError.format());
|
|
453
|
+
exit(getExitCode(normalizedError));
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
throw error;
|
|
388
457
|
}
|
|
389
|
-
optionTokens.push({
|
|
390
|
-
original: arg,
|
|
391
|
-
resolved: arg,
|
|
392
|
-
name: '',
|
|
393
|
-
type: 'none',
|
|
394
|
-
});
|
|
395
458
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
const BUILTIN_HELP_OPTION = {
|
|
399
|
-
long: 'help',
|
|
400
|
-
type: 'boolean',
|
|
401
|
-
args: 'none',
|
|
402
|
-
desc: 'Show help information',
|
|
403
|
-
};
|
|
404
|
-
const BUILTIN_VERSION_OPTION = {
|
|
405
|
-
long: 'version',
|
|
406
|
-
type: 'boolean',
|
|
407
|
-
args: 'none',
|
|
408
|
-
desc: 'Show version number',
|
|
409
|
-
};
|
|
410
|
-
const BUILTIN_COLOR_OPTION = {
|
|
411
|
-
long: 'color',
|
|
412
|
-
type: 'boolean',
|
|
413
|
-
args: 'none',
|
|
414
|
-
desc: 'Enable colored help output',
|
|
415
|
-
default: true,
|
|
416
|
-
};
|
|
417
|
-
function createBuiltinOptionState(enabled) {
|
|
418
|
-
return {
|
|
419
|
-
version: enabled,
|
|
420
|
-
color: enabled,
|
|
421
|
-
logLevel: enabled,
|
|
422
|
-
silent: enabled,
|
|
423
|
-
logDate: enabled,
|
|
424
|
-
logColorful: enabled,
|
|
425
|
-
};
|
|
459
|
+
printError(outcome.error.format());
|
|
460
|
+
exit(getExitCode(outcome.error));
|
|
426
461
|
}
|
|
427
|
-
function
|
|
428
|
-
|
|
462
|
+
function unwrapParseOutcome(params) {
|
|
463
|
+
const { outcome, commandPath } = params;
|
|
464
|
+
if (outcome.kind === 'parsed') {
|
|
465
|
+
return outcome.parseResult;
|
|
466
|
+
}
|
|
467
|
+
if (outcome.kind === 'terminated') {
|
|
468
|
+
throw new CommanderError('ConfigurationError', 'internal invariant violation: parse mode must not produce termination', commandPath);
|
|
469
|
+
}
|
|
470
|
+
throw outcome.error;
|
|
429
471
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
472
|
+
|
|
473
|
+
function validateOptionConfig(params) {
|
|
474
|
+
const { opt, commandPath } = params;
|
|
475
|
+
if (opt.long === 'help' ||
|
|
476
|
+
opt.long === 'version' ||
|
|
477
|
+
opt.long === 'devmode' ||
|
|
478
|
+
opt.long === 'logLevel') {
|
|
479
|
+
throw new CommanderError('ConfigurationError', `option long name "${opt.long}" is reserved`, commandPath);
|
|
436
480
|
}
|
|
437
|
-
if (
|
|
438
|
-
|
|
439
|
-
option: createBuiltinOptionState(true),
|
|
440
|
-
};
|
|
481
|
+
if (opt.type === 'boolean' && opt.args !== 'none') {
|
|
482
|
+
throw new CommanderError('ConfigurationError', `boolean option "--${opt.long}" must have args: 'none'`, commandPath);
|
|
441
483
|
}
|
|
442
|
-
if (
|
|
443
|
-
|
|
444
|
-
option: createBuiltinOptionState(false),
|
|
445
|
-
};
|
|
484
|
+
if ((opt.type === 'string' || opt.type === 'number') && opt.args === 'none') {
|
|
485
|
+
throw new CommanderError('ConfigurationError', `${opt.type} option "--${opt.long}" must have args: 'required', 'optional', or 'variadic'`, commandPath);
|
|
446
486
|
}
|
|
447
|
-
if (
|
|
448
|
-
|
|
449
|
-
resolved.option = createBuiltinOptionState(false);
|
|
450
|
-
}
|
|
451
|
-
else if (builtin.option === true) {
|
|
452
|
-
resolved.option = createBuiltinOptionState(true);
|
|
453
|
-
}
|
|
454
|
-
else {
|
|
455
|
-
if (builtin.option.version !== undefined)
|
|
456
|
-
resolved.option.version = builtin.option.version;
|
|
457
|
-
if (builtin.option.color !== undefined)
|
|
458
|
-
resolved.option.color = builtin.option.color;
|
|
459
|
-
if (builtin.option.logLevel !== undefined) {
|
|
460
|
-
resolved.option.logLevel = builtin.option.logLevel;
|
|
461
|
-
}
|
|
462
|
-
if (builtin.option.silent !== undefined)
|
|
463
|
-
resolved.option.silent = builtin.option.silent;
|
|
464
|
-
if (builtin.option.logDate !== undefined)
|
|
465
|
-
resolved.option.logDate = builtin.option.logDate;
|
|
466
|
-
if (builtin.option.logColorful !== undefined) {
|
|
467
|
-
resolved.option.logColorful = builtin.option.logColorful;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
487
|
+
if (opt.type === 'number' && opt.args === 'optional') {
|
|
488
|
+
throw new CommanderError('ConfigurationError', `number option "--${opt.long}" does not support args: 'optional'`, commandPath);
|
|
470
489
|
}
|
|
471
|
-
|
|
472
|
-
}
|
|
473
|
-
class Command {
|
|
474
|
-
#name;
|
|
475
|
-
#desc;
|
|
476
|
-
#version;
|
|
477
|
-
#builtinConfig;
|
|
478
|
-
#builtin;
|
|
479
|
-
#presetConfig;
|
|
480
|
-
#reporter;
|
|
481
|
-
#runtime;
|
|
482
|
-
#parent;
|
|
483
|
-
#options = [];
|
|
484
|
-
#arguments = [];
|
|
485
|
-
#examples = [];
|
|
486
|
-
#subcommandsList = [];
|
|
487
|
-
#subcommandsMap = new Map();
|
|
488
|
-
#action = undefined;
|
|
489
|
-
constructor(config) {
|
|
490
|
-
this.#name = config.name ?? '';
|
|
491
|
-
this.#desc = config.desc;
|
|
492
|
-
this.#version = config.version;
|
|
493
|
-
this.#builtinConfig = config.builtin;
|
|
494
|
-
this.#builtin = normalizeBuiltinConfig(config.builtin);
|
|
495
|
-
this.#presetConfig = config.preset;
|
|
496
|
-
this.#reporter = config.reporter;
|
|
497
|
-
this.#runtime = config.runtime ?? getDefaultCommandRuntime();
|
|
490
|
+
if (opt.long.startsWith('no')) {
|
|
491
|
+
throw new CommanderError('ConfigurationError', `option long name cannot start with "no": "${opt.long}"`, commandPath);
|
|
498
492
|
}
|
|
499
|
-
|
|
500
|
-
|
|
493
|
+
if (!/^[a-z][a-zA-Z0-9]*$/.test(opt.long)) {
|
|
494
|
+
throw new CommanderError('ConfigurationError', `option long name must be camelCase: "${opt.long}"`, commandPath);
|
|
501
495
|
}
|
|
502
|
-
|
|
503
|
-
|
|
496
|
+
if (opt.short !== undefined && opt.short.length !== 1) {
|
|
497
|
+
throw new CommanderError('ConfigurationError', `option short name must be a single character: "${opt.short}"`, commandPath);
|
|
504
498
|
}
|
|
505
|
-
|
|
506
|
-
|
|
499
|
+
if (opt.required && opt.default !== undefined) {
|
|
500
|
+
throw new CommanderError('ConfigurationError', `option "--${opt.long}" cannot be both required and have a default value`, commandPath);
|
|
507
501
|
}
|
|
508
|
-
|
|
509
|
-
|
|
502
|
+
if (opt.type === 'boolean' && opt.required) {
|
|
503
|
+
throw new CommanderError('ConfigurationError', `boolean option "--${opt.long}" cannot be required`, commandPath);
|
|
510
504
|
}
|
|
511
|
-
|
|
512
|
-
|
|
505
|
+
if (opt.required && opt.args !== 'required') {
|
|
506
|
+
throw new CommanderError('ConfigurationError', `required option "--${opt.long}" must use args: 'required'`, commandPath);
|
|
513
507
|
}
|
|
514
|
-
|
|
515
|
-
|
|
508
|
+
}
|
|
509
|
+
function checkOptionUniqueness(params) {
|
|
510
|
+
const { opt, options, commandPath } = params;
|
|
511
|
+
if (options.some(o => o.long === opt.long)) {
|
|
512
|
+
throw new CommanderError('OptionConflict', `option "--${opt.long}" is already defined`, commandPath);
|
|
516
513
|
}
|
|
517
|
-
|
|
518
|
-
|
|
514
|
+
if (opt.short && options.some(o => o.short === opt.short)) {
|
|
515
|
+
throw new CommanderError('OptionConflict', `short option "-${opt.short}" is already defined`, commandPath);
|
|
519
516
|
}
|
|
520
|
-
|
|
521
|
-
|
|
517
|
+
}
|
|
518
|
+
function validateArgumentConfig(params) {
|
|
519
|
+
const { arg, arguments_, commandPath } = params;
|
|
520
|
+
if (arg.kind !== 'required' &&
|
|
521
|
+
arg.kind !== 'optional' &&
|
|
522
|
+
arg.kind !== 'variadic' &&
|
|
523
|
+
arg.kind !== 'some') {
|
|
524
|
+
throw new CommanderError('ConfigurationError', `argument "${arg.name}" must specify a valid kind`, commandPath);
|
|
522
525
|
}
|
|
523
|
-
|
|
524
|
-
|
|
526
|
+
if (arg.type !== 'string' && arg.type !== 'choice') {
|
|
527
|
+
throw new CommanderError('ConfigurationError', `argument "${arg.name}" must specify a valid type`, commandPath);
|
|
525
528
|
}
|
|
526
|
-
|
|
527
|
-
|
|
529
|
+
if (arg.default !== undefined && arg.kind !== 'optional') {
|
|
530
|
+
throw new CommanderError('ConfigurationError', `only optional argument "${arg.name}" can have a default value`, commandPath);
|
|
528
531
|
}
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
this.#checkOptionUniqueness(opt);
|
|
532
|
-
this.#options.push(opt);
|
|
533
|
-
return this;
|
|
532
|
+
if (arg.type === 'string' && arg.choices !== undefined) {
|
|
533
|
+
throw new CommanderError('ConfigurationError', `argument "${arg.name}" of type "string" cannot declare choices`, commandPath);
|
|
534
534
|
}
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
535
|
+
if (arg.type === 'choice') {
|
|
536
|
+
if (!Array.isArray(arg.choices) || arg.choices.length === 0) {
|
|
537
|
+
throw new CommanderError('ConfigurationError', `argument "${arg.name}" of type "choice" must declare a non-empty choices array`, commandPath);
|
|
538
|
+
}
|
|
539
|
+
if (arg.choices.some(choice => typeof choice !== 'string')) {
|
|
540
|
+
throw new CommanderError('ConfigurationError', `argument "${arg.name}" choices must be string[]`, commandPath);
|
|
541
|
+
}
|
|
539
542
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
return this;
|
|
543
|
+
if (arg.default !== undefined) {
|
|
544
|
+
validateArgumentDefaultValue({ arg, commandPath });
|
|
543
545
|
}
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
546
|
+
if (arg.kind === 'variadic' || arg.kind === 'some') {
|
|
547
|
+
if (arguments_.some(a => a.kind === 'variadic' || a.kind === 'some')) {
|
|
548
|
+
throw new CommanderError('ConfigurationError', 'only one variadic/some argument is allowed', commandPath);
|
|
549
|
+
}
|
|
547
550
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
+
if (arguments_.length > 0) {
|
|
552
|
+
const last = arguments_[arguments_.length - 1];
|
|
553
|
+
if (last.kind === 'variadic' || last.kind === 'some') {
|
|
554
|
+
throw new CommanderError('ConfigurationError', 'variadic/some argument must be the last argument', commandPath);
|
|
551
555
|
}
|
|
552
|
-
|
|
553
|
-
|
|
556
|
+
}
|
|
557
|
+
if (arg.kind === 'required') {
|
|
558
|
+
const hasOptional = arguments_.some(a => a.kind === 'optional' || a.kind === 'variadic' || a.kind === 'some');
|
|
559
|
+
if (hasOptional) {
|
|
560
|
+
throw new CommanderError('ConfigurationError', `required argument "${arg.name}" cannot come after optional/variadic/some arguments`, commandPath);
|
|
554
561
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
function validateArgumentDefaultValue(params) {
|
|
565
|
+
const { arg, commandPath } = params;
|
|
566
|
+
if (typeof arg.default !== 'string') {
|
|
567
|
+
throw new CommanderError('ConfigurationError', `default value for argument "${arg.name}" must match type "${arg.type}"`, commandPath);
|
|
568
|
+
}
|
|
569
|
+
if (arg.type === 'choice') {
|
|
570
|
+
const choices = arg.choices ?? [];
|
|
571
|
+
if (!choices.includes(arg.default)) {
|
|
572
|
+
throw new CommanderError('ConfigurationError', `default value for argument "${arg.name}" must be one of declared choices`, commandPath);
|
|
558
573
|
}
|
|
559
|
-
const existing = this.#subcommandsList.find(e => e.command === cmd);
|
|
560
|
-
if (existing) {
|
|
561
|
-
if (existing.name === name || existing.aliases.includes(name)) {
|
|
562
|
-
return this;
|
|
563
|
-
}
|
|
564
|
-
existing.aliases.push(name);
|
|
565
|
-
this.#subcommandsMap.set(name, cmd);
|
|
566
|
-
}
|
|
567
|
-
else {
|
|
568
|
-
cmd.#name = name;
|
|
569
|
-
cmd.#parent = this;
|
|
570
|
-
this.#subcommandsList.push({ name, aliases: [], command: cmd });
|
|
571
|
-
this.#subcommandsMap.set(name, cmd);
|
|
572
|
-
}
|
|
573
|
-
return this;
|
|
574
574
|
}
|
|
575
|
-
|
|
575
|
+
}
|
|
576
|
+
function normalizeExample(params) {
|
|
577
|
+
const { example, commandPath } = params;
|
|
578
|
+
const title = example.title.trim();
|
|
579
|
+
const usage = example.usage.trim();
|
|
580
|
+
const desc = example.desc.trim();
|
|
581
|
+
if (!title) {
|
|
582
|
+
throw new CommanderError('ConfigurationError', 'example title cannot be empty', commandPath);
|
|
583
|
+
}
|
|
584
|
+
if (!usage) {
|
|
585
|
+
throw new CommanderError('ConfigurationError', 'example usage cannot be empty', commandPath);
|
|
586
|
+
}
|
|
587
|
+
if (!desc) {
|
|
588
|
+
throw new CommanderError('ConfigurationError', 'example description cannot be empty', commandPath);
|
|
589
|
+
}
|
|
590
|
+
return { title, usage, desc };
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
class CommandKernel {
|
|
594
|
+
#port;
|
|
595
|
+
#diagnostics;
|
|
596
|
+
#contextAdapter;
|
|
597
|
+
constructor(params) {
|
|
598
|
+
this.#port = params.port;
|
|
599
|
+
this.#diagnostics = params.diagnostics;
|
|
600
|
+
this.#contextAdapter = params.contextAdapter;
|
|
601
|
+
}
|
|
602
|
+
async execute(params, mode) {
|
|
576
603
|
const { argv, envs, reporter } = params;
|
|
577
604
|
try {
|
|
578
|
-
const routeResult = this.#route(argv);
|
|
605
|
+
const routeResult = this.#port.route(argv);
|
|
579
606
|
const { chain } = routeResult;
|
|
580
607
|
const leafCommand = chain[chain.length - 1];
|
|
581
|
-
|
|
608
|
+
let ctx = this.#port.createContext({
|
|
582
609
|
chain,
|
|
583
610
|
cmds: routeResult.cmds,
|
|
584
611
|
envs,
|
|
585
612
|
reporter,
|
|
586
613
|
});
|
|
587
|
-
const controlScanResult = this.#controlScan(routeResult.remaining, leafCommand);
|
|
588
|
-
ctx
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
614
|
+
const controlScanResult = this.#port.controlScan(routeResult.remaining, leafCommand);
|
|
615
|
+
ctx = this.#contextAdapter.applyControlScan(ctx, controlScanResult);
|
|
616
|
+
if (mode === 'run') {
|
|
617
|
+
const termination = this.#port.controlRun(leafCommand, controlScanResult);
|
|
618
|
+
if (termination !== undefined) {
|
|
619
|
+
ctx.sources.preset.state = 'skipped';
|
|
620
|
+
return {
|
|
621
|
+
kind: 'terminated',
|
|
622
|
+
termination,
|
|
623
|
+
};
|
|
624
|
+
}
|
|
595
625
|
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
626
|
+
let presetResult;
|
|
627
|
+
try {
|
|
628
|
+
presetResult = await this.#port.preset(controlScanResult.remaining, ctx);
|
|
629
|
+
}
|
|
630
|
+
catch (err) {
|
|
631
|
+
if (err instanceof CommanderError) {
|
|
632
|
+
throw this.#diagnostics.withErrorIssue(err, {
|
|
633
|
+
stage: 'preset',
|
|
634
|
+
scope: this.#port.issueScopeFromErrorKind(err.kind),
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
throw err;
|
|
599
638
|
}
|
|
600
|
-
|
|
601
|
-
const
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
639
|
+
ctx = this.#contextAdapter.applyPresetResult(ctx, presetResult);
|
|
640
|
+
const sourceSegments = presetResult.segments;
|
|
641
|
+
let optionTokens;
|
|
642
|
+
let restArgs;
|
|
643
|
+
try {
|
|
644
|
+
const tokenizeResult = this.#port.tokenize(sourceSegments, this.#port.getCommandPath(leafCommand));
|
|
645
|
+
optionTokens = tokenizeResult.optionTokens;
|
|
646
|
+
restArgs = tokenizeResult.restArgs;
|
|
647
|
+
}
|
|
648
|
+
catch (err) {
|
|
649
|
+
if (err instanceof CommanderError) {
|
|
650
|
+
const enriched = this.#diagnostics.withErrorIssue(err, {
|
|
651
|
+
stage: 'tokenize',
|
|
652
|
+
scope: this.#port.issueScopeFromErrorKind(err.kind),
|
|
653
|
+
});
|
|
654
|
+
throw this.#diagnostics.withPresetInjectedHint(enriched, sourceSegments);
|
|
655
|
+
}
|
|
656
|
+
throw err;
|
|
616
657
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
658
|
+
let optionPolicyMap;
|
|
659
|
+
try {
|
|
660
|
+
optionPolicyMap = this.#port.buildOptionPolicyMap(chain);
|
|
661
|
+
}
|
|
662
|
+
catch (err) {
|
|
663
|
+
if (err instanceof CommanderError) {
|
|
664
|
+
const enriched = this.#diagnostics.withErrorIssue(err, {
|
|
665
|
+
stage: 'builtin-resolve',
|
|
666
|
+
scope: this.#port.issueScopeFromErrorKind(err.kind),
|
|
667
|
+
});
|
|
668
|
+
throw this.#diagnostics.withPresetInjectedHint(enriched, sourceSegments);
|
|
669
|
+
}
|
|
670
|
+
throw err;
|
|
620
671
|
}
|
|
621
|
-
|
|
622
|
-
|
|
672
|
+
let resolveResult;
|
|
673
|
+
try {
|
|
674
|
+
resolveResult = this.#port.resolve(chain, optionTokens, optionPolicyMap);
|
|
675
|
+
}
|
|
676
|
+
catch (err) {
|
|
677
|
+
if (err instanceof CommanderError) {
|
|
678
|
+
const enriched = this.#diagnostics.withErrorIssue(err, {
|
|
679
|
+
stage: 'resolve',
|
|
680
|
+
scope: this.#port.issueScopeFromErrorKind(err.kind),
|
|
681
|
+
});
|
|
682
|
+
throw this.#diagnostics.withPresetInjectedHint(enriched, sourceSegments);
|
|
683
|
+
}
|
|
684
|
+
throw err;
|
|
685
|
+
}
|
|
686
|
+
let parseResult;
|
|
687
|
+
try {
|
|
688
|
+
parseResult = this.#port.parse(chain, resolveResult, optionPolicyMap, ctx, restArgs);
|
|
689
|
+
}
|
|
690
|
+
catch (err) {
|
|
691
|
+
if (err instanceof CommanderError) {
|
|
692
|
+
const optionConflictSource = this.#diagnostics.resolveOptionConflictSourceAttribution(err, sourceSegments);
|
|
693
|
+
const optionConflictPreset = this.#diagnostics.resolveOptionConflictPresetAttribution(err, sourceSegments, optionConflictSource);
|
|
694
|
+
const enriched = this.#diagnostics.withErrorIssue(err, {
|
|
695
|
+
stage: 'parse',
|
|
696
|
+
scope: this.#port.issueScopeFromErrorKind(err.kind),
|
|
697
|
+
source: optionConflictSource,
|
|
698
|
+
preset: optionConflictPreset,
|
|
699
|
+
});
|
|
700
|
+
throw this.#diagnostics.withPresetInjectedHint(enriched, sourceSegments);
|
|
701
|
+
}
|
|
702
|
+
throw err;
|
|
703
|
+
}
|
|
704
|
+
if (mode === 'run') {
|
|
705
|
+
try {
|
|
706
|
+
await this.#port.run({
|
|
707
|
+
leafCommand,
|
|
708
|
+
parseResult,
|
|
709
|
+
presetResult,
|
|
710
|
+
ctx,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
catch (err) {
|
|
714
|
+
if (err instanceof CommanderError) {
|
|
715
|
+
throw this.#diagnostics.withErrorIssue(err, {
|
|
716
|
+
stage: 'run',
|
|
717
|
+
scope: this.#port.issueScopeFromErrorKind(err.kind),
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
throw err;
|
|
721
|
+
}
|
|
623
722
|
}
|
|
723
|
+
return {
|
|
724
|
+
kind: 'parsed',
|
|
725
|
+
parseResult,
|
|
726
|
+
};
|
|
624
727
|
}
|
|
625
728
|
catch (err) {
|
|
626
729
|
if (err instanceof CommanderError) {
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
730
|
+
return {
|
|
731
|
+
kind: 'failed',
|
|
732
|
+
error: this.#diagnostics.normalizeCommanderError(err, {
|
|
733
|
+
fallbackStage: mode === 'run' ? 'run' : 'parse',
|
|
734
|
+
fallbackScope: this.#port.issueScopeFromErrorKind(err.kind),
|
|
735
|
+
}),
|
|
736
|
+
};
|
|
630
737
|
}
|
|
631
738
|
throw err;
|
|
632
739
|
}
|
|
633
740
|
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
class CommandContextAdapter {
|
|
744
|
+
applyControlScan(ctx, controlScanResult) {
|
|
745
|
+
return {
|
|
746
|
+
...ctx,
|
|
747
|
+
controls: controlScanResult.controls,
|
|
748
|
+
sources: {
|
|
749
|
+
...ctx.sources,
|
|
750
|
+
user: {
|
|
751
|
+
...ctx.sources.user,
|
|
752
|
+
argv: [...controlScanResult.remaining],
|
|
753
|
+
},
|
|
754
|
+
},
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
applyPresetResult(ctx, presetResult) {
|
|
758
|
+
return {
|
|
759
|
+
...ctx,
|
|
760
|
+
sources: presetResult.sources,
|
|
761
|
+
envs: presetResult.envs,
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
toActionParams(parseResult) {
|
|
765
|
+
return {
|
|
766
|
+
ctx: parseResult.ctx,
|
|
767
|
+
builtin: parseResult.builtin,
|
|
768
|
+
opts: parseResult.opts,
|
|
769
|
+
args: parseResult.args,
|
|
770
|
+
rawArgs: parseResult.rawArgs,
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
class CommandHintAttributor {
|
|
776
|
+
withPresetInjectedHint(error, sourceSegments) {
|
|
777
|
+
const presetSegments = sourceSegments.filter(segment => segment.source === 'preset');
|
|
778
|
+
if (presetSegments.length === 0) {
|
|
779
|
+
return error;
|
|
780
|
+
}
|
|
781
|
+
if (error.kind === 'ConfigurationError') {
|
|
782
|
+
return error;
|
|
783
|
+
}
|
|
784
|
+
let nextError = error;
|
|
785
|
+
const primaryIssue = nextError.meta?.issues.find(issue => issue.kind === 'error');
|
|
786
|
+
const conflictSources = primaryIssue?.reason.code === 'option_conflict'
|
|
787
|
+
? this.#inferOptionConflictSources(primaryIssue.reason.message, sourceSegments)
|
|
788
|
+
: undefined;
|
|
789
|
+
const hasMixedConflictAttribution = primaryIssue?.source?.related?.includes('user') === true &&
|
|
790
|
+
primaryIssue.source.related.includes('preset');
|
|
791
|
+
const isMixedConflict = hasMixedConflictAttribution ||
|
|
792
|
+
(conflictSources?.has('user') === true && conflictSources.has('preset'));
|
|
793
|
+
if (isMixedConflict &&
|
|
794
|
+
primaryIssue?.reason.code === 'option_conflict' &&
|
|
795
|
+
!nextError.meta?.issues.some(issue => issue.kind === 'hint' && issue.reason.code === 'mixed_source_conflict')) {
|
|
796
|
+
const mixedHint = {
|
|
797
|
+
kind: 'hint',
|
|
798
|
+
stage: primaryIssue.stage,
|
|
799
|
+
originStage: primaryIssue.originStage,
|
|
800
|
+
scope: 'option',
|
|
801
|
+
source: {
|
|
802
|
+
related: ['user', 'preset'],
|
|
803
|
+
},
|
|
804
|
+
reason: {
|
|
805
|
+
code: 'mixed_source_conflict',
|
|
806
|
+
message: 'option conflict involves both user input and preset-injected tokens',
|
|
807
|
+
},
|
|
808
|
+
preset: this.#resolveOptionConflictPresetByMessage(primaryIssue.reason.message, sourceSegments, { related: ['user', 'preset'] }),
|
|
809
|
+
};
|
|
810
|
+
nextError = nextError.withIssue(mixedHint);
|
|
811
|
+
}
|
|
812
|
+
const shouldAttachPresetTokenHint = primaryIssue?.source?.primary === 'preset' || isMixedConflict;
|
|
813
|
+
if (!shouldAttachPresetTokenHint) {
|
|
814
|
+
return nextError;
|
|
815
|
+
}
|
|
816
|
+
if (nextError.meta?.issues.some(issue => issue.kind === 'hint' && issue.reason.code === 'preset_token_injected')) {
|
|
817
|
+
return nextError;
|
|
818
|
+
}
|
|
819
|
+
const firstSegment = presetSegments[0];
|
|
820
|
+
const moreCount = presetSegments.length - 1;
|
|
821
|
+
const moreText = moreCount > 0 ? ` (+${moreCount} more)` : '';
|
|
822
|
+
const currentPrimaryIssue = nextError.meta?.issues.find(issue => issue.kind === 'error');
|
|
823
|
+
const hint = {
|
|
824
|
+
kind: 'hint',
|
|
825
|
+
stage: currentPrimaryIssue?.stage ?? 'parse',
|
|
826
|
+
originStage: 'preset',
|
|
827
|
+
scope: 'preset',
|
|
828
|
+
source: { primary: 'preset' },
|
|
829
|
+
reason: {
|
|
830
|
+
code: 'preset_token_injected',
|
|
831
|
+
message: `token ${JSON.stringify(firstSegment.value)} was injected from preset profile opts${moreText}`,
|
|
832
|
+
},
|
|
833
|
+
preset: firstSegment.preset,
|
|
834
|
+
};
|
|
835
|
+
return nextError.withIssue(hint);
|
|
836
|
+
}
|
|
837
|
+
resolveOptionConflictSourceAttribution(error, sourceSegments) {
|
|
838
|
+
if (error.kind !== 'OptionConflict') {
|
|
839
|
+
return undefined;
|
|
840
|
+
}
|
|
841
|
+
const sources = this.#inferOptionConflictSources(error.message, sourceSegments);
|
|
842
|
+
if (sources.has('user') && sources.has('preset')) {
|
|
843
|
+
return {
|
|
844
|
+
related: ['user', 'preset'],
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
if (sources.has('preset')) {
|
|
848
|
+
return { primary: 'preset' };
|
|
849
|
+
}
|
|
850
|
+
if (sources.has('user')) {
|
|
851
|
+
return { primary: 'user' };
|
|
852
|
+
}
|
|
853
|
+
return undefined;
|
|
854
|
+
}
|
|
855
|
+
resolveOptionConflictPresetAttribution(error, sourceSegments, source) {
|
|
856
|
+
if (error.kind !== 'OptionConflict') {
|
|
857
|
+
return undefined;
|
|
858
|
+
}
|
|
859
|
+
return this.#resolveOptionConflictPresetByMessage(error.message, sourceSegments, source);
|
|
860
|
+
}
|
|
861
|
+
#resolveOptionConflictPresetByMessage(message, sourceSegments, source) {
|
|
862
|
+
const relevantSegments = this.#collectOptionConflictSegments(message, sourceSegments);
|
|
863
|
+
const relevantPresetSegment = relevantSegments.find(segment => segment.source === 'preset' && segment.preset !== undefined);
|
|
864
|
+
if (relevantPresetSegment?.preset !== undefined) {
|
|
865
|
+
return { ...relevantPresetSegment.preset };
|
|
866
|
+
}
|
|
867
|
+
const hasPresetSource = source?.primary === 'preset' || source?.related?.includes('preset') === true;
|
|
868
|
+
if (!hasPresetSource) {
|
|
869
|
+
return undefined;
|
|
870
|
+
}
|
|
871
|
+
const fallbackPresetSegment = sourceSegments.find(segment => segment.source === 'preset' && segment.preset !== undefined);
|
|
872
|
+
return fallbackPresetSegment?.preset === undefined
|
|
873
|
+
? undefined
|
|
874
|
+
: { ...fallbackPresetSegment.preset };
|
|
875
|
+
}
|
|
876
|
+
#inferOptionConflictSources(message, sourceSegments) {
|
|
877
|
+
const relevantSegments = this.#collectOptionConflictSegments(message, sourceSegments);
|
|
878
|
+
const sources = new Set();
|
|
879
|
+
for (const segment of relevantSegments) {
|
|
880
|
+
sources.add(segment.source);
|
|
881
|
+
}
|
|
882
|
+
return sources;
|
|
883
|
+
}
|
|
884
|
+
#collectOptionConflictSegments(message, sourceSegments) {
|
|
885
|
+
const matchedLongs = Array.from(message.matchAll(/"(--[a-z][a-z0-9]*(?:-[a-z0-9]+)*)"/g), match => match[1]);
|
|
886
|
+
const matchedShorts = Array.from(message.matchAll(/"(-[A-Za-z0-9])"/g), match => match[1]);
|
|
887
|
+
const optionLiterals = new Set([...matchedLongs, ...matchedShorts]);
|
|
888
|
+
const optionSegments = sourceSegments.filter(segment => segment.value.startsWith('-'));
|
|
889
|
+
const relevantSegments = optionLiterals.size === 0
|
|
890
|
+
? optionSegments
|
|
891
|
+
: optionSegments.filter(segment => {
|
|
892
|
+
for (const literal of optionLiterals) {
|
|
893
|
+
if (segment.value === literal || segment.value.startsWith(`${literal}=`)) {
|
|
894
|
+
return true;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
return false;
|
|
898
|
+
});
|
|
899
|
+
return relevantSegments;
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
const COMMAND_ERROR_ISSUE_CODES = [
|
|
904
|
+
'invalid_option_format',
|
|
905
|
+
'invalid_negative_option',
|
|
906
|
+
'negative_option_with_value',
|
|
907
|
+
'negative_option_type',
|
|
908
|
+
'unknown_option',
|
|
909
|
+
'unknown_subcommand',
|
|
910
|
+
'unexpected_argument',
|
|
911
|
+
'missing_value',
|
|
912
|
+
'invalid_type',
|
|
913
|
+
'unsupported_short_syntax',
|
|
914
|
+
'option_conflict',
|
|
915
|
+
'missing_required',
|
|
916
|
+
'invalid_choice',
|
|
917
|
+
'invalid_boolean_value',
|
|
918
|
+
'missing_required_argument',
|
|
919
|
+
'too_many_arguments',
|
|
920
|
+
'configuration_error',
|
|
921
|
+
'action_failed',
|
|
922
|
+
];
|
|
923
|
+
const COMMAND_HINT_ISSUE_CODES = [
|
|
924
|
+
'preset_token_injected',
|
|
925
|
+
'mixed_source_conflict',
|
|
926
|
+
'did_you_mean_subcommand',
|
|
927
|
+
'command_does_not_accept_positional_arguments',
|
|
928
|
+
];
|
|
929
|
+
const ERROR_ISSUE_CODE_SET = new Set(COMMAND_ERROR_ISSUE_CODES);
|
|
930
|
+
const HINT_ISSUE_CODE_SET = new Set(COMMAND_HINT_ISSUE_CODES);
|
|
931
|
+
function isErrorIssueCode(code) {
|
|
932
|
+
return ERROR_ISSUE_CODE_SET.has(code);
|
|
933
|
+
}
|
|
934
|
+
function isHintIssueCode(code) {
|
|
935
|
+
return HINT_ISSUE_CODE_SET.has(code);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
function errorKindToIssueCode(kind) {
|
|
939
|
+
switch (kind) {
|
|
940
|
+
case 'InvalidOptionFormat':
|
|
941
|
+
return 'invalid_option_format';
|
|
942
|
+
case 'InvalidNegativeOption':
|
|
943
|
+
return 'invalid_negative_option';
|
|
944
|
+
case 'NegativeOptionWithValue':
|
|
945
|
+
return 'negative_option_with_value';
|
|
946
|
+
case 'NegativeOptionType':
|
|
947
|
+
return 'negative_option_type';
|
|
948
|
+
case 'UnknownOption':
|
|
949
|
+
return 'unknown_option';
|
|
950
|
+
case 'UnknownSubcommand':
|
|
951
|
+
return 'unknown_subcommand';
|
|
952
|
+
case 'UnexpectedArgument':
|
|
953
|
+
return 'unexpected_argument';
|
|
954
|
+
case 'MissingValue':
|
|
955
|
+
return 'missing_value';
|
|
956
|
+
case 'InvalidType':
|
|
957
|
+
return 'invalid_type';
|
|
958
|
+
case 'UnsupportedShortSyntax':
|
|
959
|
+
return 'unsupported_short_syntax';
|
|
960
|
+
case 'OptionConflict':
|
|
961
|
+
return 'option_conflict';
|
|
962
|
+
case 'MissingRequired':
|
|
963
|
+
return 'missing_required';
|
|
964
|
+
case 'InvalidChoice':
|
|
965
|
+
return 'invalid_choice';
|
|
966
|
+
case 'InvalidBooleanValue':
|
|
967
|
+
return 'invalid_boolean_value';
|
|
968
|
+
case 'MissingRequiredArgument':
|
|
969
|
+
return 'missing_required_argument';
|
|
970
|
+
case 'TooManyArguments':
|
|
971
|
+
return 'too_many_arguments';
|
|
972
|
+
case 'ActionFailed':
|
|
973
|
+
return 'action_failed';
|
|
974
|
+
case 'ConfigurationError':
|
|
975
|
+
return 'configuration_error';
|
|
976
|
+
default:
|
|
977
|
+
return 'configuration_error';
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
class CommandIssueNormalizer {
|
|
981
|
+
withErrorIssue(error, params) {
|
|
982
|
+
if (error.meta?.issues.some(existing => existing.kind === 'error')) {
|
|
983
|
+
return error;
|
|
984
|
+
}
|
|
985
|
+
return error.withIssue(this.#buildErrorIssue(error, params));
|
|
986
|
+
}
|
|
987
|
+
normalizeCommanderError(error, options) {
|
|
988
|
+
const issues = error.meta?.issues ?? [];
|
|
989
|
+
const normalizedIssues = this.#normalizeIssues(issues, {
|
|
990
|
+
error,
|
|
991
|
+
fallbackStage: options.fallbackStage,
|
|
992
|
+
fallbackScope: options.fallbackScope,
|
|
644
993
|
});
|
|
645
|
-
const
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
994
|
+
const nextMeta = {
|
|
995
|
+
commandPath: error.meta?.commandPath ?? error.commandPath,
|
|
996
|
+
token: error.meta?.token,
|
|
997
|
+
option: error.meta?.option,
|
|
998
|
+
argument: error.meta?.argument,
|
|
999
|
+
issues: normalizedIssues,
|
|
1000
|
+
};
|
|
1001
|
+
return new CommanderError(error.kind, error.message, error.commandPath, nextMeta);
|
|
1002
|
+
}
|
|
1003
|
+
#buildErrorIssue(error, params) {
|
|
1004
|
+
const { stage, scope, token, source, preset, originStage, details } = params;
|
|
1005
|
+
const tokenSource = token === undefined
|
|
1006
|
+
? undefined
|
|
1007
|
+
: {
|
|
1008
|
+
primary: token.source,
|
|
1009
|
+
};
|
|
1010
|
+
const defaultSource = error.kind === 'MissingRequiredArgument' || error.kind === 'UnknownSubcommand'
|
|
1011
|
+
? { primary: 'user' }
|
|
1012
|
+
: undefined;
|
|
1013
|
+
const issueSource = source ?? tokenSource ?? defaultSource;
|
|
1014
|
+
const issuePreset = preset ?? token?.preset;
|
|
1015
|
+
const presetSource = issueSource?.primary === 'preset' || issueSource?.related?.includes('preset');
|
|
1016
|
+
const resolvedOriginStage = originStage ?? (presetSource && stage !== 'preset' ? 'preset' : undefined);
|
|
1017
|
+
return {
|
|
1018
|
+
kind: 'error',
|
|
1019
|
+
stage,
|
|
1020
|
+
originStage: resolvedOriginStage,
|
|
1021
|
+
source: issueSource,
|
|
1022
|
+
scope,
|
|
1023
|
+
reason: {
|
|
1024
|
+
code: errorKindToIssueCode(error.kind),
|
|
1025
|
+
message: error.message,
|
|
1026
|
+
details,
|
|
1027
|
+
},
|
|
1028
|
+
preset: issuePreset,
|
|
1029
|
+
};
|
|
656
1030
|
}
|
|
657
|
-
|
|
658
|
-
|
|
1031
|
+
#normalizeIssues(issues, options) {
|
|
1032
|
+
const normalized = issues
|
|
1033
|
+
.map(issue => this.#normalizeIssue(issue))
|
|
1034
|
+
.filter((issue) => issue !== undefined);
|
|
1035
|
+
const primaryError = normalized.find(issue => issue.kind === 'error');
|
|
1036
|
+
const hints = normalized.filter(issue => issue.kind === 'hint');
|
|
1037
|
+
if (primaryError === undefined) {
|
|
1038
|
+
const fallbackError = this.#buildErrorIssue(options.error, {
|
|
1039
|
+
stage: options.fallbackStage,
|
|
1040
|
+
scope: options.fallbackScope,
|
|
1041
|
+
});
|
|
1042
|
+
return [fallbackError, ...hints];
|
|
1043
|
+
}
|
|
1044
|
+
return [primaryError, ...hints];
|
|
1045
|
+
}
|
|
1046
|
+
#normalizeIssue(issue) {
|
|
1047
|
+
let source = this.#normalizeIssueSource(issue.source);
|
|
1048
|
+
let preset = this.#normalizePresetIssueMeta(issue.preset);
|
|
1049
|
+
const reasonCode = issue.reason.code;
|
|
1050
|
+
if (!this.#hasPresetSource(source)) {
|
|
1051
|
+
preset = undefined;
|
|
1052
|
+
}
|
|
1053
|
+
if (source?.primary === 'preset' && !this.#hasPresetLocator(preset)) {
|
|
1054
|
+
source = this.#dropPresetPrimarySource(source);
|
|
1055
|
+
if (!this.#hasPresetSource(source)) {
|
|
1056
|
+
preset = undefined;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
if (issue.kind === 'error') {
|
|
1060
|
+
const normalizedCode = isErrorIssueCode(reasonCode)
|
|
1061
|
+
? reasonCode
|
|
1062
|
+
: 'configuration_error';
|
|
1063
|
+
const normalizedReason = normalizedCode === reasonCode
|
|
1064
|
+
? issue.reason
|
|
1065
|
+
: {
|
|
1066
|
+
code: normalizedCode,
|
|
1067
|
+
message: `invalid error issue code "${reasonCode}"`,
|
|
1068
|
+
details: {
|
|
1069
|
+
...(issue.reason.details ?? {}),
|
|
1070
|
+
invalidIssueCode: reasonCode,
|
|
1071
|
+
},
|
|
1072
|
+
};
|
|
1073
|
+
return {
|
|
1074
|
+
...issue,
|
|
1075
|
+
source,
|
|
1076
|
+
preset,
|
|
1077
|
+
reason: normalizedReason,
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
if (!isHintIssueCode(reasonCode)) {
|
|
1081
|
+
return undefined;
|
|
1082
|
+
}
|
|
1083
|
+
return {
|
|
1084
|
+
...issue,
|
|
1085
|
+
source,
|
|
1086
|
+
preset,
|
|
1087
|
+
};
|
|
659
1088
|
}
|
|
660
|
-
#
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
if (!this.#shouldRenderStyledHelp(color)) {
|
|
664
|
-
return this.#renderHelpPlain(helpData);
|
|
1089
|
+
#normalizeIssueSource(source) {
|
|
1090
|
+
if (source === undefined) {
|
|
1091
|
+
return undefined;
|
|
665
1092
|
}
|
|
666
|
-
|
|
1093
|
+
const related = source.related === undefined ? undefined : Array.from(new Set(source.related));
|
|
1094
|
+
const primary = source.primary;
|
|
1095
|
+
if (primary !== undefined && related !== undefined && !related.includes(primary)) {
|
|
1096
|
+
related.push(primary);
|
|
1097
|
+
}
|
|
1098
|
+
if (primary === undefined && (related === undefined || related.length === 0)) {
|
|
1099
|
+
return undefined;
|
|
1100
|
+
}
|
|
1101
|
+
return {
|
|
1102
|
+
primary,
|
|
1103
|
+
related,
|
|
1104
|
+
};
|
|
667
1105
|
}
|
|
668
|
-
#
|
|
669
|
-
|
|
1106
|
+
#dropPresetPrimarySource(source) {
|
|
1107
|
+
const related = source.related?.filter(item => item !== 'preset');
|
|
1108
|
+
if (related === undefined || related.length === 0) {
|
|
1109
|
+
return undefined;
|
|
1110
|
+
}
|
|
1111
|
+
return {
|
|
1112
|
+
related,
|
|
1113
|
+
};
|
|
670
1114
|
}
|
|
671
|
-
#
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
if (this.#supportsBuiltinVersion()) {
|
|
675
|
-
allOptions.push(BUILTIN_VERSION_OPTION);
|
|
1115
|
+
#normalizePresetIssueMeta(preset) {
|
|
1116
|
+
if (preset === undefined) {
|
|
1117
|
+
return undefined;
|
|
676
1118
|
}
|
|
677
|
-
const
|
|
678
|
-
|
|
1119
|
+
const nextPreset = {
|
|
1120
|
+
file: preset.file,
|
|
1121
|
+
profile: preset.profile,
|
|
1122
|
+
variant: preset.variant,
|
|
1123
|
+
optionKey: preset.optionKey,
|
|
1124
|
+
};
|
|
1125
|
+
const hasAnyValue = nextPreset.file !== undefined ||
|
|
1126
|
+
nextPreset.profile !== undefined ||
|
|
1127
|
+
nextPreset.variant !== undefined ||
|
|
1128
|
+
nextPreset.optionKey !== undefined;
|
|
1129
|
+
return hasAnyValue ? nextPreset : undefined;
|
|
1130
|
+
}
|
|
1131
|
+
#hasPresetLocator(preset) {
|
|
1132
|
+
return (preset?.file !== undefined || preset?.profile !== undefined || preset?.variant !== undefined);
|
|
1133
|
+
}
|
|
1134
|
+
#hasPresetSource(source) {
|
|
1135
|
+
return source?.primary === 'preset' || source?.related?.includes('preset') === true;
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
class CommandDiagnosticsEngine {
|
|
1140
|
+
#issueNormalizer = new CommandIssueNormalizer();
|
|
1141
|
+
#hintAttributor = new CommandHintAttributor();
|
|
1142
|
+
withErrorIssue(error, params) {
|
|
1143
|
+
return this.#issueNormalizer.withErrorIssue(error, params);
|
|
1144
|
+
}
|
|
1145
|
+
withPresetInjectedHint(error, sourceSegments) {
|
|
1146
|
+
return this.#hintAttributor.withPresetInjectedHint(error, sourceSegments);
|
|
1147
|
+
}
|
|
1148
|
+
normalizeCommanderError(error, options) {
|
|
1149
|
+
return this.#issueNormalizer.normalizeCommanderError(error, options);
|
|
1150
|
+
}
|
|
1151
|
+
resolveOptionConflictSourceAttribution(error, sourceSegments) {
|
|
1152
|
+
return this.#hintAttributor.resolveOptionConflictSourceAttribution(error, sourceSegments);
|
|
1153
|
+
}
|
|
1154
|
+
resolveOptionConflictPresetAttribution(error, sourceSegments, source) {
|
|
1155
|
+
return this.#hintAttributor.resolveOptionConflictPresetAttribution(error, sourceSegments, source);
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
const TERMINAL_STYLE = {
|
|
1160
|
+
bold: '\x1b[1m',
|
|
1161
|
+
italic: '\x1b[3m',
|
|
1162
|
+
underline: '\x1b[4m',
|
|
1163
|
+
cyan: '\x1b[36m',
|
|
1164
|
+
dim: '\x1b[2m',
|
|
1165
|
+
reset: '\x1b[0m',
|
|
1166
|
+
};
|
|
1167
|
+
function styleText(text, ...styles) {
|
|
1168
|
+
return `${styles.join('')}${text}${TERMINAL_STYLE.reset}`;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
const ANSI_ESCAPE_REGEX = new RegExp(String.raw `\x1B\[[0-?]*[ -/]*[@-~]`, 'g');
|
|
1172
|
+
function camelToKebabCase$3(str) {
|
|
1173
|
+
return str.replace(/[A-Z]/g, m => '-' + m.toLowerCase());
|
|
1174
|
+
}
|
|
1175
|
+
function isNoColorEnabled$1(envs) {
|
|
1176
|
+
return envs['NO_COLOR'] !== undefined;
|
|
1177
|
+
}
|
|
1178
|
+
function stripAnsi(value) {
|
|
1179
|
+
return value.replace(ANSI_ESCAPE_REGEX, '');
|
|
1180
|
+
}
|
|
1181
|
+
function isCombiningMark(codePoint) {
|
|
1182
|
+
return ((codePoint >= 0x0300 && codePoint <= 0x036f) ||
|
|
1183
|
+
(codePoint >= 0x1ab0 && codePoint <= 0x1aff) ||
|
|
1184
|
+
(codePoint >= 0x1dc0 && codePoint <= 0x1dff) ||
|
|
1185
|
+
(codePoint >= 0x20d0 && codePoint <= 0x20ff) ||
|
|
1186
|
+
(codePoint >= 0xfe20 && codePoint <= 0xfe2f));
|
|
1187
|
+
}
|
|
1188
|
+
function isWideCodePoint(codePoint) {
|
|
1189
|
+
if (codePoint < 0x1100) {
|
|
1190
|
+
return false;
|
|
1191
|
+
}
|
|
1192
|
+
return (codePoint <= 0x115f ||
|
|
1193
|
+
codePoint === 0x2329 ||
|
|
1194
|
+
codePoint === 0x232a ||
|
|
1195
|
+
(codePoint >= 0x2e80 && codePoint <= 0x3247 && codePoint !== 0x303f) ||
|
|
1196
|
+
(codePoint >= 0x3250 && codePoint <= 0x4dbf) ||
|
|
1197
|
+
(codePoint >= 0x4e00 && codePoint <= 0xa4c6) ||
|
|
1198
|
+
(codePoint >= 0xa960 && codePoint <= 0xa97c) ||
|
|
1199
|
+
(codePoint >= 0xac00 && codePoint <= 0xd7a3) ||
|
|
1200
|
+
(codePoint >= 0xf900 && codePoint <= 0xfaff) ||
|
|
1201
|
+
(codePoint >= 0xfe10 && codePoint <= 0xfe19) ||
|
|
1202
|
+
(codePoint >= 0xfe30 && codePoint <= 0xfe6b) ||
|
|
1203
|
+
(codePoint >= 0xff01 && codePoint <= 0xff60) ||
|
|
1204
|
+
(codePoint >= 0xffe0 && codePoint <= 0xffe6) ||
|
|
1205
|
+
(codePoint >= 0x1b000 && codePoint <= 0x1b001) ||
|
|
1206
|
+
(codePoint >= 0x1f200 && codePoint <= 0x1f251) ||
|
|
1207
|
+
(codePoint >= 0x20000 && codePoint <= 0x3fffd));
|
|
1208
|
+
}
|
|
1209
|
+
function getDisplayWidth(value) {
|
|
1210
|
+
const normalized = stripAnsi(value).normalize('NFC');
|
|
1211
|
+
let width = 0;
|
|
1212
|
+
for (const char of normalized) {
|
|
1213
|
+
const codePoint = char.codePointAt(0);
|
|
1214
|
+
if (codePoint === undefined || isCombiningMark(codePoint)) {
|
|
1215
|
+
continue;
|
|
1216
|
+
}
|
|
1217
|
+
width += isWideCodePoint(codePoint) ? 2 : 1;
|
|
1218
|
+
}
|
|
1219
|
+
return width;
|
|
1220
|
+
}
|
|
1221
|
+
function padDisplayEnd(value, targetWidth) {
|
|
1222
|
+
const width = getDisplayWidth(value);
|
|
1223
|
+
if (width >= targetWidth) {
|
|
1224
|
+
return value;
|
|
1225
|
+
}
|
|
1226
|
+
return value + ' '.repeat(targetWidth - width);
|
|
1227
|
+
}
|
|
1228
|
+
class CommandHelpRenderer {
|
|
1229
|
+
buildHelpData(params) {
|
|
1230
|
+
const allOptions = [...params.options, params.builtinHelpOption];
|
|
1231
|
+
if (params.supportsBuiltinVersion) {
|
|
1232
|
+
allOptions.push(params.builtinVersionOption);
|
|
1233
|
+
}
|
|
1234
|
+
let usage = `Usage: ${params.commandPath}`;
|
|
679
1235
|
if (allOptions.length > 0)
|
|
680
1236
|
usage += ' [options]';
|
|
681
|
-
if (
|
|
1237
|
+
if (params.subcommands.length > 0)
|
|
682
1238
|
usage += ' [command]';
|
|
683
|
-
for (const arg of
|
|
1239
|
+
for (const arg of params.arguments) {
|
|
684
1240
|
if (arg.kind === 'required') {
|
|
685
1241
|
usage += ` <${arg.name}>`;
|
|
686
1242
|
}
|
|
@@ -695,7 +1251,7 @@ class Command {
|
|
|
695
1251
|
}
|
|
696
1252
|
}
|
|
697
1253
|
const argumentsLines = [];
|
|
698
|
-
for (const arg of
|
|
1254
|
+
for (const arg of params.arguments) {
|
|
699
1255
|
const sig = arg.kind === 'required'
|
|
700
1256
|
? `<${arg.name}>`
|
|
701
1257
|
: arg.kind === 'optional'
|
|
@@ -731,11 +1287,11 @@ class Command {
|
|
|
731
1287
|
if (rankA !== rankB) {
|
|
732
1288
|
return rankA - rankB;
|
|
733
1289
|
}
|
|
734
|
-
return camelToKebabCase(a.long).localeCompare(camelToKebabCase(b.long));
|
|
1290
|
+
return camelToKebabCase$3(a.long).localeCompare(camelToKebabCase$3(b.long));
|
|
735
1291
|
});
|
|
736
1292
|
const options = [];
|
|
737
1293
|
for (const opt of sortedOptions) {
|
|
738
|
-
const kebabLong = camelToKebabCase(opt.long);
|
|
1294
|
+
const kebabLong = camelToKebabCase$3(opt.long);
|
|
739
1295
|
let sig = opt.short ? `-${opt.short}, ` : ' ';
|
|
740
1296
|
sig += `--${kebabLong}`;
|
|
741
1297
|
if (opt.args === 'optional') {
|
|
@@ -754,24 +1310,24 @@ class Command {
|
|
|
754
1310
|
options.push({ sig, desc });
|
|
755
1311
|
}
|
|
756
1312
|
const commands = [];
|
|
757
|
-
if (
|
|
1313
|
+
if (params.subcommands.length > 0) {
|
|
758
1314
|
commands.push({ name: 'help', desc: 'Show help for a command' });
|
|
759
1315
|
}
|
|
760
|
-
const sortedSubcommands = [...
|
|
1316
|
+
const sortedSubcommands = [...params.subcommands].sort((a, b) => a.name.localeCompare(b.name));
|
|
761
1317
|
for (const entry of sortedSubcommands) {
|
|
762
1318
|
let name = entry.name;
|
|
763
1319
|
if (entry.aliases.length > 0) {
|
|
764
1320
|
name += `, ${entry.aliases.join(', ')}`;
|
|
765
1321
|
}
|
|
766
|
-
commands.push({ name, desc: entry.
|
|
1322
|
+
commands.push({ name, desc: entry.desc });
|
|
767
1323
|
}
|
|
768
|
-
const examples =
|
|
1324
|
+
const examples = params.examples.map(example => ({
|
|
769
1325
|
title: example.title,
|
|
770
|
-
usage: commandPath ? `${commandPath} ${example.usage}` : example.usage,
|
|
1326
|
+
usage: params.commandPath ? `${params.commandPath} ${example.usage}` : example.usage,
|
|
771
1327
|
desc: example.desc,
|
|
772
1328
|
}));
|
|
773
1329
|
return {
|
|
774
|
-
desc:
|
|
1330
|
+
desc: params.desc,
|
|
775
1331
|
usage,
|
|
776
1332
|
arguments: argumentsLines,
|
|
777
1333
|
options,
|
|
@@ -779,6 +1335,51 @@ class Command {
|
|
|
779
1335
|
examples,
|
|
780
1336
|
};
|
|
781
1337
|
}
|
|
1338
|
+
formatHelp(helpData) {
|
|
1339
|
+
return this.#renderHelpPlain(helpData);
|
|
1340
|
+
}
|
|
1341
|
+
formatHelpForDisplay(helpData, color) {
|
|
1342
|
+
if (!this.#shouldRenderStyledHelp(color)) {
|
|
1343
|
+
return this.#renderHelpPlain(helpData);
|
|
1344
|
+
}
|
|
1345
|
+
return this.#renderHelpTerminal(helpData);
|
|
1346
|
+
}
|
|
1347
|
+
resolveHelpColorFromTailArgv(params) {
|
|
1348
|
+
const colorOption = params.options.find(opt => opt.long === 'color');
|
|
1349
|
+
let color = !isNoColorEnabled$1(params.envs);
|
|
1350
|
+
if (!colorOption || colorOption.type !== 'boolean' || colorOption.args !== 'none') {
|
|
1351
|
+
return color;
|
|
1352
|
+
}
|
|
1353
|
+
const separatorIndex = params.tailArgv.indexOf('--');
|
|
1354
|
+
const scanTokens = separatorIndex === -1 ? params.tailArgv : params.tailArgv.slice(0, separatorIndex);
|
|
1355
|
+
for (const token of scanTokens) {
|
|
1356
|
+
if (token === '--color') {
|
|
1357
|
+
color = true;
|
|
1358
|
+
continue;
|
|
1359
|
+
}
|
|
1360
|
+
if (token === '--no-color') {
|
|
1361
|
+
color = false;
|
|
1362
|
+
continue;
|
|
1363
|
+
}
|
|
1364
|
+
if (!token.startsWith('--color=')) {
|
|
1365
|
+
continue;
|
|
1366
|
+
}
|
|
1367
|
+
const value = token.slice('--color='.length);
|
|
1368
|
+
if (value === 'true') {
|
|
1369
|
+
color = true;
|
|
1370
|
+
}
|
|
1371
|
+
else if (value === 'false') {
|
|
1372
|
+
color = false;
|
|
1373
|
+
}
|
|
1374
|
+
else {
|
|
1375
|
+
throw new CommanderError('InvalidBooleanValue', `invalid value "${value}" for boolean option "--color". Use "true" or "false"`, params.commandPath);
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
return color;
|
|
1379
|
+
}
|
|
1380
|
+
#shouldRenderStyledHelp(color) {
|
|
1381
|
+
return color && process.stdout.isTTY === true;
|
|
1382
|
+
}
|
|
782
1383
|
#renderHelpPlain(helpData) {
|
|
783
1384
|
const lines = [];
|
|
784
1385
|
const labelWidth = this.#getHelpLabelWidth(helpData);
|
|
@@ -873,243 +1474,287 @@ class Command {
|
|
|
873
1474
|
const outputLabel = styleLabel ? styleLabel(paddedLabel) : paddedLabel;
|
|
874
1475
|
return ` ${outputLabel} ${desc}`;
|
|
875
1476
|
}
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
const DECIMAL_INTEGER_REGEX = /^\d(?:_?\d)*$/;
|
|
1480
|
+
const DECIMAL_FRACTION_REGEX = /^\d(?:_?\d)*$/;
|
|
1481
|
+
const DECIMAL_EXPONENT_REGEX = /^[eE][+-]?\d(?:_?\d)*$/;
|
|
1482
|
+
const BINARY_LITERAL_REGEX = /^0[bB][01](?:_?[01])*$/;
|
|
1483
|
+
const OCTAL_LITERAL_REGEX = /^0[oO][0-7](?:_?[0-7])*$/;
|
|
1484
|
+
const HEX_LITERAL_REGEX = /^0[xX][0-9a-fA-F](?:_?[0-9a-fA-F])*$/;
|
|
1485
|
+
function camelToKebabCase$2(str) {
|
|
1486
|
+
return str.replace(/[A-Z]/g, m => '-' + m.toLowerCase());
|
|
1487
|
+
}
|
|
1488
|
+
function isNoColorEnabled(envs) {
|
|
1489
|
+
return envs['NO_COLOR'] !== undefined;
|
|
1490
|
+
}
|
|
1491
|
+
function isValidPrimitiveNumberLiteral(rawValue) {
|
|
1492
|
+
if (rawValue.trim() !== rawValue || rawValue.length === 0) {
|
|
1493
|
+
return false;
|
|
1494
|
+
}
|
|
1495
|
+
if (rawValue === 'NaN' || rawValue === 'Infinity' || rawValue === '-Infinity') {
|
|
1496
|
+
return false;
|
|
1497
|
+
}
|
|
1498
|
+
if (BINARY_LITERAL_REGEX.test(rawValue)) {
|
|
1499
|
+
return true;
|
|
1500
|
+
}
|
|
1501
|
+
if (OCTAL_LITERAL_REGEX.test(rawValue)) {
|
|
1502
|
+
return true;
|
|
1503
|
+
}
|
|
1504
|
+
if (HEX_LITERAL_REGEX.test(rawValue)) {
|
|
1505
|
+
return true;
|
|
1506
|
+
}
|
|
1507
|
+
const sign = rawValue[0] === '+' || rawValue[0] === '-' ? rawValue[0] : '';
|
|
1508
|
+
const body = sign ? rawValue.slice(1) : rawValue;
|
|
1509
|
+
if (body.length === 0) {
|
|
1510
|
+
return false;
|
|
1511
|
+
}
|
|
1512
|
+
const expIndex = body.search(/[eE]/);
|
|
1513
|
+
const basePart = expIndex === -1 ? body : body.slice(0, expIndex);
|
|
1514
|
+
const expPart = expIndex === -1 ? '' : body.slice(expIndex);
|
|
1515
|
+
if (expPart && !DECIMAL_EXPONENT_REGEX.test(expPart)) {
|
|
1516
|
+
return false;
|
|
1517
|
+
}
|
|
1518
|
+
if (basePart.includes('.')) {
|
|
1519
|
+
const decimalParts = basePart.split('.');
|
|
1520
|
+
if (decimalParts.length !== 2) {
|
|
1521
|
+
return false;
|
|
897
1522
|
}
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
choices: arg.type === 'choice' ? arg.choices?.map(choice => String(choice)) : undefined,
|
|
904
|
-
});
|
|
1523
|
+
const [intPart, fracPart] = decimalParts;
|
|
1524
|
+
const intOk = intPart.length === 0 || DECIMAL_INTEGER_REGEX.test(intPart);
|
|
1525
|
+
const fracOk = fracPart.length === 0 || DECIMAL_FRACTION_REGEX.test(fracPart);
|
|
1526
|
+
if (!intOk || !fracOk) {
|
|
1527
|
+
return false;
|
|
905
1528
|
}
|
|
906
|
-
return
|
|
907
|
-
name: this.#name,
|
|
908
|
-
desc: this.#desc,
|
|
909
|
-
aliases: [],
|
|
910
|
-
options,
|
|
911
|
-
arguments: argumentsMeta,
|
|
912
|
-
subcommands: this.#subcommandsList.map(entry => {
|
|
913
|
-
const subMeta = entry.command.getCompletionMeta();
|
|
914
|
-
return {
|
|
915
|
-
...subMeta,
|
|
916
|
-
name: entry.name,
|
|
917
|
-
aliases: entry.aliases,
|
|
918
|
-
};
|
|
919
|
-
}),
|
|
920
|
-
};
|
|
1529
|
+
return intPart.length > 0 || fracPart.length > 0;
|
|
921
1530
|
}
|
|
922
|
-
|
|
923
|
-
|
|
1531
|
+
return DECIMAL_INTEGER_REGEX.test(basePart);
|
|
1532
|
+
}
|
|
1533
|
+
function parsePrimitiveNumber(rawValue) {
|
|
1534
|
+
if (!isValidPrimitiveNumberLiteral(rawValue)) {
|
|
1535
|
+
return undefined;
|
|
924
1536
|
}
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
let idx = 0;
|
|
930
|
-
while (idx < argv.length) {
|
|
931
|
-
const token = argv[idx];
|
|
932
|
-
if (token.startsWith('-'))
|
|
933
|
-
break;
|
|
934
|
-
const entry = current.#findSubcommandEntry(token);
|
|
935
|
-
if (!entry)
|
|
936
|
-
break;
|
|
937
|
-
current = entry.command;
|
|
938
|
-
cmds.push(token);
|
|
939
|
-
chain.push(current);
|
|
940
|
-
idx += 1;
|
|
941
|
-
}
|
|
942
|
-
return { chain, remaining: argv.slice(idx), cmds };
|
|
1537
|
+
const normalized = rawValue.replaceAll('_', '');
|
|
1538
|
+
const value = Number(normalized);
|
|
1539
|
+
if (!Number.isFinite(value)) {
|
|
1540
|
+
return undefined;
|
|
943
1541
|
}
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
const
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
}
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
1542
|
+
return value;
|
|
1543
|
+
}
|
|
1544
|
+
class CommandOptionParser {
|
|
1545
|
+
parseOptions(params) {
|
|
1546
|
+
const { tokens, allOptions, envs, commandPath } = params;
|
|
1547
|
+
const opts = {};
|
|
1548
|
+
const explicitOptionLongs = new Set();
|
|
1549
|
+
let sawColorToken = false;
|
|
1550
|
+
for (const opt of allOptions) {
|
|
1551
|
+
if (opt.default !== undefined) {
|
|
1552
|
+
opts[opt.long] = opt.default;
|
|
1553
|
+
}
|
|
1554
|
+
else if (opt.type === 'boolean' && opt.args === 'none') {
|
|
1555
|
+
opts[opt.long] = false;
|
|
1556
|
+
}
|
|
1557
|
+
else if (opt.args === 'variadic') {
|
|
1558
|
+
opts[opt.long] = [];
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
const optionByLong = new Map();
|
|
1562
|
+
const optionByShort = new Map();
|
|
1563
|
+
for (const opt of allOptions) {
|
|
1564
|
+
optionByLong.set(opt.long, opt);
|
|
1565
|
+
if (opt.short) {
|
|
1566
|
+
optionByShort.set(opt.short, opt);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
let i = 0;
|
|
1570
|
+
while (i < tokens.length) {
|
|
1571
|
+
const token = tokens[i];
|
|
1572
|
+
const opt = token.type === 'long' ? optionByLong.get(token.name) : optionByShort.get(token.name);
|
|
1573
|
+
if (!opt) {
|
|
1574
|
+
i += 1;
|
|
1575
|
+
continue;
|
|
1576
|
+
}
|
|
1577
|
+
explicitOptionLongs.add(opt.long);
|
|
1578
|
+
if (opt.long === 'color') {
|
|
1579
|
+
sawColorToken = true;
|
|
1580
|
+
}
|
|
1581
|
+
const isNegativeToken = token.original.toLowerCase().startsWith('--no-');
|
|
1582
|
+
if (isNegativeToken && !(opt.type === 'boolean' && opt.args === 'none')) {
|
|
1583
|
+
throw new CommanderError('NegativeOptionType', `"--no-${camelToKebabCase$2(opt.long)}" can only be used with boolean options`, commandPath);
|
|
1584
|
+
}
|
|
1585
|
+
if (opt.type === 'boolean' && opt.args === 'none') {
|
|
1586
|
+
const eqIdx = token.resolved.indexOf('=');
|
|
1587
|
+
if (eqIdx !== -1) {
|
|
1588
|
+
const value = token.resolved.slice(eqIdx + 1);
|
|
1589
|
+
if (value === 'true') {
|
|
1590
|
+
opts[opt.long] = true;
|
|
1591
|
+
}
|
|
1592
|
+
else if (value === 'false') {
|
|
1593
|
+
opts[opt.long] = false;
|
|
1594
|
+
}
|
|
1595
|
+
else {
|
|
1596
|
+
throw new CommanderError('InvalidBooleanValue', `invalid value "${value}" for boolean option "--${camelToKebabCase$2(opt.long)}". Use "true" or "false"`, commandPath);
|
|
1597
|
+
}
|
|
1598
|
+
}
|
|
1599
|
+
else {
|
|
1600
|
+
opts[opt.long] = true;
|
|
1601
|
+
}
|
|
1602
|
+
i += 1;
|
|
1603
|
+
continue;
|
|
1604
|
+
}
|
|
1605
|
+
if (opt.args === 'required') {
|
|
1606
|
+
const eqIdx = token.resolved.indexOf('=');
|
|
1607
|
+
let rawValue;
|
|
1608
|
+
if (eqIdx !== -1) {
|
|
1609
|
+
rawValue = token.resolved.slice(eqIdx + 1);
|
|
1610
|
+
}
|
|
1611
|
+
else if (i + 1 < tokens.length && tokens[i + 1].type === 'none') {
|
|
1612
|
+
rawValue = tokens[i + 1].original;
|
|
1613
|
+
i += 1;
|
|
1614
|
+
}
|
|
1615
|
+
else {
|
|
1616
|
+
throw new CommanderError('MissingValue', `option "--${camelToKebabCase$2(opt.long)}" requires a value`, commandPath);
|
|
1617
|
+
}
|
|
1618
|
+
opts[opt.long] = this.#convertValue(opt, rawValue, commandPath);
|
|
1619
|
+
i += 1;
|
|
1620
|
+
continue;
|
|
1621
|
+
}
|
|
1622
|
+
if (opt.args === 'optional') {
|
|
1623
|
+
const eqIdx = token.resolved.indexOf('=');
|
|
1624
|
+
if (eqIdx !== -1) {
|
|
1625
|
+
opts[opt.long] = this.#convertValue(opt, token.resolved.slice(eqIdx + 1), commandPath);
|
|
1626
|
+
i += 1;
|
|
1627
|
+
continue;
|
|
1628
|
+
}
|
|
1629
|
+
if (i + 1 < tokens.length && tokens[i + 1].type === 'none') {
|
|
1630
|
+
opts[opt.long] = this.#convertValue(opt, tokens[i + 1].original, commandPath);
|
|
1631
|
+
i += 1;
|
|
1632
|
+
}
|
|
1633
|
+
else {
|
|
1634
|
+
opts[opt.long] = undefined;
|
|
1635
|
+
}
|
|
1636
|
+
i += 1;
|
|
965
1637
|
continue;
|
|
966
1638
|
}
|
|
967
|
-
if (
|
|
968
|
-
|
|
1639
|
+
if (opt.args === 'variadic') {
|
|
1640
|
+
const values = Array.isArray(opts[opt.long]) ? opts[opt.long] : [];
|
|
1641
|
+
const eqIdx = token.resolved.indexOf('=');
|
|
1642
|
+
if (eqIdx !== -1) {
|
|
1643
|
+
values.push(this.#convertValue(opt, token.resolved.slice(eqIdx + 1), commandPath));
|
|
1644
|
+
}
|
|
1645
|
+
else {
|
|
1646
|
+
while (i + 1 < tokens.length && tokens[i + 1].type === 'none') {
|
|
1647
|
+
i += 1;
|
|
1648
|
+
values.push(this.#convertValue(opt, tokens[i].original, commandPath));
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
opts[opt.long] = values;
|
|
1652
|
+
i += 1;
|
|
969
1653
|
continue;
|
|
970
1654
|
}
|
|
971
|
-
|
|
1655
|
+
i += 1;
|
|
1656
|
+
}
|
|
1657
|
+
for (const opt of allOptions) {
|
|
1658
|
+
if (opt.required && !Object.prototype.hasOwnProperty.call(opts, opt.long)) {
|
|
1659
|
+
throw new CommanderError('MissingRequired', `missing required option "--${camelToKebabCase$2(opt.long)}"`, commandPath);
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
for (const opt of allOptions) {
|
|
1663
|
+
if (opt.choices && opts[opt.long] !== undefined) {
|
|
1664
|
+
const value = opts[opt.long];
|
|
1665
|
+
const values = Array.isArray(value) ? value : [value];
|
|
1666
|
+
const choices = opt.choices;
|
|
1667
|
+
for (const v of values) {
|
|
1668
|
+
if (!choices.includes(v)) {
|
|
1669
|
+
throw new CommanderError('InvalidChoice', `invalid value "${v}" for option "--${camelToKebabCase$2(opt.long)}". Allowed: ${opt.choices.join(', ')}`, commandPath);
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
if (isNoColorEnabled(envs) && !sawColorToken && opts['color'] === true) {
|
|
1675
|
+
opts['color'] = false;
|
|
972
1676
|
}
|
|
973
|
-
const remaining = separatorIndex === -1
|
|
974
|
-
? remainingBeforeSeparator
|
|
975
|
-
: [...remainingBeforeSeparator, '--', ...afterSeparator];
|
|
976
|
-
return {
|
|
977
|
-
controls,
|
|
978
|
-
remaining,
|
|
979
|
-
helpTarget,
|
|
980
|
-
};
|
|
981
|
-
}
|
|
982
|
-
#createContext(params) {
|
|
983
|
-
const { chain, cmds, envs, reporter } = params;
|
|
984
|
-
const leafCommand = chain[chain.length - 1];
|
|
985
|
-
const envSnapshot = { ...envs };
|
|
986
1677
|
return {
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
envs: envSnapshot,
|
|
990
|
-
controls: { help: false, version: false },
|
|
991
|
-
sources: {
|
|
992
|
-
preset: {
|
|
993
|
-
argv: [],
|
|
994
|
-
envs: {},
|
|
995
|
-
},
|
|
996
|
-
user: {
|
|
997
|
-
cmds: [...cmds],
|
|
998
|
-
argv: [],
|
|
999
|
-
envs: envSnapshot,
|
|
1000
|
-
},
|
|
1001
|
-
},
|
|
1002
|
-
reporter: reporter ?? this.#reporter ?? new Reporter(),
|
|
1678
|
+
opts,
|
|
1679
|
+
explicitOptionLongs,
|
|
1003
1680
|
};
|
|
1004
1681
|
}
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1682
|
+
applyBuiltinDevmodeLogLevel(params) {
|
|
1683
|
+
const { opts, explicitOptionLongs, builtinOption, hasUserOption } = params;
|
|
1684
|
+
const hasBuiltinDevmode = builtinOption.devmode;
|
|
1685
|
+
const hasBuiltinLogLevel = builtinOption.logLevel && !hasUserOption('logLevel');
|
|
1686
|
+
if (!hasBuiltinDevmode || !hasBuiltinLogLevel) {
|
|
1687
|
+
return;
|
|
1688
|
+
}
|
|
1689
|
+
if (opts['devmode'] !== true) {
|
|
1690
|
+
return;
|
|
1008
1691
|
}
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
return leafCommand;
|
|
1692
|
+
if (explicitOptionLongs.has('logLevel')) {
|
|
1693
|
+
return;
|
|
1012
1694
|
}
|
|
1013
|
-
|
|
1695
|
+
opts['logLevel'] = 'debug';
|
|
1014
1696
|
}
|
|
1015
|
-
|
|
1016
|
-
const
|
|
1017
|
-
const
|
|
1018
|
-
|
|
1019
|
-
const afterSeparator = separatorIndex === -1 ? [] : controlTailArgv.slice(separatorIndex + 1);
|
|
1020
|
-
const profileScanResult = this.#scanPresetProfileDirectives(beforeSeparator, commandPath);
|
|
1021
|
-
const cleanArgv = separatorIndex === -1
|
|
1022
|
-
? profileScanResult.cleanArgv
|
|
1023
|
-
: [...profileScanResult.cleanArgv, '--', ...afterSeparator];
|
|
1024
|
-
const commandChain = ctx.chain;
|
|
1025
|
-
const commandPresetFile = this.#resolveCommandPresetFileFromChain(commandChain);
|
|
1026
|
-
const effectivePresetFile = profileScanResult.presetFile ?? commandPresetFile;
|
|
1027
|
-
const commandPresetProfile = this.#resolveCommandPresetProfileFromChain(commandChain);
|
|
1028
|
-
const useCommandPresetProfile = profileScanResult.presetProfile === undefined && commandPresetProfile !== undefined;
|
|
1029
|
-
if (useCommandPresetProfile) {
|
|
1030
|
-
this.#assertPresetProfileSelectorValue(commandPresetProfile, 'command.preset.profile', commandPath);
|
|
1031
|
-
}
|
|
1032
|
-
const effectivePresetProfile = profileScanResult.presetProfile ?? commandPresetProfile;
|
|
1033
|
-
const effectivePresetProfileSourceName = profileScanResult.presetProfile !== undefined
|
|
1034
|
-
? PRESET_PROFILE_FLAG
|
|
1035
|
-
: commandPresetProfile !== undefined
|
|
1036
|
-
? 'command.preset.profile'
|
|
1037
|
-
: undefined;
|
|
1038
|
-
if (effectivePresetFile === undefined && useCommandPresetProfile) {
|
|
1039
|
-
throw new CommanderError('ConfigurationError', 'cannot use "command.preset.profile" without "command.preset.file" or "--preset-file"', commandPath);
|
|
1040
|
-
}
|
|
1041
|
-
const resolvedProfile = await this.#resolvePresetProfile({
|
|
1042
|
-
presetFile: effectivePresetFile,
|
|
1043
|
-
presetProfile: effectivePresetProfile,
|
|
1044
|
-
presetProfileSourceName: effectivePresetProfileSourceName,
|
|
1045
|
-
commandPath,
|
|
1046
|
-
});
|
|
1047
|
-
const userSources = {
|
|
1048
|
-
cmds: [...ctx.sources.user.cmds],
|
|
1049
|
-
argv: [...cleanArgv],
|
|
1050
|
-
envs: { ...ctx.sources.user.envs },
|
|
1697
|
+
resolveBuiltinParsedOptions(params) {
|
|
1698
|
+
const { opts, builtinOption, hasUserOption } = params;
|
|
1699
|
+
const builtin = {
|
|
1700
|
+
devmode: builtinOption.devmode ? Boolean(opts['devmode']) : false,
|
|
1051
1701
|
};
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
this.#validatePresetOptionTokens(resolvedProfile.optsArgv, resolvedProfile.optsSourceLabel, commandPath);
|
|
1055
|
-
this.#assertPresetOptionFragments(resolvedProfile.optsArgv, resolvedProfile.optsSourceLabel, commandChain, optionPolicyMap);
|
|
1056
|
-
presetArgv.push(...resolvedProfile.optsArgv);
|
|
1057
|
-
}
|
|
1058
|
-
const presetEnvs = {};
|
|
1059
|
-
if (resolvedProfile !== undefined) {
|
|
1060
|
-
if (resolvedProfile.profileEnvFileSource !== undefined) {
|
|
1061
|
-
const content = await this.#readPresetFile(resolvedProfile.profileEnvFileSource, commandPath);
|
|
1062
|
-
if (content !== undefined) {
|
|
1063
|
-
const parsed = this.#parsePresetEnvsContent(content, resolvedProfile.profileEnvFileSource, commandPath);
|
|
1064
|
-
Object.assign(presetEnvs, parsed);
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
Object.assign(presetEnvs, resolvedProfile.profileInlineEnvs);
|
|
1068
|
-
if (resolvedProfile.variantEnvFileSource !== undefined) {
|
|
1069
|
-
const content = await this.#readPresetFile(resolvedProfile.variantEnvFileSource, commandPath);
|
|
1070
|
-
if (content !== undefined) {
|
|
1071
|
-
const parsed = this.#parsePresetEnvsContent(content, resolvedProfile.variantEnvFileSource, commandPath);
|
|
1072
|
-
Object.assign(presetEnvs, parsed);
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
Object.assign(presetEnvs, resolvedProfile.variantInlineEnvs);
|
|
1702
|
+
if (builtinOption.color && !hasUserOption('color')) {
|
|
1703
|
+
builtin.color = Boolean(opts['color']);
|
|
1076
1704
|
}
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
envs: presetEnvs,
|
|
1082
|
-
},
|
|
1083
|
-
};
|
|
1084
|
-
const envs = { ...sources.user.envs, ...sources.preset.envs };
|
|
1085
|
-
const tailArgv = [...sources.preset.argv, ...sources.user.argv];
|
|
1086
|
-
return { tailArgv, envs, sources };
|
|
1087
|
-
}
|
|
1088
|
-
#resolveCommandPresetFileFromChain(chain) {
|
|
1089
|
-
for (let index = chain.length - 1; index >= 0; index -= 1) {
|
|
1090
|
-
const preset = chain[index].#presetConfig;
|
|
1091
|
-
if (preset?.file !== undefined) {
|
|
1092
|
-
return preset.file;
|
|
1705
|
+
if (builtinOption.logLevel && !hasUserOption('logLevel')) {
|
|
1706
|
+
const logLevel = opts['logLevel'];
|
|
1707
|
+
if (typeof logLevel === 'string') {
|
|
1708
|
+
builtin.logLevel = logLevel;
|
|
1093
1709
|
}
|
|
1094
1710
|
}
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
#resolveCommandPresetProfileFromChain(chain) {
|
|
1098
|
-
for (let index = chain.length - 1; index >= 0; index -= 1) {
|
|
1099
|
-
const preset = chain[index].#presetConfig;
|
|
1100
|
-
if (preset?.profile !== undefined) {
|
|
1101
|
-
return preset.profile;
|
|
1102
|
-
}
|
|
1711
|
+
if (builtinOption.silent && !hasUserOption('silent')) {
|
|
1712
|
+
builtin.silent = Boolean(opts['silent']);
|
|
1103
1713
|
}
|
|
1104
|
-
|
|
1714
|
+
if (builtinOption.logDate && !hasUserOption('logDate')) {
|
|
1715
|
+
builtin.logDate = Boolean(opts['logDate']);
|
|
1716
|
+
}
|
|
1717
|
+
if (builtinOption.logColorful && !hasUserOption('logColorful')) {
|
|
1718
|
+
builtin.logColorful = Boolean(opts['logColorful']);
|
|
1719
|
+
}
|
|
1720
|
+
return builtin;
|
|
1105
1721
|
}
|
|
1106
|
-
#
|
|
1107
|
-
if (
|
|
1108
|
-
return
|
|
1722
|
+
#convertValue(opt, rawValue, commandPath) {
|
|
1723
|
+
if (opt.coerce) {
|
|
1724
|
+
return opt.coerce(rawValue);
|
|
1725
|
+
}
|
|
1726
|
+
if (opt.type === 'number') {
|
|
1727
|
+
const num = parsePrimitiveNumber(rawValue);
|
|
1728
|
+
if (num === undefined) {
|
|
1729
|
+
throw new CommanderError('InvalidType', `invalid number "${rawValue}" for option "--${camelToKebabCase$2(opt.long)}"`, commandPath);
|
|
1730
|
+
}
|
|
1731
|
+
return num;
|
|
1109
1732
|
}
|
|
1110
|
-
return
|
|
1733
|
+
return rawValue;
|
|
1111
1734
|
}
|
|
1112
|
-
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
const PRESET_FILE_FLAG = '--preset-file';
|
|
1738
|
+
const PRESET_PROFILE_FLAG = '--preset-profile';
|
|
1739
|
+
const PRESET_SELECTOR_DELIMITER = ':';
|
|
1740
|
+
const PRESET_PROFILE_NAME_REGEX = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
1741
|
+
const PRESET_VARIANT_NAME_REGEX = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
1742
|
+
function kebabToCamelCase$1(str) {
|
|
1743
|
+
return str.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
1744
|
+
}
|
|
1745
|
+
function camelToKebabCase$1(str) {
|
|
1746
|
+
return str.replace(/[A-Z]/g, m => '-' + m.toLowerCase());
|
|
1747
|
+
}
|
|
1748
|
+
class CommandPresetProfileParser {
|
|
1749
|
+
#resolvePresetFileAbsolutePath;
|
|
1750
|
+
#resolvePath;
|
|
1751
|
+
#readPresetFile;
|
|
1752
|
+
constructor(params) {
|
|
1753
|
+
this.#resolvePresetFileAbsolutePath = params.resolvePresetFileAbsolutePath;
|
|
1754
|
+
this.#resolvePath = params.resolvePath;
|
|
1755
|
+
this.#readPresetFile = params.readPresetFile;
|
|
1756
|
+
}
|
|
1757
|
+
async resolvePresetProfile(params) {
|
|
1113
1758
|
const { presetFile, presetProfile, presetProfileSourceName, commandPath } = params;
|
|
1114
1759
|
if (presetFile === undefined) {
|
|
1115
1760
|
if (presetProfile !== undefined) {
|
|
@@ -1152,9 +1797,9 @@ class Command {
|
|
|
1152
1797
|
: `${resolvedProfileName}${PRESET_SELECTOR_DELIMITER}${selectedVariantName}`;
|
|
1153
1798
|
const mergedOpts = { ...(profile.opts ?? {}), ...(selectedVariant?.opts ?? {}) };
|
|
1154
1799
|
const optsArgv = this.#buildPresetArgvFromProfileOptions(mergedOpts, profileSelectorLabel, commandPath);
|
|
1155
|
-
const profileInlineEnvs = this.#normalizePresetProfileEnvs(profile.envs
|
|
1156
|
-
const variantInlineEnvs = this.#normalizePresetProfileEnvs(selectedVariant?.envs
|
|
1157
|
-
const profileDir = this.#
|
|
1800
|
+
const profileInlineEnvs = this.#normalizePresetProfileEnvs(profile.envs);
|
|
1801
|
+
const variantInlineEnvs = this.#normalizePresetProfileEnvs(selectedVariant?.envs);
|
|
1802
|
+
const profileDir = this.#resolvePath(profileFile.absolutePath, '..');
|
|
1158
1803
|
let profileEnvFileSource;
|
|
1159
1804
|
if (profile.envFile !== undefined) {
|
|
1160
1805
|
profileEnvFileSource = {
|
|
@@ -1176,19 +1821,49 @@ class Command {
|
|
|
1176
1821
|
variantName: selectedVariantName,
|
|
1177
1822
|
optsArgv,
|
|
1178
1823
|
optsSourceLabel: `${profileFile.displayPath}#${profileSelectorLabel}.opts`,
|
|
1824
|
+
issueMeta: {
|
|
1825
|
+
file: profileFile.displayPath,
|
|
1826
|
+
profile: resolvedProfileName,
|
|
1827
|
+
variant: selectedVariantName,
|
|
1828
|
+
},
|
|
1179
1829
|
profileInlineEnvs,
|
|
1180
1830
|
variantInlineEnvs,
|
|
1181
1831
|
profileEnvFileSource,
|
|
1182
1832
|
variantEnvFileSource,
|
|
1183
1833
|
};
|
|
1184
1834
|
}
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1835
|
+
assertPresetProfileSelectorValue(selector, sourceName, commandPath) {
|
|
1836
|
+
void this.#parsePresetProfileSelector(selector, sourceName, commandPath);
|
|
1837
|
+
}
|
|
1838
|
+
validatePresetOptionTokens(tokens, filepath, commandPath) {
|
|
1839
|
+
if (tokens.length === 0) {
|
|
1840
|
+
return;
|
|
1189
1841
|
}
|
|
1190
|
-
|
|
1191
|
-
throw new CommanderError('ConfigurationError', `
|
|
1842
|
+
if (!tokens[0].startsWith('-')) {
|
|
1843
|
+
throw new CommanderError('ConfigurationError', `invalid preset options in "${filepath}": bare token "${tokens[0]}" cannot appear before any option token`, commandPath);
|
|
1844
|
+
}
|
|
1845
|
+
for (const token of tokens) {
|
|
1846
|
+
if (token === '--') {
|
|
1847
|
+
throw new CommanderError('ConfigurationError', `invalid preset options in "${filepath}": "--" is not allowed`, commandPath);
|
|
1848
|
+
}
|
|
1849
|
+
if (token === 'help' || token === '--help' || token === '--version') {
|
|
1850
|
+
throw new CommanderError('ConfigurationError', `invalid preset options in "${filepath}": control token "${token}" is not allowed`, commandPath);
|
|
1851
|
+
}
|
|
1852
|
+
if (token === PRESET_FILE_FLAG ||
|
|
1853
|
+
token.startsWith(`${PRESET_FILE_FLAG}=`) ||
|
|
1854
|
+
token === PRESET_PROFILE_FLAG ||
|
|
1855
|
+
token.startsWith(`${PRESET_PROFILE_FLAG}=`)) {
|
|
1856
|
+
throw new CommanderError('ConfigurationError', `invalid preset options in "${filepath}": preset directive "${token}" is not allowed`, commandPath);
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
#parsePresetProfileManifest(content, filepath, commandPath) {
|
|
1861
|
+
let parsed;
|
|
1862
|
+
try {
|
|
1863
|
+
parsed = JSON.parse(content);
|
|
1864
|
+
}
|
|
1865
|
+
catch (error) {
|
|
1866
|
+
throw new CommanderError('ConfigurationError', `failed to parse preset file "${filepath}": ${error.message}`, commandPath);
|
|
1192
1867
|
}
|
|
1193
1868
|
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
|
1194
1869
|
throw new CommanderError('ConfigurationError', `invalid preset file "${filepath}": root must be an object`, commandPath);
|
|
@@ -1208,7 +1883,7 @@ class Command {
|
|
|
1208
1883
|
if (typeof defaultsRecord.profile !== 'string') {
|
|
1209
1884
|
throw new CommanderError('ConfigurationError', `invalid preset file "${filepath}": "defaults.profile" must be a string`, commandPath);
|
|
1210
1885
|
}
|
|
1211
|
-
this
|
|
1886
|
+
this.assertPresetProfileSelectorValue(defaultsRecord.profile, 'defaults.profile', commandPath);
|
|
1212
1887
|
}
|
|
1213
1888
|
defaults = { profile: defaultsRecord.profile };
|
|
1214
1889
|
}
|
|
@@ -1364,7 +2039,7 @@ class Command {
|
|
|
1364
2039
|
}
|
|
1365
2040
|
throw new CommanderError('ConfigurationError', `${valueLabel} must be boolean|string|number|(string|number)[]`, commandPath);
|
|
1366
2041
|
}
|
|
1367
|
-
#normalizePresetProfileEnvs(envs
|
|
2042
|
+
#normalizePresetProfileEnvs(envs) {
|
|
1368
2043
|
return envs === undefined ? {} : { ...envs };
|
|
1369
2044
|
}
|
|
1370
2045
|
#normalizePresetOptionName(rawName, profileName, commandPath) {
|
|
@@ -1381,7 +2056,7 @@ class Command {
|
|
|
1381
2056
|
if (!/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/.test(lowered)) {
|
|
1382
2057
|
throw new CommanderError('ConfigurationError', `invalid option name "${rawName}" in preset profile "${profileName}"`, commandPath);
|
|
1383
2058
|
}
|
|
1384
|
-
return kebabToCamelCase(lowered);
|
|
2059
|
+
return kebabToCamelCase$1(lowered);
|
|
1385
2060
|
}
|
|
1386
2061
|
if (!/^[a-z][a-zA-Z0-9]*$/.test(stripped)) {
|
|
1387
2062
|
throw new CommanderError('ConfigurationError', `invalid option name "${rawName}" in preset profile "${profileName}"`, commandPath);
|
|
@@ -1392,7 +2067,7 @@ class Command {
|
|
|
1392
2067
|
const argv = [];
|
|
1393
2068
|
for (const [rawName, rawValue] of Object.entries(opts)) {
|
|
1394
2069
|
const optionName = this.#normalizePresetOptionName(rawName, profileName, commandPath);
|
|
1395
|
-
const kebabName = camelToKebabCase(optionName);
|
|
2070
|
+
const kebabName = camelToKebabCase$1(optionName);
|
|
1396
2071
|
const positiveFlag = `--${kebabName}`;
|
|
1397
2072
|
const negativeFlag = `--no-${kebabName}`;
|
|
1398
2073
|
if (typeof rawValue === 'boolean') {
|
|
@@ -1414,80 +2089,6 @@ class Command {
|
|
|
1414
2089
|
}
|
|
1415
2090
|
return argv;
|
|
1416
2091
|
}
|
|
1417
|
-
#scanPresetProfileDirectives(argv, commandPath) {
|
|
1418
|
-
const cleanArgv = [];
|
|
1419
|
-
let presetFile;
|
|
1420
|
-
let presetProfile;
|
|
1421
|
-
const assignDirective = (flag, value) => {
|
|
1422
|
-
if (flag === PRESET_FILE_FLAG) {
|
|
1423
|
-
presetFile = value;
|
|
1424
|
-
}
|
|
1425
|
-
else {
|
|
1426
|
-
this.#assertPresetProfileSelectorValue(value, PRESET_PROFILE_FLAG, commandPath);
|
|
1427
|
-
presetProfile = value;
|
|
1428
|
-
}
|
|
1429
|
-
};
|
|
1430
|
-
let index = 0;
|
|
1431
|
-
while (index < argv.length) {
|
|
1432
|
-
const token = argv[index];
|
|
1433
|
-
if (token === PRESET_FILE_FLAG || token === PRESET_PROFILE_FLAG) {
|
|
1434
|
-
const value = argv[index + 1];
|
|
1435
|
-
if (value === undefined || value.length === 0) {
|
|
1436
|
-
throw new CommanderError('ConfigurationError', `missing value for "${token}"`, commandPath);
|
|
1437
|
-
}
|
|
1438
|
-
assignDirective(token, value);
|
|
1439
|
-
index += 2;
|
|
1440
|
-
continue;
|
|
1441
|
-
}
|
|
1442
|
-
if (token.startsWith(`${PRESET_FILE_FLAG}=`)) {
|
|
1443
|
-
const value = token.slice(PRESET_FILE_FLAG.length + 1);
|
|
1444
|
-
if (value.length === 0) {
|
|
1445
|
-
throw new CommanderError('ConfigurationError', `missing value for "${PRESET_FILE_FLAG}"`, commandPath);
|
|
1446
|
-
}
|
|
1447
|
-
assignDirective(PRESET_FILE_FLAG, value);
|
|
1448
|
-
index += 1;
|
|
1449
|
-
continue;
|
|
1450
|
-
}
|
|
1451
|
-
if (token.startsWith(`${PRESET_PROFILE_FLAG}=`)) {
|
|
1452
|
-
const value = token.slice(PRESET_PROFILE_FLAG.length + 1);
|
|
1453
|
-
if (value.length === 0) {
|
|
1454
|
-
throw new CommanderError('ConfigurationError', `missing value for "${PRESET_PROFILE_FLAG}"`, commandPath);
|
|
1455
|
-
}
|
|
1456
|
-
assignDirective(PRESET_PROFILE_FLAG, value);
|
|
1457
|
-
index += 1;
|
|
1458
|
-
continue;
|
|
1459
|
-
}
|
|
1460
|
-
cleanArgv.push(token);
|
|
1461
|
-
index += 1;
|
|
1462
|
-
}
|
|
1463
|
-
return { cleanArgv, presetFile, presetProfile };
|
|
1464
|
-
}
|
|
1465
|
-
#assertPresetOptionFragments(tokens, filepath, chain, optionPolicyMap) {
|
|
1466
|
-
if (tokens.length === 0) {
|
|
1467
|
-
return;
|
|
1468
|
-
}
|
|
1469
|
-
const commandPath = chain[chain.length - 1].#getCommandPath();
|
|
1470
|
-
try {
|
|
1471
|
-
const { optionTokens, restArgs } = tokenize(tokens, commandPath);
|
|
1472
|
-
void restArgs;
|
|
1473
|
-
const { argTokens } = this.#resolve(chain, optionTokens, optionPolicyMap);
|
|
1474
|
-
if (argTokens.length > 0) {
|
|
1475
|
-
throw new CommanderError('ConfigurationError', `invalid preset options in "${filepath}": token "${argTokens[0].original}" cannot be resolved as an option fragment`, commandPath);
|
|
1476
|
-
}
|
|
1477
|
-
}
|
|
1478
|
-
catch (error) {
|
|
1479
|
-
if (error instanceof CommanderError) {
|
|
1480
|
-
if (error.kind === 'ConfigurationError') {
|
|
1481
|
-
throw error;
|
|
1482
|
-
}
|
|
1483
|
-
throw new CommanderError('ConfigurationError', `invalid preset options in "${filepath}": ${error.message}`, commandPath);
|
|
1484
|
-
}
|
|
1485
|
-
throw error;
|
|
1486
|
-
}
|
|
1487
|
-
}
|
|
1488
|
-
#assertPresetProfileSelectorValue(selector, sourceName, commandPath) {
|
|
1489
|
-
void this.#parsePresetProfileSelector(selector, sourceName, commandPath);
|
|
1490
|
-
}
|
|
1491
2092
|
#parsePresetProfileSelector(selector, sourceName, commandPath) {
|
|
1492
2093
|
const normalizedSelector = selector.trim();
|
|
1493
2094
|
const separatorIndex = normalizedSelector.indexOf(PRESET_SELECTOR_DELIMITER);
|
|
@@ -1519,713 +2120,1374 @@ class Command {
|
|
|
1519
2120
|
}
|
|
1520
2121
|
throw new CommanderError('ConfigurationError', `invalid variant name for "${sourceName}": "${variantName}" (must match ${PRESET_VARIANT_NAME_REGEX.source})`, commandPath);
|
|
1521
2122
|
}
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
2123
|
+
}
|
|
2124
|
+
|
|
2125
|
+
const BUILTIN_LOG_LEVELS = ['debug', 'info', 'hint', 'warn', 'error'];
|
|
2126
|
+
function resolveReporterLogLevel(raw) {
|
|
2127
|
+
const normalized = raw.trim().toLowerCase();
|
|
2128
|
+
return BUILTIN_LOG_LEVELS.find(level => level === normalized);
|
|
2129
|
+
}
|
|
2130
|
+
function setReporterLevel(ctx, level) {
|
|
2131
|
+
const reporter = ctx.reporter;
|
|
2132
|
+
reporter?.setLevel?.(level);
|
|
2133
|
+
}
|
|
2134
|
+
function setReporterFlight(ctx, flight) {
|
|
2135
|
+
const reporter = ctx.reporter;
|
|
2136
|
+
reporter?.setFlight?.(flight);
|
|
2137
|
+
}
|
|
2138
|
+
const devmodeOption = {
|
|
2139
|
+
long: 'devmode',
|
|
2140
|
+
type: 'boolean',
|
|
2141
|
+
args: 'none',
|
|
2142
|
+
desc: 'Enable development mode',
|
|
2143
|
+
default: false,
|
|
2144
|
+
};
|
|
2145
|
+
const logLevelOption = {
|
|
2146
|
+
long: 'logLevel',
|
|
2147
|
+
type: 'string',
|
|
2148
|
+
args: 'required',
|
|
2149
|
+
desc: 'Set log level',
|
|
2150
|
+
default: 'info',
|
|
2151
|
+
choices: [...BUILTIN_LOG_LEVELS],
|
|
2152
|
+
coerce: (raw) => {
|
|
2153
|
+
const level = resolveReporterLogLevel(raw);
|
|
2154
|
+
if (level === undefined) {
|
|
2155
|
+
throw new Error(`Invalid log level: ${raw}`);
|
|
1529
2156
|
}
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
2157
|
+
return level;
|
|
2158
|
+
},
|
|
2159
|
+
apply: (value, ctx) => {
|
|
2160
|
+
setReporterLevel(ctx, value);
|
|
2161
|
+
},
|
|
2162
|
+
};
|
|
2163
|
+
const logDateOption = {
|
|
2164
|
+
long: 'logDate',
|
|
2165
|
+
type: 'boolean',
|
|
2166
|
+
args: 'none',
|
|
2167
|
+
desc: 'Enable log timestamp',
|
|
2168
|
+
default: true,
|
|
2169
|
+
apply: (value, ctx) => {
|
|
2170
|
+
setReporterFlight(ctx, { date: Boolean(value) });
|
|
2171
|
+
},
|
|
2172
|
+
};
|
|
2173
|
+
const logColorfulOption = {
|
|
2174
|
+
long: 'logColorful',
|
|
2175
|
+
type: 'boolean',
|
|
2176
|
+
args: 'none',
|
|
2177
|
+
desc: 'Enable colorful log output',
|
|
2178
|
+
default: true,
|
|
2179
|
+
apply: (value, ctx) => {
|
|
2180
|
+
setReporterFlight(ctx, { color: Boolean(value) });
|
|
2181
|
+
},
|
|
2182
|
+
};
|
|
2183
|
+
const silentOption = {
|
|
2184
|
+
long: 'silent',
|
|
2185
|
+
type: 'boolean',
|
|
2186
|
+
args: 'none',
|
|
2187
|
+
desc: 'Suppress non-error output',
|
|
2188
|
+
default: false,
|
|
2189
|
+
apply: (value, ctx) => {
|
|
2190
|
+
if (value) {
|
|
2191
|
+
setReporterLevel(ctx, 'error');
|
|
1536
2192
|
}
|
|
2193
|
+
},
|
|
2194
|
+
};
|
|
2195
|
+
|
|
2196
|
+
const BUILTIN_COLOR_OPTION = {
|
|
2197
|
+
long: 'color',
|
|
2198
|
+
type: 'boolean',
|
|
2199
|
+
args: 'none',
|
|
2200
|
+
desc: 'Enable colored help output',
|
|
2201
|
+
default: true,
|
|
2202
|
+
};
|
|
2203
|
+
function resolveOptionPolicy(params) {
|
|
2204
|
+
const { builtinOption, localOptions } = params;
|
|
2205
|
+
const optionMap = new Map();
|
|
2206
|
+
const hasUserOption = (long) => localOptions.some(option => option.long === long);
|
|
2207
|
+
if (builtinOption.color && !hasUserOption('color')) {
|
|
2208
|
+
optionMap.set('color', BUILTIN_COLOR_OPTION);
|
|
1537
2209
|
}
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
return parse(content);
|
|
1541
|
-
}
|
|
1542
|
-
catch (error) {
|
|
1543
|
-
throw new CommanderError('ConfigurationError', `failed to parse preset env file "${file.displayPath}": ${error.message}`, commandPath);
|
|
1544
|
-
}
|
|
2210
|
+
if (builtinOption.devmode) {
|
|
2211
|
+
optionMap.set('devmode', devmodeOption);
|
|
1545
2212
|
}
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
2213
|
+
if (builtinOption.logLevel && !hasUserOption('logLevel')) {
|
|
2214
|
+
optionMap.set('logLevel', logLevelOption);
|
|
2215
|
+
}
|
|
2216
|
+
if (builtinOption.silent && !hasUserOption('silent')) {
|
|
2217
|
+
optionMap.set('silent', silentOption);
|
|
2218
|
+
}
|
|
2219
|
+
if (builtinOption.logDate && !hasUserOption('logDate')) {
|
|
2220
|
+
optionMap.set('logDate', logDateOption);
|
|
2221
|
+
}
|
|
2222
|
+
if (builtinOption.logColorful && !hasUserOption('logColorful')) {
|
|
2223
|
+
optionMap.set('logColorful', logColorfulOption);
|
|
2224
|
+
}
|
|
2225
|
+
for (const option of localOptions) {
|
|
2226
|
+
optionMap.set(option.long, option);
|
|
2227
|
+
}
|
|
2228
|
+
return {
|
|
2229
|
+
mergedOptions: Array.from(optionMap.values()),
|
|
2230
|
+
};
|
|
2231
|
+
}
|
|
2232
|
+
function buildOptionPolicyMap(params) {
|
|
2233
|
+
const { chain, resolveOptionPolicy } = params;
|
|
2234
|
+
const optionPolicyMap = new Map();
|
|
2235
|
+
for (const command of chain) {
|
|
2236
|
+
optionPolicyMap.set(command, resolveOptionPolicy(command));
|
|
2237
|
+
}
|
|
2238
|
+
return optionPolicyMap;
|
|
2239
|
+
}
|
|
2240
|
+
function mustGetOptionPolicy(params) {
|
|
2241
|
+
const { optionPolicyMap, command, resolveOptionPolicy } = params;
|
|
2242
|
+
const policy = optionPolicyMap.get(command) ?? resolveOptionPolicy(command);
|
|
2243
|
+
optionPolicyMap.set(command, policy);
|
|
2244
|
+
return policy;
|
|
2245
|
+
}
|
|
2246
|
+
|
|
2247
|
+
function scanControl(params) {
|
|
2248
|
+
const { tailArgv, supportsBuiltinVersion } = params;
|
|
2249
|
+
const controls = { help: false, version: false };
|
|
2250
|
+
const separatorIndex = tailArgv.indexOf('--');
|
|
2251
|
+
const beforeSeparator = separatorIndex === -1 ? tailArgv : tailArgv.slice(0, separatorIndex);
|
|
2252
|
+
const afterSeparator = separatorIndex === -1 ? [] : tailArgv.slice(separatorIndex + 1);
|
|
2253
|
+
let helpTarget;
|
|
2254
|
+
let scanStartIndex = 0;
|
|
2255
|
+
if (beforeSeparator[0] === 'help') {
|
|
2256
|
+
controls.help = true;
|
|
2257
|
+
scanStartIndex = 1;
|
|
2258
|
+
const candidate = beforeSeparator[1];
|
|
2259
|
+
if (candidate !== undefined && !candidate.startsWith('-')) {
|
|
2260
|
+
helpTarget = candidate;
|
|
2261
|
+
scanStartIndex = 2;
|
|
2262
|
+
}
|
|
2263
|
+
}
|
|
2264
|
+
const remainingBeforeSeparator = [];
|
|
2265
|
+
for (let index = scanStartIndex; index < beforeSeparator.length; index += 1) {
|
|
2266
|
+
const token = beforeSeparator[index];
|
|
2267
|
+
if (token === '--help') {
|
|
2268
|
+
controls.help = true;
|
|
2269
|
+
continue;
|
|
1549
2270
|
}
|
|
1550
|
-
if (
|
|
1551
|
-
|
|
2271
|
+
if (token === '--version' && supportsBuiltinVersion) {
|
|
2272
|
+
controls.version = true;
|
|
2273
|
+
continue;
|
|
1552
2274
|
}
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
2275
|
+
remainingBeforeSeparator.push(token);
|
|
2276
|
+
}
|
|
2277
|
+
return {
|
|
2278
|
+
controls,
|
|
2279
|
+
remaining: separatorIndex === -1
|
|
2280
|
+
? remainingBeforeSeparator
|
|
2281
|
+
: [...remainingBeforeSeparator, '--', ...afterSeparator],
|
|
2282
|
+
helpTarget,
|
|
2283
|
+
};
|
|
2284
|
+
}
|
|
2285
|
+
function runControl(params) {
|
|
2286
|
+
const { leafCommand, controlScanResult, resolveHelpCommand, getCommandPath, getCommandVersion } = params;
|
|
2287
|
+
if (controlScanResult.controls.help) {
|
|
2288
|
+
const helpCommand = resolveHelpCommand(leafCommand, controlScanResult.helpTarget);
|
|
2289
|
+
return {
|
|
2290
|
+
kind: 'help',
|
|
2291
|
+
targetCommandPath: getCommandPath(helpCommand),
|
|
2292
|
+
};
|
|
2293
|
+
}
|
|
2294
|
+
if (controlScanResult.controls.version) {
|
|
2295
|
+
const version = getCommandVersion(leafCommand);
|
|
2296
|
+
if (version !== undefined) {
|
|
2297
|
+
return {
|
|
2298
|
+
kind: 'version',
|
|
2299
|
+
targetCommandPath: getCommandPath(leafCommand),
|
|
2300
|
+
version,
|
|
2301
|
+
};
|
|
1566
2302
|
}
|
|
1567
2303
|
}
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
}
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
}
|
|
1590
|
-
return { consumedTokens, argTokens };
|
|
1591
|
-
}
|
|
1592
|
-
#shift(tokens, shadowed, allOptions) {
|
|
1593
|
-
const effectiveOptions = allOptions.filter(o => !shadowed.has(o.long));
|
|
1594
|
-
const optionByLong = new Map();
|
|
1595
|
-
const optionByShort = new Map();
|
|
1596
|
-
for (const opt of effectiveOptions) {
|
|
1597
|
-
optionByLong.set(opt.long, opt);
|
|
1598
|
-
if (opt.short) {
|
|
1599
|
-
optionByShort.set(opt.short, opt);
|
|
1600
|
-
}
|
|
2304
|
+
return undefined;
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
function resolveStage(params) {
|
|
2308
|
+
const { chain, tokens, optionPolicyMap, mustGetOptionPolicy, getLocalOptions, getCommandPath, withUnknownOptionIssue, } = params;
|
|
2309
|
+
try {
|
|
2310
|
+
return resolveTokensByChain({
|
|
2311
|
+
chain,
|
|
2312
|
+
tokens,
|
|
2313
|
+
optionPolicyMap,
|
|
2314
|
+
mustGetOptionPolicy,
|
|
2315
|
+
getLocalOptions,
|
|
2316
|
+
getCommandPath,
|
|
2317
|
+
});
|
|
2318
|
+
}
|
|
2319
|
+
catch (error) {
|
|
2320
|
+
if (error instanceof CommanderError && error.kind === 'UnknownOption') {
|
|
2321
|
+
const unresolvedToken = error.meta?.token === undefined
|
|
2322
|
+
? undefined
|
|
2323
|
+
: tokens.find(token => token.original === error.meta?.token);
|
|
2324
|
+
throw withUnknownOptionIssue(error, unresolvedToken);
|
|
1601
2325
|
}
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
2326
|
+
throw error;
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
function shiftTokens(params) {
|
|
2330
|
+
const { tokens, shadowed, allOptions } = params;
|
|
2331
|
+
const effectiveOptions = allOptions.filter(option => !shadowed.has(option.long));
|
|
2332
|
+
const optionByLong = new Map();
|
|
2333
|
+
const optionByShort = new Map();
|
|
2334
|
+
for (const option of effectiveOptions) {
|
|
2335
|
+
optionByLong.set(option.long, option);
|
|
2336
|
+
if (option.short) {
|
|
2337
|
+
optionByShort.set(option.short, option);
|
|
2338
|
+
}
|
|
2339
|
+
}
|
|
2340
|
+
const consumed = [];
|
|
2341
|
+
const remaining = [];
|
|
2342
|
+
let index = 0;
|
|
2343
|
+
while (index < tokens.length) {
|
|
2344
|
+
const token = tokens[index];
|
|
2345
|
+
if (token.type === 'long') {
|
|
2346
|
+
const option = optionByLong.get(token.name);
|
|
2347
|
+
if (option !== undefined) {
|
|
2348
|
+
consumed.push(token);
|
|
2349
|
+
if (option.args === 'required') {
|
|
2350
|
+
if (!token.resolved.includes('=') && index + 1 < tokens.length) {
|
|
2351
|
+
index += 1;
|
|
2352
|
+
consumed.push(tokens[index]);
|
|
1616
2353
|
}
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
2354
|
+
}
|
|
2355
|
+
else if (option.args === 'optional') {
|
|
2356
|
+
if (!token.resolved.includes('=') &&
|
|
2357
|
+
index + 1 < tokens.length &&
|
|
2358
|
+
tokens[index + 1].type === 'none') {
|
|
2359
|
+
index += 1;
|
|
2360
|
+
consumed.push(tokens[index]);
|
|
1624
2361
|
}
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
}
|
|
1631
|
-
}
|
|
2362
|
+
}
|
|
2363
|
+
else if (option.args === 'variadic' && !token.resolved.includes('=')) {
|
|
2364
|
+
while (index + 1 < tokens.length && tokens[index + 1].type === 'none') {
|
|
2365
|
+
index += 1;
|
|
2366
|
+
consumed.push(tokens[index]);
|
|
1632
2367
|
}
|
|
1633
|
-
i += 1;
|
|
1634
|
-
continue;
|
|
1635
2368
|
}
|
|
1636
|
-
|
|
1637
|
-
i += 1;
|
|
2369
|
+
index += 1;
|
|
1638
2370
|
continue;
|
|
1639
2371
|
}
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
i += 1;
|
|
1653
|
-
consumed.push(tokens[i]);
|
|
1654
|
-
}
|
|
2372
|
+
remaining.push(token);
|
|
2373
|
+
index += 1;
|
|
2374
|
+
continue;
|
|
2375
|
+
}
|
|
2376
|
+
if (token.type === 'short') {
|
|
2377
|
+
const option = optionByShort.get(token.name);
|
|
2378
|
+
if (option !== undefined) {
|
|
2379
|
+
consumed.push(token);
|
|
2380
|
+
if (option.args === 'required' || option.args === 'optional') {
|
|
2381
|
+
if (index + 1 < tokens.length && tokens[index + 1].type === 'none') {
|
|
2382
|
+
index += 1;
|
|
2383
|
+
consumed.push(tokens[index]);
|
|
1655
2384
|
}
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
2385
|
+
}
|
|
2386
|
+
else if (option.args === 'variadic') {
|
|
2387
|
+
while (index + 1 < tokens.length && tokens[index + 1].type === 'none') {
|
|
2388
|
+
index += 1;
|
|
2389
|
+
consumed.push(tokens[index]);
|
|
1661
2390
|
}
|
|
1662
|
-
i += 1;
|
|
1663
|
-
continue;
|
|
1664
2391
|
}
|
|
1665
|
-
|
|
1666
|
-
i += 1;
|
|
2392
|
+
index += 1;
|
|
1667
2393
|
continue;
|
|
1668
2394
|
}
|
|
1669
2395
|
remaining.push(token);
|
|
1670
|
-
|
|
2396
|
+
index += 1;
|
|
2397
|
+
continue;
|
|
1671
2398
|
}
|
|
1672
|
-
|
|
2399
|
+
remaining.push(token);
|
|
2400
|
+
index += 1;
|
|
1673
2401
|
}
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
const
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
2402
|
+
return { consumed, remaining };
|
|
2403
|
+
}
|
|
2404
|
+
function resolveTokensByChain(params) {
|
|
2405
|
+
const { chain, tokens, optionPolicyMap, mustGetOptionPolicy, getLocalOptions, getCommandPath } = params;
|
|
2406
|
+
const consumedTokens = new Map();
|
|
2407
|
+
let remaining = [...tokens];
|
|
2408
|
+
const shadowed = new Set();
|
|
2409
|
+
for (let index = chain.length - 1; index >= 0; index -= 1) {
|
|
2410
|
+
const command = chain[index];
|
|
2411
|
+
const policy = mustGetOptionPolicy(optionPolicyMap, command);
|
|
2412
|
+
const result = shiftTokens({
|
|
2413
|
+
tokens: remaining,
|
|
2414
|
+
shadowed,
|
|
2415
|
+
allOptions: policy.mergedOptions,
|
|
2416
|
+
});
|
|
2417
|
+
consumedTokens.set(command, result.consumed);
|
|
2418
|
+
remaining = result.remaining;
|
|
2419
|
+
for (const option of getLocalOptions(command)) {
|
|
2420
|
+
shadowed.add(option.long);
|
|
2421
|
+
}
|
|
2422
|
+
}
|
|
2423
|
+
const leafCommand = chain[chain.length - 1];
|
|
2424
|
+
const leafCommandPath = getCommandPath(leafCommand);
|
|
2425
|
+
const argTokens = [];
|
|
2426
|
+
for (const token of remaining) {
|
|
2427
|
+
if (token.type !== 'none') {
|
|
2428
|
+
throw new CommanderError('UnknownOption', `unknown option "${token.original}" for command "${leafCommandPath}"`, leafCommandPath, {
|
|
2429
|
+
commandPath: leafCommandPath,
|
|
2430
|
+
token: token.original,
|
|
2431
|
+
issues: [],
|
|
2432
|
+
});
|
|
1696
2433
|
}
|
|
1697
|
-
|
|
1698
|
-
const rawArgStrings = [...argTokens.map(t => t.original), ...restArgs];
|
|
1699
|
-
const { args, rawArgs } = leafCommand.#parseArguments(rawArgStrings);
|
|
1700
|
-
const parseCtx = {
|
|
1701
|
-
...ctx,
|
|
1702
|
-
sources: this.#freezeInputSources(ctx.sources),
|
|
1703
|
-
};
|
|
1704
|
-
return { ctx: parseCtx, opts: leafLocalOpts, args, rawArgs };
|
|
2434
|
+
argTokens.push(token);
|
|
1705
2435
|
}
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
2436
|
+
return { consumedTokens, argTokens };
|
|
2437
|
+
}
|
|
2438
|
+
function validateMergedShortOptions(params) {
|
|
2439
|
+
const { chain, optionPolicyMap, mustGetOptionPolicy, rootCommandPath } = params;
|
|
2440
|
+
const mergedByLong = new Map();
|
|
2441
|
+
for (const command of chain) {
|
|
2442
|
+
const policy = mustGetOptionPolicy(optionPolicyMap, command);
|
|
2443
|
+
for (const option of policy.mergedOptions) {
|
|
2444
|
+
mergedByLong.set(option.long, option);
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
const shortMap = new Map();
|
|
2448
|
+
for (const option of mergedByLong.values()) {
|
|
2449
|
+
if (!option.short) {
|
|
2450
|
+
continue;
|
|
1719
2451
|
}
|
|
1720
|
-
const
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
optionByLong.set(opt.long, opt);
|
|
1724
|
-
if (opt.short) {
|
|
1725
|
-
optionByShort.set(opt.short, opt);
|
|
1726
|
-
}
|
|
2452
|
+
const existingLong = shortMap.get(option.short);
|
|
2453
|
+
if (existingLong !== undefined && existingLong !== option.long) {
|
|
2454
|
+
throw new CommanderError('OptionConflict', `short option "-${option.short}" conflicts with "--${existingLong}"`, rootCommandPath);
|
|
1727
2455
|
}
|
|
1728
|
-
|
|
1729
|
-
while (i < tokens.length) {
|
|
1730
|
-
const token = tokens[i];
|
|
1731
|
-
const opt = token.type === 'long' ? optionByLong.get(token.name) : optionByShort.get(token.name);
|
|
1732
|
-
if (!opt) {
|
|
1733
|
-
i += 1;
|
|
1734
|
-
continue;
|
|
1735
|
-
}
|
|
1736
|
-
if (opt.long === 'color') {
|
|
1737
|
-
sawColorToken = true;
|
|
1738
|
-
}
|
|
1739
|
-
const isNegativeToken = token.original.toLowerCase().startsWith('--no-');
|
|
1740
|
-
if (isNegativeToken && !(opt.type === 'boolean' && opt.args === 'none')) {
|
|
1741
|
-
throw new CommanderError('NegativeOptionType', `"--no-${camelToKebabCase(opt.long)}" can only be used with boolean options`, this.#getCommandPath());
|
|
1742
|
-
}
|
|
1743
|
-
if (opt.type === 'boolean' && opt.args === 'none') {
|
|
1744
|
-
const eqIdx = token.resolved.indexOf('=');
|
|
1745
|
-
if (eqIdx !== -1) {
|
|
1746
|
-
const value = token.resolved.slice(eqIdx + 1);
|
|
1747
|
-
if (value === 'true') {
|
|
1748
|
-
opts[opt.long] = true;
|
|
1749
|
-
}
|
|
1750
|
-
else if (value === 'false') {
|
|
1751
|
-
opts[opt.long] = false;
|
|
1752
|
-
}
|
|
1753
|
-
else {
|
|
1754
|
-
throw new CommanderError('InvalidBooleanValue', `invalid value "${value}" for boolean option "--${camelToKebabCase(opt.long)}". Use "true" or "false"`, this.#getCommandPath());
|
|
1755
|
-
}
|
|
1756
|
-
}
|
|
1757
|
-
else {
|
|
1758
|
-
opts[opt.long] = true;
|
|
1759
|
-
}
|
|
1760
|
-
i += 1;
|
|
1761
|
-
continue;
|
|
1762
|
-
}
|
|
1763
|
-
if (opt.args === 'required') {
|
|
1764
|
-
const eqIdx = token.resolved.indexOf('=');
|
|
1765
|
-
let rawValue;
|
|
1766
|
-
if (eqIdx !== -1) {
|
|
1767
|
-
rawValue = token.resolved.slice(eqIdx + 1);
|
|
1768
|
-
}
|
|
1769
|
-
else if (i + 1 < tokens.length && tokens[i + 1].type === 'none') {
|
|
1770
|
-
rawValue = tokens[i + 1].original;
|
|
1771
|
-
i += 1;
|
|
1772
|
-
}
|
|
1773
|
-
else {
|
|
1774
|
-
throw new CommanderError('MissingValue', `option "--${camelToKebabCase(opt.long)}" requires a value`, this.#getCommandPath());
|
|
1775
|
-
}
|
|
1776
|
-
opts[opt.long] = this.#convertValue(opt, rawValue);
|
|
1777
|
-
i += 1;
|
|
1778
|
-
continue;
|
|
1779
|
-
}
|
|
1780
|
-
if (opt.args === 'optional') {
|
|
1781
|
-
const eqIdx = token.resolved.indexOf('=');
|
|
1782
|
-
if (eqIdx !== -1) {
|
|
1783
|
-
opts[opt.long] = this.#convertValue(opt, token.resolved.slice(eqIdx + 1));
|
|
1784
|
-
i += 1;
|
|
1785
|
-
continue;
|
|
1786
|
-
}
|
|
1787
|
-
if (i + 1 < tokens.length && tokens[i + 1].type === 'none') {
|
|
1788
|
-
opts[opt.long] = this.#convertValue(opt, tokens[i + 1].original);
|
|
1789
|
-
i += 1;
|
|
1790
|
-
}
|
|
1791
|
-
else {
|
|
1792
|
-
opts[opt.long] = undefined;
|
|
1793
|
-
}
|
|
1794
|
-
i += 1;
|
|
1795
|
-
continue;
|
|
1796
|
-
}
|
|
1797
|
-
if (opt.args === 'variadic') {
|
|
1798
|
-
const values = Array.isArray(opts[opt.long]) ? opts[opt.long] : [];
|
|
1799
|
-
const eqIdx = token.resolved.indexOf('=');
|
|
1800
|
-
if (eqIdx !== -1) {
|
|
1801
|
-
values.push(this.#convertValue(opt, token.resolved.slice(eqIdx + 1)));
|
|
1802
|
-
}
|
|
1803
|
-
else {
|
|
1804
|
-
while (i + 1 < tokens.length && tokens[i + 1].type === 'none') {
|
|
1805
|
-
i += 1;
|
|
1806
|
-
values.push(this.#convertValue(opt, tokens[i].original));
|
|
1807
|
-
}
|
|
1808
|
-
}
|
|
1809
|
-
opts[opt.long] = values;
|
|
1810
|
-
i += 1;
|
|
1811
|
-
continue;
|
|
1812
|
-
}
|
|
1813
|
-
i += 1;
|
|
1814
|
-
}
|
|
1815
|
-
for (const opt of allOptions) {
|
|
1816
|
-
if (opt.required && !Object.prototype.hasOwnProperty.call(opts, opt.long)) {
|
|
1817
|
-
throw new CommanderError('MissingRequired', `missing required option "--${camelToKebabCase(opt.long)}"`, this.#getCommandPath());
|
|
1818
|
-
}
|
|
1819
|
-
}
|
|
1820
|
-
for (const opt of allOptions) {
|
|
1821
|
-
if (opt.choices && opts[opt.long] !== undefined) {
|
|
1822
|
-
const value = opts[opt.long];
|
|
1823
|
-
const values = Array.isArray(value) ? value : [value];
|
|
1824
|
-
const choices = opt.choices;
|
|
1825
|
-
for (const v of values) {
|
|
1826
|
-
if (!choices.includes(v)) {
|
|
1827
|
-
throw new CommanderError('InvalidChoice', `invalid value "${v}" for option "--${camelToKebabCase(opt.long)}". Allowed: ${opt.choices.join(', ')}`, this.#getCommandPath());
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
}
|
|
1831
|
-
}
|
|
1832
|
-
if (isNoColorEnabled(envs) && !sawColorToken && opts['color'] === true) {
|
|
1833
|
-
opts['color'] = false;
|
|
1834
|
-
}
|
|
1835
|
-
return opts;
|
|
1836
|
-
}
|
|
1837
|
-
#convertValue(opt, rawValue) {
|
|
1838
|
-
if (opt.coerce) {
|
|
1839
|
-
return opt.coerce(rawValue);
|
|
1840
|
-
}
|
|
1841
|
-
if (opt.type === 'number') {
|
|
1842
|
-
const num = parsePrimitiveNumber(rawValue);
|
|
1843
|
-
if (num === undefined) {
|
|
1844
|
-
throw new CommanderError('InvalidType', `invalid number "${rawValue}" for option "--${camelToKebabCase(opt.long)}"`, this.#getCommandPath());
|
|
1845
|
-
}
|
|
1846
|
-
return num;
|
|
1847
|
-
}
|
|
1848
|
-
return rawValue;
|
|
2456
|
+
shortMap.set(option.short, option.long);
|
|
1849
2457
|
}
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
missing.push(def.name);
|
|
1862
|
-
}
|
|
1863
|
-
else {
|
|
1864
|
-
remaining -= 1;
|
|
1865
|
-
}
|
|
1866
|
-
continue;
|
|
1867
|
-
}
|
|
1868
|
-
if (def.kind === 'optional') {
|
|
1869
|
-
if (remaining > 0) {
|
|
1870
|
-
remaining -= 1;
|
|
1871
|
-
}
|
|
1872
|
-
continue;
|
|
1873
|
-
}
|
|
1874
|
-
if (def.kind === 'some') {
|
|
1875
|
-
if (remaining === 0) {
|
|
1876
|
-
missing.push(def.name);
|
|
1877
|
-
}
|
|
1878
|
-
remaining = 0;
|
|
1879
|
-
continue;
|
|
1880
|
-
}
|
|
1881
|
-
remaining = 0;
|
|
1882
|
-
}
|
|
1883
|
-
if (missing.length > 0) {
|
|
1884
|
-
throw new CommanderError('MissingRequiredArgument', `missing required argument(s): ${missing.join(', ')}`, this.#getCommandPath());
|
|
1885
|
-
}
|
|
1886
|
-
let index = 0;
|
|
1887
|
-
for (const def of argumentDefs) {
|
|
1888
|
-
if (def.kind === 'variadic') {
|
|
1889
|
-
const rest = rawArgs.slice(index);
|
|
1890
|
-
args[def.name] = rest.map(raw => this.#convertArgument(def, raw));
|
|
1891
|
-
index = rawArgs.length;
|
|
1892
|
-
break;
|
|
1893
|
-
}
|
|
1894
|
-
if (def.kind === 'some') {
|
|
1895
|
-
const rest = rawArgs.slice(index);
|
|
1896
|
-
args[def.name] = rest.map(raw => this.#convertArgument(def, raw));
|
|
1897
|
-
index = rawArgs.length;
|
|
1898
|
-
break;
|
|
1899
|
-
}
|
|
1900
|
-
if (def.kind === 'optional') {
|
|
1901
|
-
const raw = rawArgs[index];
|
|
1902
|
-
if (raw === undefined) {
|
|
1903
|
-
args[def.name] = def.default ?? undefined;
|
|
1904
|
-
continue;
|
|
1905
|
-
}
|
|
1906
|
-
args[def.name] = this.#convertArgument(def, raw);
|
|
1907
|
-
index += 1;
|
|
1908
|
-
continue;
|
|
1909
|
-
}
|
|
1910
|
-
const raw = rawArgs[index];
|
|
1911
|
-
args[def.name] = this.#convertArgument(def, raw);
|
|
1912
|
-
index += 1;
|
|
1913
|
-
}
|
|
1914
|
-
const hasRestArgument = argumentDefs.some(a => a.kind === 'variadic' || a.kind === 'some');
|
|
1915
|
-
if (!hasRestArgument && index < rawArgs.length) {
|
|
1916
|
-
throw new CommanderError('TooManyArguments', `too many arguments: expected ${argumentDefs.length}, got ${rawArgs.length}`, this.#getCommandPath());
|
|
1917
|
-
}
|
|
1918
|
-
return { args, rawArgs };
|
|
2458
|
+
}
|
|
2459
|
+
|
|
2460
|
+
function camelToKebabCase(str) {
|
|
2461
|
+
return str.replace(/[A-Z]/g, m => '-' + m.toLowerCase());
|
|
2462
|
+
}
|
|
2463
|
+
function normalizeSubcommandNameForDistance(name) {
|
|
2464
|
+
return camelToKebabCase(name).toLowerCase();
|
|
2465
|
+
}
|
|
2466
|
+
function levenshteinDistance(left, right) {
|
|
2467
|
+
if (left === right) {
|
|
2468
|
+
return 0;
|
|
1919
2469
|
}
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
if (def.coerce) {
|
|
1923
|
-
try {
|
|
1924
|
-
value = def.coerce(raw);
|
|
1925
|
-
}
|
|
1926
|
-
catch {
|
|
1927
|
-
throw new CommanderError('InvalidType', `invalid value "${raw}" for argument "${def.name}"`, this.#getCommandPath());
|
|
1928
|
-
}
|
|
1929
|
-
}
|
|
1930
|
-
else {
|
|
1931
|
-
value = raw;
|
|
1932
|
-
}
|
|
1933
|
-
if (typeof value !== 'string') {
|
|
1934
|
-
throw new CommanderError('InvalidType', `invalid value for argument "${def.name}": expected ${def.type}`, this.#getCommandPath());
|
|
1935
|
-
}
|
|
1936
|
-
if (def.type === 'choice') {
|
|
1937
|
-
const choices = def.choices ?? [];
|
|
1938
|
-
if (!choices.includes(value)) {
|
|
1939
|
-
throw new CommanderError('InvalidChoice', `invalid value "${value}" for argument "${def.name}". Allowed: ${choices
|
|
1940
|
-
.map(choice => JSON.stringify(choice))
|
|
1941
|
-
.join(', ')}`, this.#getCommandPath());
|
|
1942
|
-
}
|
|
1943
|
-
}
|
|
1944
|
-
return value;
|
|
2470
|
+
if (left.length === 0) {
|
|
2471
|
+
return right.length;
|
|
1945
2472
|
}
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
return;
|
|
1949
|
-
}
|
|
1950
|
-
const token = userTailArgv[0];
|
|
1951
|
-
if (token === undefined || token.startsWith('-') || token === 'help') {
|
|
1952
|
-
return;
|
|
1953
|
-
}
|
|
1954
|
-
if (this.#findSubcommandEntry(token) !== undefined) {
|
|
1955
|
-
return;
|
|
1956
|
-
}
|
|
1957
|
-
const hints = [];
|
|
1958
|
-
if (this.#arguments.length === 0) {
|
|
1959
|
-
hints.push(`Hint: command "${this.#getCommandPath()}" does not accept positional arguments.`);
|
|
1960
|
-
}
|
|
1961
|
-
const candidate = this.#resolveDidYouMeanSubcommandName(token);
|
|
1962
|
-
if (candidate !== undefined) {
|
|
1963
|
-
hints.push(`Hint: did you mean "${candidate}"?`);
|
|
1964
|
-
}
|
|
1965
|
-
const details = hints.length > 0 ? `\n${hints.join('\n')}` : '';
|
|
1966
|
-
throw new CommanderError('UnknownSubcommand', `unknown subcommand "${token}" for command "${this.#getCommandPath()}"${details}`, this.#getCommandPath());
|
|
2473
|
+
if (right.length === 0) {
|
|
2474
|
+
return left.length;
|
|
1967
2475
|
}
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
let
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
const target = normalizeSubcommandNameForDistance(entry.name);
|
|
1975
|
-
const distance = levenshteinDistance(source, target);
|
|
1976
|
-
if (distance < minDistance) {
|
|
1977
|
-
minDistance = distance;
|
|
1978
|
-
bestName = entry.name;
|
|
1979
|
-
isUniqueBest = true;
|
|
1980
|
-
}
|
|
1981
|
-
else if (distance === minDistance) {
|
|
1982
|
-
isUniqueBest = false;
|
|
1983
|
-
}
|
|
1984
|
-
}
|
|
1985
|
-
if (minDistance <= 2 && isUniqueBest) {
|
|
1986
|
-
return bestName;
|
|
2476
|
+
let prev = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
2477
|
+
for (let row = 0; row < left.length; row += 1) {
|
|
2478
|
+
const current = [row + 1];
|
|
2479
|
+
for (let col = 0; col < right.length; col += 1) {
|
|
2480
|
+
const substitutionCost = left[row] === right[col] ? 0 : 1;
|
|
2481
|
+
current[col + 1] = Math.min(current[col] + 1, prev[col + 1] + 1, prev[col] + substitutionCost);
|
|
1987
2482
|
}
|
|
1988
|
-
|
|
1989
|
-
}
|
|
1990
|
-
#hasUserOption(long) {
|
|
1991
|
-
return this.#options.some(option => option.long === long);
|
|
1992
|
-
}
|
|
1993
|
-
#supportsBuiltinVersion() {
|
|
1994
|
-
return this.#version !== undefined && this.#builtin.option.version;
|
|
2483
|
+
prev = current;
|
|
1995
2484
|
}
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
optionMap.set('color', BUILTIN_COLOR_OPTION);
|
|
2005
|
-
}
|
|
2006
|
-
if (this.#builtin.option.logLevel && !hasUserLogLevel) {
|
|
2007
|
-
optionMap.set('logLevel', logLevelOption);
|
|
2008
|
-
}
|
|
2009
|
-
if (this.#builtin.option.silent && !hasUserSilent) {
|
|
2010
|
-
optionMap.set('silent', silentOption);
|
|
2011
|
-
}
|
|
2012
|
-
if (this.#builtin.option.logDate && !hasUserLogDate) {
|
|
2013
|
-
optionMap.set('logDate', logDateOption);
|
|
2014
|
-
}
|
|
2015
|
-
if (this.#builtin.option.logColorful && !hasUserLogColorful) {
|
|
2016
|
-
optionMap.set('logColorful', logColorfulOption);
|
|
2017
|
-
}
|
|
2018
|
-
for (const opt of this.#options) {
|
|
2019
|
-
optionMap.set(opt.long, opt);
|
|
2485
|
+
return prev[right.length];
|
|
2486
|
+
}
|
|
2487
|
+
function convertArgumentValue(params) {
|
|
2488
|
+
const { def, raw, commandPath } = params;
|
|
2489
|
+
let value;
|
|
2490
|
+
if (def.coerce) {
|
|
2491
|
+
try {
|
|
2492
|
+
value = def.coerce(raw);
|
|
2020
2493
|
}
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
};
|
|
2024
|
-
}
|
|
2025
|
-
#buildOptionPolicyMap(chain) {
|
|
2026
|
-
const optionPolicyMap = new Map();
|
|
2027
|
-
for (const cmd of chain) {
|
|
2028
|
-
optionPolicyMap.set(cmd, cmd.#resolveOptionPolicy());
|
|
2494
|
+
catch {
|
|
2495
|
+
throw new CommanderError('InvalidType', `invalid value "${raw}" for argument "${def.name}"`, commandPath);
|
|
2029
2496
|
}
|
|
2030
|
-
return optionPolicyMap;
|
|
2031
|
-
}
|
|
2032
|
-
#mustGetOptionPolicy(optionPolicyMap, cmd) {
|
|
2033
|
-
const policy = optionPolicyMap.get(cmd) ?? cmd.#resolveOptionPolicy();
|
|
2034
|
-
optionPolicyMap.set(cmd, policy);
|
|
2035
|
-
return policy;
|
|
2036
2497
|
}
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
for (const cmd of chain) {
|
|
2040
|
-
const policy = this.#mustGetOptionPolicy(optionPolicyMap, cmd);
|
|
2041
|
-
for (const opt of policy.mergedOptions) {
|
|
2042
|
-
mergedByLong.set(opt.long, opt);
|
|
2043
|
-
}
|
|
2044
|
-
}
|
|
2045
|
-
const shortMap = new Map();
|
|
2046
|
-
for (const opt of mergedByLong.values()) {
|
|
2047
|
-
if (!opt.short)
|
|
2048
|
-
continue;
|
|
2049
|
-
const existingLong = shortMap.get(opt.short);
|
|
2050
|
-
if (existingLong && existingLong !== opt.long) {
|
|
2051
|
-
throw new CommanderError('OptionConflict', `short option "-${opt.short}" conflicts with "--${existingLong}"`, this.#getCommandPath());
|
|
2052
|
-
}
|
|
2053
|
-
shortMap.set(opt.short, opt.long);
|
|
2054
|
-
}
|
|
2498
|
+
else {
|
|
2499
|
+
value = raw;
|
|
2055
2500
|
}
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
throw new CommanderError('ConfigurationError', `option long name "${opt.long}" is reserved`, this.#getCommandPath());
|
|
2059
|
-
}
|
|
2060
|
-
if (opt.type === 'boolean' && opt.args !== 'none') {
|
|
2061
|
-
throw new CommanderError('ConfigurationError', `boolean option "--${opt.long}" must have args: 'none'`, this.#getCommandPath());
|
|
2062
|
-
}
|
|
2063
|
-
if ((opt.type === 'string' || opt.type === 'number') && opt.args === 'none') {
|
|
2064
|
-
throw new CommanderError('ConfigurationError', `${opt.type} option "--${opt.long}" must have args: 'required', 'optional', or 'variadic'`, this.#getCommandPath());
|
|
2065
|
-
}
|
|
2066
|
-
if (opt.type === 'number' && opt.args === 'optional') {
|
|
2067
|
-
throw new CommanderError('ConfigurationError', `number option "--${opt.long}" does not support args: 'optional'`, this.#getCommandPath());
|
|
2068
|
-
}
|
|
2069
|
-
if (opt.long.startsWith('no')) {
|
|
2070
|
-
throw new CommanderError('ConfigurationError', `option long name cannot start with "no": "${opt.long}"`, this.#getCommandPath());
|
|
2071
|
-
}
|
|
2072
|
-
if (!/^[a-z][a-zA-Z0-9]*$/.test(opt.long)) {
|
|
2073
|
-
throw new CommanderError('ConfigurationError', `option long name must be camelCase: "${opt.long}"`, this.#getCommandPath());
|
|
2074
|
-
}
|
|
2075
|
-
if (opt.short !== undefined && opt.short.length !== 1) {
|
|
2076
|
-
throw new CommanderError('ConfigurationError', `option short name must be a single character: "${opt.short}"`, this.#getCommandPath());
|
|
2077
|
-
}
|
|
2078
|
-
if (opt.required && opt.default !== undefined) {
|
|
2079
|
-
throw new CommanderError('ConfigurationError', `option "--${opt.long}" cannot be both required and have a default value`, this.#getCommandPath());
|
|
2080
|
-
}
|
|
2081
|
-
if (opt.type === 'boolean' && opt.required) {
|
|
2082
|
-
throw new CommanderError('ConfigurationError', `boolean option "--${opt.long}" cannot be required`, this.#getCommandPath());
|
|
2083
|
-
}
|
|
2084
|
-
if (opt.required && opt.args !== 'required') {
|
|
2085
|
-
throw new CommanderError('ConfigurationError', `required option "--${opt.long}" must use args: 'required'`, this.#getCommandPath());
|
|
2086
|
-
}
|
|
2501
|
+
if (typeof value !== 'string') {
|
|
2502
|
+
throw new CommanderError('InvalidType', `invalid value for argument "${def.name}": expected ${def.type}`, commandPath);
|
|
2087
2503
|
}
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2504
|
+
if (def.type === 'choice') {
|
|
2505
|
+
const choices = def.choices ?? [];
|
|
2506
|
+
if (!choices.includes(value)) {
|
|
2507
|
+
throw new CommanderError('InvalidChoice', `invalid value "${value}" for argument "${def.name}". Allowed: ${choices
|
|
2508
|
+
.map(choice => JSON.stringify(choice))
|
|
2509
|
+
.join(', ')}`, commandPath);
|
|
2094
2510
|
}
|
|
2095
2511
|
}
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
}
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
throw new CommanderError('ConfigurationError', `argument "${arg.name}" of type "string" cannot declare choices`, this.#getCommandPath());
|
|
2111
|
-
}
|
|
2112
|
-
if (arg.type === 'choice') {
|
|
2113
|
-
if (!Array.isArray(arg.choices) || arg.choices.length === 0) {
|
|
2114
|
-
throw new CommanderError('ConfigurationError', `argument "${arg.name}" of type "choice" must declare a non-empty choices array`, this.#getCommandPath());
|
|
2115
|
-
}
|
|
2116
|
-
if (arg.choices.some(choice => typeof choice !== 'string')) {
|
|
2117
|
-
throw new CommanderError('ConfigurationError', `argument "${arg.name}" choices must be string[]`, this.#getCommandPath());
|
|
2512
|
+
return value;
|
|
2513
|
+
}
|
|
2514
|
+
function parseArguments(params) {
|
|
2515
|
+
const { argumentDefs, rawArgs, commandPath } = params;
|
|
2516
|
+
const args = {};
|
|
2517
|
+
if (argumentDefs.length === 0 && rawArgs.length > 0) {
|
|
2518
|
+
throw new CommanderError('UnexpectedArgument', `unexpected argument "${rawArgs[0]}"`, commandPath);
|
|
2519
|
+
}
|
|
2520
|
+
const missing = [];
|
|
2521
|
+
let remaining = rawArgs.length;
|
|
2522
|
+
for (const def of argumentDefs) {
|
|
2523
|
+
if (def.kind === 'required') {
|
|
2524
|
+
if (remaining === 0) {
|
|
2525
|
+
missing.push(def.name);
|
|
2118
2526
|
}
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
this.#validateArgumentDefaultValue(arg);
|
|
2122
|
-
}
|
|
2123
|
-
if (arg.kind === 'variadic' || arg.kind === 'some') {
|
|
2124
|
-
if (this.#arguments.some(a => a.kind === 'variadic' || a.kind === 'some')) {
|
|
2125
|
-
throw new CommanderError('ConfigurationError', 'only one variadic/some argument is allowed', this.#getCommandPath());
|
|
2527
|
+
else {
|
|
2528
|
+
remaining -= 1;
|
|
2126
2529
|
}
|
|
2530
|
+
continue;
|
|
2127
2531
|
}
|
|
2128
|
-
if (
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
throw new CommanderError('ConfigurationError', 'variadic/some argument must be the last argument', this.#getCommandPath());
|
|
2532
|
+
if (def.kind === 'optional') {
|
|
2533
|
+
if (remaining > 0) {
|
|
2534
|
+
remaining -= 1;
|
|
2132
2535
|
}
|
|
2536
|
+
continue;
|
|
2133
2537
|
}
|
|
2134
|
-
if (
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
throw new CommanderError('ConfigurationError', `required argument "${arg.name}" cannot come after optional/variadic/some arguments`, this.#getCommandPath());
|
|
2538
|
+
if (def.kind === 'some') {
|
|
2539
|
+
if (remaining === 0) {
|
|
2540
|
+
missing.push(def.name);
|
|
2138
2541
|
}
|
|
2542
|
+
remaining = 0;
|
|
2543
|
+
continue;
|
|
2139
2544
|
}
|
|
2545
|
+
remaining = 0;
|
|
2546
|
+
}
|
|
2547
|
+
if (missing.length > 0) {
|
|
2548
|
+
throw new CommanderError('MissingRequiredArgument', `missing required argument(s): ${missing.join(', ')}`, commandPath);
|
|
2140
2549
|
}
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2550
|
+
let index = 0;
|
|
2551
|
+
for (const def of argumentDefs) {
|
|
2552
|
+
if (def.kind === 'variadic' || def.kind === 'some') {
|
|
2553
|
+
const rest = rawArgs.slice(index);
|
|
2554
|
+
args[def.name] = rest.map(raw => convertArgumentValue({ def, raw, commandPath }));
|
|
2555
|
+
index = rawArgs.length;
|
|
2556
|
+
break;
|
|
2144
2557
|
}
|
|
2145
|
-
if (
|
|
2146
|
-
const
|
|
2147
|
-
if (
|
|
2148
|
-
|
|
2558
|
+
if (def.kind === 'optional') {
|
|
2559
|
+
const raw = rawArgs[index];
|
|
2560
|
+
if (raw === undefined) {
|
|
2561
|
+
args[def.name] = def.default ?? undefined;
|
|
2562
|
+
continue;
|
|
2149
2563
|
}
|
|
2564
|
+
args[def.name] = convertArgumentValue({ def, raw, commandPath });
|
|
2565
|
+
index += 1;
|
|
2566
|
+
continue;
|
|
2150
2567
|
}
|
|
2568
|
+
const raw = rawArgs[index];
|
|
2569
|
+
args[def.name] = convertArgumentValue({ def, raw, commandPath });
|
|
2570
|
+
index += 1;
|
|
2571
|
+
}
|
|
2572
|
+
const hasRestArgument = argumentDefs.some(def => def.kind === 'variadic' || def.kind === 'some');
|
|
2573
|
+
if (!hasRestArgument && index < rawArgs.length) {
|
|
2574
|
+
throw new CommanderError('TooManyArguments', `too many arguments: expected ${argumentDefs.length}, got ${rawArgs.length}`, commandPath);
|
|
2575
|
+
}
|
|
2576
|
+
return { args, rawArgs };
|
|
2577
|
+
}
|
|
2578
|
+
function resolveDidYouMeanSubcommandName(token, subcommands) {
|
|
2579
|
+
const source = normalizeSubcommandNameForDistance(token);
|
|
2580
|
+
let minDistance = Number.POSITIVE_INFINITY;
|
|
2581
|
+
let bestName;
|
|
2582
|
+
let isUniqueBest = false;
|
|
2583
|
+
for (const entry of subcommands) {
|
|
2584
|
+
const target = normalizeSubcommandNameForDistance(entry.name);
|
|
2585
|
+
const distance = levenshteinDistance(source, target);
|
|
2586
|
+
if (distance < minDistance) {
|
|
2587
|
+
minDistance = distance;
|
|
2588
|
+
bestName = entry.name;
|
|
2589
|
+
isUniqueBest = true;
|
|
2590
|
+
}
|
|
2591
|
+
else if (distance === minDistance) {
|
|
2592
|
+
isUniqueBest = false;
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
if (minDistance <= 2 && isUniqueBest) {
|
|
2596
|
+
return bestName;
|
|
2597
|
+
}
|
|
2598
|
+
return undefined;
|
|
2599
|
+
}
|
|
2600
|
+
function assertUnknownSubcommand(params) {
|
|
2601
|
+
const { userTailArgv, subcommands, hasArguments, commandPath, withDiagnosticsIssue } = params;
|
|
2602
|
+
if (subcommands.length === 0) {
|
|
2603
|
+
return;
|
|
2604
|
+
}
|
|
2605
|
+
const token = userTailArgv[0];
|
|
2606
|
+
if (token === undefined || token.startsWith('-') || token === 'help') {
|
|
2607
|
+
return;
|
|
2608
|
+
}
|
|
2609
|
+
const matched = subcommands.some(entry => entry.name === token || entry.aliases.includes(token));
|
|
2610
|
+
if (matched) {
|
|
2611
|
+
return;
|
|
2612
|
+
}
|
|
2613
|
+
let error = new CommanderError('UnknownSubcommand', `unknown subcommand "${token}" for command "${commandPath}"`, commandPath);
|
|
2614
|
+
error = withDiagnosticsIssue(error);
|
|
2615
|
+
if (!hasArguments) {
|
|
2616
|
+
const hint = {
|
|
2617
|
+
kind: 'hint',
|
|
2618
|
+
stage: 'parse',
|
|
2619
|
+
scope: 'command',
|
|
2620
|
+
reason: {
|
|
2621
|
+
code: 'command_does_not_accept_positional_arguments',
|
|
2622
|
+
message: `command "${commandPath}" does not accept positional arguments`,
|
|
2623
|
+
},
|
|
2624
|
+
};
|
|
2625
|
+
error = error.withIssue(hint);
|
|
2626
|
+
}
|
|
2627
|
+
const candidate = resolveDidYouMeanSubcommandName(token, subcommands);
|
|
2628
|
+
if (candidate !== undefined) {
|
|
2629
|
+
const hint = {
|
|
2630
|
+
kind: 'hint',
|
|
2631
|
+
stage: 'parse',
|
|
2632
|
+
scope: 'command',
|
|
2633
|
+
reason: {
|
|
2634
|
+
code: 'did_you_mean_subcommand',
|
|
2635
|
+
message: `did you mean "${candidate}"?`,
|
|
2636
|
+
details: { candidate },
|
|
2637
|
+
},
|
|
2638
|
+
};
|
|
2639
|
+
error = error.withIssue(hint);
|
|
2640
|
+
}
|
|
2641
|
+
throw error;
|
|
2642
|
+
}
|
|
2643
|
+
function collectRawArguments(params) {
|
|
2644
|
+
const { argTokens, restArgs } = params;
|
|
2645
|
+
return [...argTokens.map(token => token.original), ...restArgs];
|
|
2646
|
+
}
|
|
2647
|
+
function parseStage(params) {
|
|
2648
|
+
const { chain, resolveResult, optionPolicyMap, ctx, restArgs, rootCommandPath, mustGetOptionPolicy, parseOptions, applyBuiltinDevmodeLogLevel, resolveBuiltinParsedOptions, applyOptionCallbacks, getLocalOptions, getSubcommands, getArguments, getCommandPath, withUnknownSubcommandIssue, freezeInputSources, } = params;
|
|
2649
|
+
const { consumedTokens, argTokens } = resolveResult;
|
|
2650
|
+
const leafCommand = chain[chain.length - 1];
|
|
2651
|
+
validateMergedShortOptions({
|
|
2652
|
+
chain,
|
|
2653
|
+
optionPolicyMap,
|
|
2654
|
+
mustGetOptionPolicy: (map, command) => mustGetOptionPolicy(map, command),
|
|
2655
|
+
rootCommandPath,
|
|
2656
|
+
});
|
|
2657
|
+
const optsMap = new Map();
|
|
2658
|
+
const builtinMap = new Map();
|
|
2659
|
+
for (const command of chain) {
|
|
2660
|
+
const policy = mustGetOptionPolicy(optionPolicyMap, command);
|
|
2661
|
+
const tokens = consumedTokens.get(command) ?? [];
|
|
2662
|
+
const commandPath = getCommandPath(command);
|
|
2663
|
+
const parseOptionsResult = parseOptions({
|
|
2664
|
+
command,
|
|
2665
|
+
tokens,
|
|
2666
|
+
allOptions: policy.mergedOptions,
|
|
2667
|
+
envs: ctx.envs,
|
|
2668
|
+
commandPath,
|
|
2669
|
+
});
|
|
2670
|
+
const opts = parseOptionsResult.opts;
|
|
2671
|
+
applyBuiltinDevmodeLogLevel({
|
|
2672
|
+
command,
|
|
2673
|
+
opts,
|
|
2674
|
+
explicitOptionLongs: parseOptionsResult.explicitOptionLongs,
|
|
2675
|
+
});
|
|
2676
|
+
optsMap.set(command, opts);
|
|
2677
|
+
builtinMap.set(command, resolveBuiltinParsedOptions({ command, opts }));
|
|
2678
|
+
applyOptionCallbacks({
|
|
2679
|
+
command,
|
|
2680
|
+
opts,
|
|
2681
|
+
allOptions: policy.mergedOptions,
|
|
2682
|
+
ctx,
|
|
2683
|
+
});
|
|
2151
2684
|
}
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2685
|
+
const leafLocalOpts = {};
|
|
2686
|
+
const leafParsedOpts = optsMap.get(leafCommand) ?? {};
|
|
2687
|
+
for (const option of getLocalOptions(leafCommand)) {
|
|
2688
|
+
if (Object.prototype.hasOwnProperty.call(leafParsedOpts, option.long)) {
|
|
2689
|
+
leafLocalOpts[option.long] = leafParsedOpts[option.long];
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
const leafCommandPath = getCommandPath(leafCommand);
|
|
2693
|
+
const leafArguments = getArguments(leafCommand);
|
|
2694
|
+
assertUnknownSubcommand({
|
|
2695
|
+
userTailArgv: ctx.sources.user.argv,
|
|
2696
|
+
subcommands: getSubcommands(leafCommand),
|
|
2697
|
+
hasArguments: leafArguments.length > 0,
|
|
2698
|
+
commandPath: leafCommandPath,
|
|
2699
|
+
withDiagnosticsIssue: withUnknownSubcommandIssue,
|
|
2700
|
+
});
|
|
2701
|
+
const rawArgStrings = collectRawArguments({ argTokens, restArgs });
|
|
2702
|
+
const { args, rawArgs } = parseArguments({
|
|
2703
|
+
argumentDefs: leafArguments,
|
|
2704
|
+
rawArgs: rawArgStrings,
|
|
2705
|
+
commandPath: leafCommandPath,
|
|
2706
|
+
});
|
|
2707
|
+
const parseCtx = {
|
|
2708
|
+
...ctx,
|
|
2709
|
+
sources: freezeInputSources(ctx.sources),
|
|
2710
|
+
};
|
|
2711
|
+
return {
|
|
2712
|
+
ctx: parseCtx,
|
|
2713
|
+
builtin: builtinMap.get(leafCommand) ?? { devmode: false },
|
|
2714
|
+
opts: leafLocalOpts,
|
|
2715
|
+
args,
|
|
2716
|
+
rawArgs,
|
|
2717
|
+
};
|
|
2718
|
+
}
|
|
2719
|
+
|
|
2720
|
+
function resolvePresetConfigFromChain(params) {
|
|
2721
|
+
const { chain, getPresetConfig } = params;
|
|
2722
|
+
let presetFile;
|
|
2723
|
+
let presetProfile;
|
|
2724
|
+
for (let index = chain.length - 1; index >= 0; index -= 1) {
|
|
2725
|
+
const presetConfig = getPresetConfig(chain[index]);
|
|
2726
|
+
if (presetFile === undefined && presetConfig?.file !== undefined) {
|
|
2727
|
+
presetFile = presetConfig.file;
|
|
2158
2728
|
}
|
|
2159
|
-
if (
|
|
2160
|
-
|
|
2729
|
+
if (presetProfile === undefined && presetConfig?.profile !== undefined) {
|
|
2730
|
+
presetProfile = presetConfig.profile;
|
|
2161
2731
|
}
|
|
2162
|
-
if (
|
|
2163
|
-
|
|
2732
|
+
if (presetFile !== undefined && presetProfile !== undefined) {
|
|
2733
|
+
break;
|
|
2164
2734
|
}
|
|
2165
|
-
return { title, usage, desc };
|
|
2166
2735
|
}
|
|
2167
|
-
|
|
2168
|
-
|
|
2736
|
+
return { presetFile, presetProfile };
|
|
2737
|
+
}
|
|
2738
|
+
function scanPresetDirectives(params) {
|
|
2739
|
+
const { argv, commandPath, presetFileFlag, presetProfileFlag, assertPresetProfileSelectorValue } = params;
|
|
2740
|
+
const cleanArgv = [];
|
|
2741
|
+
let presetFile;
|
|
2742
|
+
let presetProfile;
|
|
2743
|
+
const assignDirective = (flag, value) => {
|
|
2744
|
+
if (flag === presetFileFlag) {
|
|
2745
|
+
presetFile = value;
|
|
2169
2746
|
return;
|
|
2170
|
-
try {
|
|
2171
|
-
await this.#action(params);
|
|
2172
2747
|
}
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2748
|
+
assertPresetProfileSelectorValue(value, presetProfileFlag, commandPath);
|
|
2749
|
+
presetProfile = value;
|
|
2750
|
+
};
|
|
2751
|
+
let index = 0;
|
|
2752
|
+
while (index < argv.length) {
|
|
2753
|
+
const token = argv[index];
|
|
2754
|
+
if (token === presetFileFlag || token === presetProfileFlag) {
|
|
2755
|
+
const value = argv[index + 1];
|
|
2756
|
+
if (value === undefined || value.length === 0) {
|
|
2757
|
+
throw new CommanderError('ConfigurationError', `missing value for "${token}"`, commandPath);
|
|
2758
|
+
}
|
|
2759
|
+
assignDirective(token, value);
|
|
2760
|
+
index += 2;
|
|
2761
|
+
continue;
|
|
2762
|
+
}
|
|
2763
|
+
if (token.startsWith(`${presetFileFlag}=`)) {
|
|
2764
|
+
const value = token.slice(presetFileFlag.length + 1);
|
|
2765
|
+
if (value.length === 0) {
|
|
2766
|
+
throw new CommanderError('ConfigurationError', `missing value for "${presetFileFlag}"`, commandPath);
|
|
2176
2767
|
}
|
|
2177
|
-
|
|
2178
|
-
|
|
2768
|
+
assignDirective(presetFileFlag, value);
|
|
2769
|
+
index += 1;
|
|
2770
|
+
continue;
|
|
2771
|
+
}
|
|
2772
|
+
if (token.startsWith(`${presetProfileFlag}=`)) {
|
|
2773
|
+
const value = token.slice(presetProfileFlag.length + 1);
|
|
2774
|
+
if (value.length === 0) {
|
|
2775
|
+
throw new CommanderError('ConfigurationError', `missing value for "${presetProfileFlag}"`, commandPath);
|
|
2179
2776
|
}
|
|
2180
|
-
|
|
2777
|
+
assignDirective(presetProfileFlag, value);
|
|
2778
|
+
index += 1;
|
|
2779
|
+
continue;
|
|
2181
2780
|
}
|
|
2781
|
+
cleanArgv.push(token);
|
|
2782
|
+
index += 1;
|
|
2182
2783
|
}
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
}
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2784
|
+
return { cleanArgv, presetFile, presetProfile };
|
|
2785
|
+
}
|
|
2786
|
+
function buildPresetSources(params) {
|
|
2787
|
+
const { userCmds, userArgv, userEnvs, presetArgv, presetEnvs, presetMeta } = params;
|
|
2788
|
+
const presetSourceMeta = presetMeta === undefined
|
|
2789
|
+
? undefined
|
|
2790
|
+
: {
|
|
2791
|
+
applied: true,
|
|
2792
|
+
file: presetMeta.file,
|
|
2793
|
+
profile: presetMeta.profile,
|
|
2794
|
+
variant: presetMeta.variant,
|
|
2795
|
+
};
|
|
2796
|
+
const presetState = presetSourceMeta === undefined ? 'none' : 'applied';
|
|
2797
|
+
const sources = {
|
|
2798
|
+
user: {
|
|
2799
|
+
cmds: [...userCmds],
|
|
2800
|
+
argv: [...userArgv],
|
|
2801
|
+
envs: { ...userEnvs },
|
|
2802
|
+
},
|
|
2803
|
+
preset: {
|
|
2804
|
+
state: presetState,
|
|
2805
|
+
argv: [...presetArgv],
|
|
2806
|
+
envs: { ...presetEnvs },
|
|
2807
|
+
meta: presetSourceMeta === undefined ? undefined : { ...presetSourceMeta },
|
|
2808
|
+
},
|
|
2809
|
+
};
|
|
2810
|
+
const envs = { ...sources.user.envs, ...sources.preset.envs };
|
|
2811
|
+
const tailArgv = [...sources.preset.argv, ...sources.user.argv];
|
|
2812
|
+
const segments = [
|
|
2813
|
+
...sources.preset.argv.map(value => ({
|
|
2814
|
+
value,
|
|
2815
|
+
source: 'preset',
|
|
2816
|
+
preset: sources.preset.meta === undefined
|
|
2817
|
+
? undefined
|
|
2818
|
+
: {
|
|
2819
|
+
file: sources.preset.meta.file,
|
|
2820
|
+
profile: sources.preset.meta.profile,
|
|
2821
|
+
variant: sources.preset.meta.variant,
|
|
2822
|
+
},
|
|
2823
|
+
})),
|
|
2824
|
+
...sources.user.argv.map(value => ({ value, source: 'user' })),
|
|
2825
|
+
];
|
|
2826
|
+
return {
|
|
2827
|
+
sources,
|
|
2828
|
+
tailArgv,
|
|
2829
|
+
envs,
|
|
2830
|
+
segments,
|
|
2831
|
+
};
|
|
2832
|
+
}
|
|
2833
|
+
async function resolvePresetStage(params) {
|
|
2834
|
+
const { controlTailArgv, chain, commandPath, presetFileFlag, presetProfileFlag, getPresetConfig, assertPresetProfileSelectorValue, resolvePresetProfile, } = params;
|
|
2835
|
+
const separatorIndex = controlTailArgv.indexOf('--');
|
|
2836
|
+
const beforeSeparator = separatorIndex === -1 ? controlTailArgv : controlTailArgv.slice(0, separatorIndex);
|
|
2837
|
+
const afterSeparator = separatorIndex === -1 ? [] : controlTailArgv.slice(separatorIndex + 1);
|
|
2838
|
+
const profileScanResult = scanPresetDirectives({
|
|
2839
|
+
argv: beforeSeparator,
|
|
2840
|
+
commandPath,
|
|
2841
|
+
presetFileFlag,
|
|
2842
|
+
presetProfileFlag,
|
|
2843
|
+
assertPresetProfileSelectorValue,
|
|
2844
|
+
});
|
|
2845
|
+
const cleanArgv = separatorIndex === -1
|
|
2846
|
+
? profileScanResult.cleanArgv
|
|
2847
|
+
: [...profileScanResult.cleanArgv, '--', ...afterSeparator];
|
|
2848
|
+
const resolvedCommandPresetConfig = resolvePresetConfigFromChain({
|
|
2849
|
+
chain,
|
|
2850
|
+
getPresetConfig,
|
|
2851
|
+
});
|
|
2852
|
+
const commandPresetFile = resolvedCommandPresetConfig.presetFile;
|
|
2853
|
+
const effectivePresetFile = profileScanResult.presetFile ?? commandPresetFile;
|
|
2854
|
+
const commandPresetProfile = resolvedCommandPresetConfig.presetProfile;
|
|
2855
|
+
const useCommandPresetProfile = profileScanResult.presetProfile === undefined && commandPresetProfile !== undefined;
|
|
2856
|
+
if (useCommandPresetProfile) {
|
|
2857
|
+
assertPresetProfileSelectorValue(commandPresetProfile, 'command.preset.profile', commandPath);
|
|
2858
|
+
}
|
|
2859
|
+
const effectivePresetProfile = profileScanResult.presetProfile ?? commandPresetProfile;
|
|
2860
|
+
const effectivePresetProfileSourceName = profileScanResult.presetProfile !== undefined
|
|
2861
|
+
? presetProfileFlag
|
|
2862
|
+
: commandPresetProfile !== undefined
|
|
2863
|
+
? 'command.preset.profile'
|
|
2864
|
+
: undefined;
|
|
2865
|
+
if (effectivePresetFile === undefined && useCommandPresetProfile) {
|
|
2866
|
+
throw new CommanderError('ConfigurationError', 'cannot use "command.preset.profile" without "command.preset.file" or "--preset-file"', commandPath);
|
|
2867
|
+
}
|
|
2868
|
+
const resolvedProfile = await resolvePresetProfile({
|
|
2869
|
+
presetFile: effectivePresetFile,
|
|
2870
|
+
presetProfile: effectivePresetProfile,
|
|
2871
|
+
presetProfileSourceName: effectivePresetProfileSourceName,
|
|
2872
|
+
commandPath,
|
|
2873
|
+
});
|
|
2874
|
+
return { cleanArgv, resolvedProfile };
|
|
2875
|
+
}
|
|
2876
|
+
async function runPresetStage(params) {
|
|
2877
|
+
const { controlTailArgv, chain, commandPath, presetFileFlag, presetProfileFlag, runtime, userCmds, userEnvs, getPresetConfig, assertPresetProfileSelectorValue, resolvePresetProfile, validatePresetOptionTokens, } = params;
|
|
2878
|
+
const { cleanArgv, resolvedProfile } = await resolvePresetStage({
|
|
2879
|
+
controlTailArgv,
|
|
2880
|
+
chain,
|
|
2881
|
+
commandPath,
|
|
2882
|
+
presetFileFlag,
|
|
2883
|
+
presetProfileFlag,
|
|
2884
|
+
getPresetConfig,
|
|
2885
|
+
assertPresetProfileSelectorValue,
|
|
2886
|
+
resolvePresetProfile,
|
|
2887
|
+
});
|
|
2888
|
+
const { presetArgv, presetEnvs } = await buildPresetProfileInputs({
|
|
2889
|
+
runtime,
|
|
2890
|
+
commandPath,
|
|
2891
|
+
resolvedProfile,
|
|
2892
|
+
validatePresetOptionTokens,
|
|
2893
|
+
});
|
|
2894
|
+
const { tailArgv, envs, segments, sources } = buildPresetSources({
|
|
2895
|
+
userCmds,
|
|
2896
|
+
userArgv: cleanArgv,
|
|
2897
|
+
userEnvs,
|
|
2898
|
+
presetArgv,
|
|
2899
|
+
presetEnvs,
|
|
2900
|
+
presetMeta: resolvedProfile?.issueMeta,
|
|
2901
|
+
});
|
|
2902
|
+
return { tailArgv, envs, segments, sources };
|
|
2903
|
+
}
|
|
2904
|
+
|
|
2905
|
+
function findSubcommandEntry(entries, token) {
|
|
2906
|
+
return entries.find(entry => entry.name === token || entry.aliases.includes(token));
|
|
2907
|
+
}
|
|
2908
|
+
function routeCommandChain(params) {
|
|
2909
|
+
const { root, argv, getSubcommandEntries } = params;
|
|
2910
|
+
const chain = [root];
|
|
2911
|
+
const cmds = [];
|
|
2912
|
+
let current = root;
|
|
2913
|
+
let index = 0;
|
|
2914
|
+
while (index < argv.length) {
|
|
2915
|
+
const token = argv[index];
|
|
2916
|
+
if (token.startsWith('-')) {
|
|
2917
|
+
break;
|
|
2918
|
+
}
|
|
2919
|
+
const entry = findSubcommandEntry(getSubcommandEntries(current), token);
|
|
2920
|
+
if (entry === undefined) {
|
|
2921
|
+
break;
|
|
2922
|
+
}
|
|
2923
|
+
current = entry.command;
|
|
2924
|
+
cmds.push(token);
|
|
2925
|
+
chain.push(current);
|
|
2926
|
+
index += 1;
|
|
2927
|
+
}
|
|
2928
|
+
return {
|
|
2929
|
+
chain,
|
|
2930
|
+
cmds,
|
|
2931
|
+
remaining: argv.slice(index),
|
|
2932
|
+
};
|
|
2933
|
+
}
|
|
2934
|
+
function resolveHelpCommand(params) {
|
|
2935
|
+
const { leafCommand, helpTarget, getSubcommandEntries } = params;
|
|
2936
|
+
if (helpTarget === undefined) {
|
|
2937
|
+
return leafCommand;
|
|
2938
|
+
}
|
|
2939
|
+
const entry = findSubcommandEntry(getSubcommandEntries(leafCommand), helpTarget);
|
|
2940
|
+
if (entry === undefined) {
|
|
2941
|
+
return leafCommand;
|
|
2942
|
+
}
|
|
2943
|
+
return entry.command;
|
|
2944
|
+
}
|
|
2945
|
+
function findCommandByPath(params) {
|
|
2946
|
+
const { root, commandPath, getCommandName, getSubcommandEntries } = params;
|
|
2947
|
+
if (!commandPath) {
|
|
2948
|
+
return root;
|
|
2949
|
+
}
|
|
2950
|
+
const segments = commandPath.split(' ').filter(Boolean);
|
|
2951
|
+
if (segments.length === 0) {
|
|
2952
|
+
return root;
|
|
2953
|
+
}
|
|
2954
|
+
let startIndex = 0;
|
|
2955
|
+
const rootName = getCommandName(root);
|
|
2956
|
+
if (rootName !== undefined && segments[0] === rootName) {
|
|
2957
|
+
startIndex = 1;
|
|
2958
|
+
}
|
|
2959
|
+
let current = root;
|
|
2960
|
+
for (const segment of segments.slice(startIndex)) {
|
|
2961
|
+
if (current === undefined) {
|
|
2962
|
+
return undefined;
|
|
2963
|
+
}
|
|
2964
|
+
const entry = findSubcommandEntry(getSubcommandEntries(current), segment);
|
|
2965
|
+
current = entry?.command;
|
|
2966
|
+
}
|
|
2967
|
+
return current;
|
|
2968
|
+
}
|
|
2969
|
+
|
|
2970
|
+
async function runStage(params) {
|
|
2971
|
+
const { leafCommand, actionParams, tailArgv, envs, hasAction, runAction, hasSubcommands, renderHelpForDisplay, print, getCommandPath, } = params;
|
|
2972
|
+
if (hasAction(leafCommand)) {
|
|
2973
|
+
await runAction(leafCommand, actionParams);
|
|
2974
|
+
return;
|
|
2975
|
+
}
|
|
2976
|
+
if (hasSubcommands(leafCommand)) {
|
|
2977
|
+
print(renderHelpForDisplay(leafCommand, {
|
|
2978
|
+
tailArgv,
|
|
2979
|
+
envs,
|
|
2980
|
+
}));
|
|
2981
|
+
return;
|
|
2982
|
+
}
|
|
2983
|
+
throw new CommanderError('ConfigurationError', `no action defined for command "${getCommandPath(leafCommand)}"`, getCommandPath(leafCommand));
|
|
2984
|
+
}
|
|
2985
|
+
|
|
2986
|
+
const LONG_OPTION_REGEX = /^--[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
2987
|
+
const NEGATIVE_OPTION_REGEX = /^--no-[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
2988
|
+
function kebabToCamelCase(str) {
|
|
2989
|
+
return str.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
2990
|
+
}
|
|
2991
|
+
function tokenizeLongOption(segment, commandPath) {
|
|
2992
|
+
const arg = segment.value;
|
|
2993
|
+
const eqIdx = arg.indexOf('=');
|
|
2994
|
+
const namePart = eqIdx !== -1 ? arg.slice(0, eqIdx) : arg;
|
|
2995
|
+
const valuePart = eqIdx !== -1 ? arg.slice(eqIdx) : '';
|
|
2996
|
+
if (namePart.includes('_')) {
|
|
2997
|
+
throw new CommanderError('InvalidOptionFormat', `invalid option "${arg}": use '-' instead of '_'`, commandPath);
|
|
2998
|
+
}
|
|
2999
|
+
const lowerName = namePart.toLowerCase();
|
|
3000
|
+
if (lowerName === '--no' || lowerName === '--no-') {
|
|
3001
|
+
throw new CommanderError('InvalidNegativeOption', `invalid negative option syntax "${arg}"`, commandPath);
|
|
3002
|
+
}
|
|
3003
|
+
if (lowerName.startsWith('--no-')) {
|
|
3004
|
+
if (valuePart !== '') {
|
|
3005
|
+
throw new CommanderError('NegativeOptionWithValue', `"${namePart}" does not accept a value`, commandPath);
|
|
3006
|
+
}
|
|
3007
|
+
if (!NEGATIVE_OPTION_REGEX.test(lowerName)) {
|
|
3008
|
+
throw new CommanderError('InvalidOptionFormat', `invalid option format "${arg}"`, commandPath);
|
|
3009
|
+
}
|
|
3010
|
+
const camelName = kebabToCamelCase(lowerName.slice(5));
|
|
3011
|
+
return {
|
|
3012
|
+
original: arg,
|
|
3013
|
+
resolved: `--${camelName}=false`,
|
|
3014
|
+
name: camelName,
|
|
3015
|
+
type: 'long',
|
|
3016
|
+
source: segment.source,
|
|
3017
|
+
preset: segment.preset,
|
|
3018
|
+
};
|
|
3019
|
+
}
|
|
3020
|
+
if (!LONG_OPTION_REGEX.test(lowerName)) {
|
|
3021
|
+
throw new CommanderError('InvalidOptionFormat', `invalid option format "${arg}"`, commandPath);
|
|
3022
|
+
}
|
|
3023
|
+
const camelName = kebabToCamelCase(lowerName.slice(2));
|
|
3024
|
+
return {
|
|
3025
|
+
original: arg,
|
|
3026
|
+
resolved: `--${camelName}${valuePart}`,
|
|
3027
|
+
name: camelName,
|
|
3028
|
+
type: 'long',
|
|
3029
|
+
source: segment.source,
|
|
3030
|
+
preset: segment.preset,
|
|
3031
|
+
};
|
|
3032
|
+
}
|
|
3033
|
+
function tokenizeShortOptions(segment, commandPath) {
|
|
3034
|
+
const arg = segment.value;
|
|
3035
|
+
if (arg.includes('=')) {
|
|
3036
|
+
throw new CommanderError('UnsupportedShortSyntax', `"${arg}" is not supported. Use "-${arg[1]} ${arg.slice(3)}" instead`, commandPath);
|
|
3037
|
+
}
|
|
3038
|
+
return arg
|
|
3039
|
+
.slice(1)
|
|
3040
|
+
.split('')
|
|
3041
|
+
.map(flag => ({
|
|
3042
|
+
original: `-${flag}`,
|
|
3043
|
+
resolved: `-${flag}`,
|
|
3044
|
+
name: flag,
|
|
3045
|
+
type: 'short',
|
|
3046
|
+
source: segment.source,
|
|
3047
|
+
preset: segment.preset,
|
|
3048
|
+
}));
|
|
3049
|
+
}
|
|
3050
|
+
function tokenizeArgv(segments, commandPath) {
|
|
3051
|
+
const optionTokens = [];
|
|
3052
|
+
const restArgs = [];
|
|
3053
|
+
let passThrough = false;
|
|
3054
|
+
for (const segment of segments) {
|
|
3055
|
+
const arg = segment.value;
|
|
3056
|
+
if (arg === '--') {
|
|
3057
|
+
passThrough = true;
|
|
3058
|
+
continue;
|
|
3059
|
+
}
|
|
3060
|
+
if (passThrough) {
|
|
3061
|
+
restArgs.push(segment.value);
|
|
3062
|
+
continue;
|
|
3063
|
+
}
|
|
3064
|
+
if (arg.startsWith('--')) {
|
|
3065
|
+
optionTokens.push(tokenizeLongOption(segment, commandPath));
|
|
3066
|
+
continue;
|
|
3067
|
+
}
|
|
3068
|
+
if (arg.startsWith('-') && arg.length > 1) {
|
|
3069
|
+
optionTokens.push(...tokenizeShortOptions(segment, commandPath));
|
|
3070
|
+
continue;
|
|
3071
|
+
}
|
|
3072
|
+
optionTokens.push({
|
|
3073
|
+
original: arg,
|
|
3074
|
+
resolved: arg,
|
|
3075
|
+
name: '',
|
|
3076
|
+
type: 'none',
|
|
3077
|
+
source: segment.source,
|
|
3078
|
+
preset: segment.preset,
|
|
3079
|
+
});
|
|
3080
|
+
}
|
|
3081
|
+
return { optionTokens, restArgs };
|
|
3082
|
+
}
|
|
3083
|
+
|
|
3084
|
+
class Command {
|
|
3085
|
+
#name;
|
|
3086
|
+
#desc;
|
|
3087
|
+
#version;
|
|
3088
|
+
#builtinConfig;
|
|
3089
|
+
#builtin;
|
|
3090
|
+
#presetConfig;
|
|
3091
|
+
#reporter;
|
|
3092
|
+
#runtime;
|
|
3093
|
+
#contextAdapter = new CommandContextAdapter();
|
|
3094
|
+
#diagnostics = new CommandDiagnosticsEngine();
|
|
3095
|
+
#helpRenderer = new CommandHelpRenderer();
|
|
3096
|
+
#optionParser = new CommandOptionParser();
|
|
3097
|
+
#presetProfileParser;
|
|
3098
|
+
#kernel;
|
|
3099
|
+
#parent;
|
|
3100
|
+
#options = [];
|
|
3101
|
+
#arguments = [];
|
|
3102
|
+
#examples = [];
|
|
3103
|
+
#subcommandsList = [];
|
|
3104
|
+
#subcommandsMap = new Map();
|
|
3105
|
+
#action = undefined;
|
|
3106
|
+
constructor(config) {
|
|
3107
|
+
this.#name = config.name ?? '';
|
|
3108
|
+
this.#desc = config.desc;
|
|
3109
|
+
this.#version = config.version;
|
|
3110
|
+
this.#builtinConfig = config.builtin;
|
|
3111
|
+
this.#builtin = normalizeBuiltinConfig(config.builtin);
|
|
3112
|
+
this.#presetConfig = config.preset;
|
|
3113
|
+
this.#reporter = config.reporter;
|
|
3114
|
+
this.#runtime = config.runtime ?? getDefaultCommandRuntime();
|
|
3115
|
+
this.#presetProfileParser = new CommandPresetProfileParser({
|
|
3116
|
+
resolvePresetFileAbsolutePath: (filepath, baseDirectory) => resolvePresetFileAbsolutePath({
|
|
3117
|
+
runtime: this.#runtime,
|
|
3118
|
+
filepath,
|
|
3119
|
+
baseDirectory,
|
|
3120
|
+
}),
|
|
3121
|
+
resolvePath: (...paths) => this.#runtime.resolve(...paths),
|
|
3122
|
+
readPresetFile: async (file, commandPath) => readPresetFile({ runtime: this.#runtime, file, commandPath }),
|
|
3123
|
+
});
|
|
3124
|
+
this.#kernel = new CommandKernel({
|
|
3125
|
+
port: {
|
|
3126
|
+
route: argv => this.#route(argv),
|
|
3127
|
+
createContext: params => this.#createContext(params),
|
|
3128
|
+
controlScan: (tailArgv, leafCommand) => this.#controlScan(tailArgv, leafCommand),
|
|
3129
|
+
controlRun: (leafCommand, controlScanResult) => this.#controlRun(leafCommand, controlScanResult),
|
|
3130
|
+
preset: async (tailArgv, ctx) => this.#preset(tailArgv, ctx),
|
|
3131
|
+
tokenize: (segments, commandPath) => tokenizeArgv(segments, commandPath),
|
|
3132
|
+
getCommandPath: command => command.#getCommandPath(),
|
|
3133
|
+
buildOptionPolicyMap: chain => this.#buildOptionPolicyMap(chain),
|
|
3134
|
+
resolve: (chain, optionTokens, optionPolicyMap) => this.#resolve(chain, optionTokens, optionPolicyMap),
|
|
3135
|
+
parse: (chain, resolveResult, optionPolicyMap, ctx, restArgs) => this.#parse(chain, resolveResult, optionPolicyMap, ctx, restArgs),
|
|
3136
|
+
run: async ({ leafCommand, parseResult, presetResult, ctx }) => runStage({
|
|
3137
|
+
leafCommand,
|
|
3138
|
+
actionParams: this.#contextAdapter.toActionParams(parseResult),
|
|
3139
|
+
tailArgv: presetResult.tailArgv,
|
|
3140
|
+
envs: ctx.envs,
|
|
3141
|
+
hasAction: command => command.#action !== undefined,
|
|
3142
|
+
runAction: async (command, actionParams) => runCommandAction({
|
|
3143
|
+
action: command.#action,
|
|
3144
|
+
actionParams,
|
|
3145
|
+
commandPath: command.#getCommandPath(),
|
|
3146
|
+
}),
|
|
3147
|
+
hasSubcommands: command => command.#subcommandsList.length > 0,
|
|
3148
|
+
renderHelpForDisplay: (command, options) => {
|
|
3149
|
+
const helpColor = command.#resolveHelpColorFromTailArgv(options.tailArgv, options.envs);
|
|
3150
|
+
return command.#formatHelpForDisplay({ color: helpColor });
|
|
3151
|
+
},
|
|
3152
|
+
print: content => {
|
|
3153
|
+
console.log(content);
|
|
3154
|
+
},
|
|
3155
|
+
getCommandPath: command => command.#getCommandPath(),
|
|
3156
|
+
}),
|
|
3157
|
+
issueScopeFromErrorKind: kind => errorKindToIssueScope(kind),
|
|
3158
|
+
},
|
|
3159
|
+
diagnostics: this.#diagnostics,
|
|
3160
|
+
contextAdapter: this.#contextAdapter,
|
|
3161
|
+
});
|
|
3162
|
+
}
|
|
3163
|
+
get name() {
|
|
3164
|
+
return this.#name || undefined;
|
|
3165
|
+
}
|
|
3166
|
+
get description() {
|
|
3167
|
+
return this.#desc;
|
|
3168
|
+
}
|
|
3169
|
+
get version() {
|
|
3170
|
+
return this.#version;
|
|
3171
|
+
}
|
|
3172
|
+
get builtin() {
|
|
3173
|
+
return this.#builtinConfig;
|
|
3174
|
+
}
|
|
3175
|
+
get preset() {
|
|
3176
|
+
return this.#presetConfig === undefined ? undefined : { ...this.#presetConfig };
|
|
3177
|
+
}
|
|
3178
|
+
get parent() {
|
|
3179
|
+
return this.#parent;
|
|
3180
|
+
}
|
|
3181
|
+
get options() {
|
|
3182
|
+
return [...this.#options];
|
|
3183
|
+
}
|
|
3184
|
+
get arguments() {
|
|
3185
|
+
return [...this.#arguments];
|
|
3186
|
+
}
|
|
3187
|
+
get examples() {
|
|
3188
|
+
return this.#examples.map(example => ({ ...example }));
|
|
3189
|
+
}
|
|
3190
|
+
get subcommands() {
|
|
3191
|
+
return new Map(this.#subcommandsMap);
|
|
3192
|
+
}
|
|
3193
|
+
option(opt) {
|
|
3194
|
+
const commandPath = this.#getCommandPath();
|
|
3195
|
+
validateOptionConfig({ opt, commandPath });
|
|
3196
|
+
checkOptionUniqueness({ opt, options: this.#options, commandPath });
|
|
3197
|
+
this.#options.push(opt);
|
|
3198
|
+
return this;
|
|
3199
|
+
}
|
|
3200
|
+
argument(arg) {
|
|
3201
|
+
validateArgumentConfig({
|
|
3202
|
+
arg: arg,
|
|
3203
|
+
arguments_: this.#arguments,
|
|
3204
|
+
commandPath: this.#getCommandPath(),
|
|
3205
|
+
});
|
|
3206
|
+
this.#arguments.push(arg);
|
|
3207
|
+
return this;
|
|
3208
|
+
}
|
|
3209
|
+
action(fn) {
|
|
3210
|
+
this.#action = fn;
|
|
3211
|
+
return this;
|
|
3212
|
+
}
|
|
3213
|
+
example(title, usage, desc) {
|
|
3214
|
+
this.#examples.push(normalizeExample({
|
|
3215
|
+
example: { title, usage, desc },
|
|
3216
|
+
commandPath: this.#getCommandPath(),
|
|
3217
|
+
}));
|
|
3218
|
+
return this;
|
|
3219
|
+
}
|
|
3220
|
+
subcommand(name, cmd) {
|
|
3221
|
+
if (name === 'help') {
|
|
3222
|
+
throw new CommanderError('ConfigurationError', '"help" is a reserved subcommand name', this.#getCommandPath());
|
|
3223
|
+
}
|
|
3224
|
+
if (cmd.#parent && cmd.#parent !== this) {
|
|
3225
|
+
throw new CommanderError('ConfigurationError', `command "${cmd.#name}" already has a parent`, this.#getCommandPath());
|
|
3226
|
+
}
|
|
3227
|
+
const occupied = this.#subcommandsMap.get(name);
|
|
3228
|
+
if (occupied && occupied !== cmd) {
|
|
3229
|
+
throw new CommanderError('ConfigurationError', `subcommand name/alias "${name}" conflicts with an existing command`, this.#getCommandPath());
|
|
3230
|
+
}
|
|
3231
|
+
const existing = this.#subcommandsList.find(e => e.command === cmd);
|
|
3232
|
+
if (existing) {
|
|
3233
|
+
if (existing.name === name || existing.aliases.includes(name)) {
|
|
3234
|
+
return this;
|
|
2212
3235
|
}
|
|
3236
|
+
existing.aliases.push(name);
|
|
3237
|
+
this.#subcommandsMap.set(name, cmd);
|
|
2213
3238
|
}
|
|
2214
|
-
|
|
3239
|
+
else {
|
|
3240
|
+
cmd.#name = name;
|
|
3241
|
+
cmd.#parent = this;
|
|
3242
|
+
this.#subcommandsList.push({ name, aliases: [], command: cmd });
|
|
3243
|
+
this.#subcommandsMap.set(name, cmd);
|
|
3244
|
+
}
|
|
3245
|
+
return this;
|
|
2215
3246
|
}
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
3247
|
+
async run(params) {
|
|
3248
|
+
const outcome = await this.#execute(params, 'run');
|
|
3249
|
+
handleRunOutcome({
|
|
3250
|
+
outcome,
|
|
3251
|
+
argv: params.argv,
|
|
3252
|
+
envs: params.envs,
|
|
3253
|
+
route: argv => this.#route(argv),
|
|
3254
|
+
controlScan: (tailArgv, leafCommand) => this.#controlScan(tailArgv, leafCommand),
|
|
3255
|
+
findCommandByPath: commandPath => this.#findCommandByPath(commandPath),
|
|
3256
|
+
resolveHelpCommand: (leafCommand, helpTarget) => this.#resolveHelpCommand(leafCommand, helpTarget),
|
|
3257
|
+
resolveHelpColor: (command, tailArgv, envs) => command.#resolveHelpColorFromTailArgv(tailArgv, envs),
|
|
3258
|
+
formatHelpForDisplay: (command, color) => command.#formatHelpForDisplay({ color }),
|
|
3259
|
+
print: content => {
|
|
3260
|
+
console.log(content);
|
|
3261
|
+
},
|
|
3262
|
+
printError: content => {
|
|
3263
|
+
console.error(content);
|
|
3264
|
+
},
|
|
3265
|
+
exit: code => this.#exit(code),
|
|
3266
|
+
normalizeControlRunError: error => {
|
|
3267
|
+
const enrichedError = this.#diagnostics.withErrorIssue(error, {
|
|
3268
|
+
stage: 'control-run',
|
|
3269
|
+
scope: 'control',
|
|
3270
|
+
});
|
|
3271
|
+
return this.#diagnostics.normalizeCommanderError(enrichedError, {
|
|
3272
|
+
fallbackStage: 'control-run',
|
|
3273
|
+
fallbackScope: 'control',
|
|
3274
|
+
});
|
|
3275
|
+
},
|
|
3276
|
+
});
|
|
3277
|
+
}
|
|
3278
|
+
#exit(code) {
|
|
3279
|
+
process.exit(code);
|
|
3280
|
+
}
|
|
3281
|
+
async parse(params) {
|
|
3282
|
+
const outcome = await this.#execute(params, 'parse');
|
|
3283
|
+
return unwrapParseOutcome({
|
|
3284
|
+
outcome,
|
|
3285
|
+
commandPath: this.#getCommandPath(),
|
|
3286
|
+
});
|
|
3287
|
+
}
|
|
3288
|
+
async #execute(params, mode) {
|
|
3289
|
+
return this.#kernel.execute(params, mode);
|
|
3290
|
+
}
|
|
3291
|
+
#controlRun(leafCommand, controlScanResult) {
|
|
3292
|
+
return runControl({
|
|
3293
|
+
leafCommand,
|
|
3294
|
+
controlScanResult,
|
|
3295
|
+
resolveHelpCommand: (leaf, helpTarget) => this.#resolveHelpCommand(leaf, helpTarget),
|
|
3296
|
+
getCommandPath: command => command.#getCommandPath(),
|
|
3297
|
+
getCommandVersion: command => command.#version,
|
|
3298
|
+
});
|
|
3299
|
+
}
|
|
3300
|
+
#findCommandByPath(commandPath) {
|
|
3301
|
+
return findCommandByPath({
|
|
3302
|
+
root: this,
|
|
3303
|
+
commandPath,
|
|
3304
|
+
getCommandName: command => command.#name || undefined,
|
|
3305
|
+
getSubcommandEntries: command => command.#subcommandsList,
|
|
3306
|
+
});
|
|
3307
|
+
}
|
|
3308
|
+
formatHelp() {
|
|
3309
|
+
return this.#helpRenderer.formatHelp(this.#buildHelpData());
|
|
3310
|
+
}
|
|
3311
|
+
#formatHelpForDisplay(params = {}) {
|
|
3312
|
+
const { color = true } = params;
|
|
3313
|
+
return this.#helpRenderer.formatHelpForDisplay(this.#buildHelpData(), color);
|
|
3314
|
+
}
|
|
3315
|
+
#buildHelpData() {
|
|
3316
|
+
const subcommands = buildHelpSubcommands({
|
|
3317
|
+
entries: this.#subcommandsList,
|
|
3318
|
+
getDescription: command => command.#desc,
|
|
3319
|
+
});
|
|
3320
|
+
return this.#helpRenderer.buildHelpData({
|
|
3321
|
+
desc: this.#desc,
|
|
3322
|
+
commandPath: this.#getCommandPath(),
|
|
3323
|
+
arguments: this.#arguments,
|
|
3324
|
+
options: this.#resolveOptionPolicy().mergedOptions,
|
|
3325
|
+
supportsBuiltinVersion: this.#supportsBuiltinVersion(),
|
|
3326
|
+
subcommands,
|
|
3327
|
+
examples: this.#examples,
|
|
3328
|
+
builtinHelpOption: BUILTIN_HELP_OPTION,
|
|
3329
|
+
builtinVersionOption: BUILTIN_VERSION_OPTION,
|
|
3330
|
+
});
|
|
3331
|
+
}
|
|
3332
|
+
getCompletionMeta() {
|
|
3333
|
+
return buildCompletionMeta({
|
|
3334
|
+
name: this.#name,
|
|
3335
|
+
desc: this.#desc,
|
|
3336
|
+
mergedOptions: this.#resolveOptionPolicy().mergedOptions,
|
|
3337
|
+
arguments_: this.#arguments,
|
|
3338
|
+
supportsBuiltinVersion: this.#supportsBuiltinVersion(),
|
|
3339
|
+
builtinHelpOption: BUILTIN_HELP_OPTION,
|
|
3340
|
+
builtinVersionOption: BUILTIN_VERSION_OPTION,
|
|
3341
|
+
subcommands: this.#subcommandsList,
|
|
3342
|
+
resolveSubcommandMeta: command => command.getCompletionMeta(),
|
|
3343
|
+
});
|
|
3344
|
+
}
|
|
3345
|
+
#route(argv) {
|
|
3346
|
+
return routeCommandChain({
|
|
3347
|
+
root: this,
|
|
3348
|
+
argv,
|
|
3349
|
+
getSubcommandEntries: command => command.#subcommandsList,
|
|
3350
|
+
});
|
|
3351
|
+
}
|
|
3352
|
+
#controlScan(tailArgv, leafCommand) {
|
|
3353
|
+
return scanControl({
|
|
3354
|
+
tailArgv,
|
|
3355
|
+
supportsBuiltinVersion: leafCommand.#supportsBuiltinVersion(),
|
|
3356
|
+
});
|
|
3357
|
+
}
|
|
3358
|
+
#createContext(params) {
|
|
3359
|
+
const { chain, cmds, envs, reporter } = params;
|
|
3360
|
+
const leafCommand = chain[chain.length - 1];
|
|
3361
|
+
return createCommandContext({
|
|
3362
|
+
leafCommand,
|
|
3363
|
+
chain,
|
|
3364
|
+
cmds,
|
|
3365
|
+
envs,
|
|
3366
|
+
reporter: reporter ?? this.#reporter ?? new Reporter(),
|
|
3367
|
+
});
|
|
3368
|
+
}
|
|
3369
|
+
#resolveHelpCommand(leafCommand, helpTarget) {
|
|
3370
|
+
return resolveHelpCommand({
|
|
3371
|
+
leafCommand,
|
|
3372
|
+
helpTarget,
|
|
3373
|
+
getSubcommandEntries: command => command.#subcommandsList,
|
|
3374
|
+
});
|
|
3375
|
+
}
|
|
3376
|
+
async #preset(controlTailArgv, ctx) {
|
|
3377
|
+
const commandPath = ctx.chain[ctx.chain.length - 1].#getCommandPath();
|
|
3378
|
+
return runPresetStage({
|
|
3379
|
+
controlTailArgv,
|
|
3380
|
+
chain: ctx.chain,
|
|
3381
|
+
commandPath,
|
|
3382
|
+
presetFileFlag: PRESET_FILE_FLAG,
|
|
3383
|
+
presetProfileFlag: PRESET_PROFILE_FLAG,
|
|
3384
|
+
runtime: this.#runtime,
|
|
3385
|
+
userCmds: ctx.sources.user.cmds,
|
|
3386
|
+
userEnvs: ctx.sources.user.envs,
|
|
3387
|
+
getPresetConfig: command => command.#presetConfig,
|
|
3388
|
+
assertPresetProfileSelectorValue: (value, sourceName, path) => this.#presetProfileParser.assertPresetProfileSelectorValue(value, sourceName, path),
|
|
3389
|
+
resolvePresetProfile: params => this.#presetProfileParser.resolvePresetProfile(params),
|
|
3390
|
+
validatePresetOptionTokens: (tokens, filepath, path) => this.#presetProfileParser.validatePresetOptionTokens(tokens, filepath, path),
|
|
3391
|
+
});
|
|
3392
|
+
}
|
|
3393
|
+
#resolve(chain, tokens, optionPolicyMap) {
|
|
3394
|
+
return resolveStage({
|
|
3395
|
+
chain,
|
|
3396
|
+
tokens,
|
|
3397
|
+
optionPolicyMap,
|
|
3398
|
+
mustGetOptionPolicy: (map, command) => this.#mustGetOptionPolicy(map, command),
|
|
3399
|
+
getLocalOptions: command => command.#options,
|
|
3400
|
+
getCommandPath: command => command.#getCommandPath(),
|
|
3401
|
+
withUnknownOptionIssue: (error, token) => this.#diagnostics.withErrorIssue(error, {
|
|
3402
|
+
stage: 'resolve',
|
|
3403
|
+
scope: 'option',
|
|
3404
|
+
token,
|
|
3405
|
+
}),
|
|
3406
|
+
});
|
|
3407
|
+
}
|
|
3408
|
+
#parse(chain, resolveResult, optionPolicyMap, ctx, restArgs) {
|
|
3409
|
+
return parseStage({
|
|
3410
|
+
chain,
|
|
3411
|
+
resolveResult,
|
|
3412
|
+
optionPolicyMap,
|
|
3413
|
+
ctx,
|
|
3414
|
+
restArgs,
|
|
3415
|
+
rootCommandPath: this.#getCommandPath(),
|
|
3416
|
+
mustGetOptionPolicy: (map, command) => this.#mustGetOptionPolicy(map, command),
|
|
3417
|
+
parseOptions: ({ command, tokens, allOptions, envs, commandPath }) => command.#optionParser.parseOptions({
|
|
3418
|
+
tokens,
|
|
3419
|
+
allOptions,
|
|
3420
|
+
envs,
|
|
3421
|
+
commandPath,
|
|
2221
3422
|
}),
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
3423
|
+
applyBuiltinDevmodeLogLevel: ({ command, opts, explicitOptionLongs }) => {
|
|
3424
|
+
command.#optionParser.applyBuiltinDevmodeLogLevel({
|
|
3425
|
+
opts,
|
|
3426
|
+
explicitOptionLongs,
|
|
3427
|
+
builtinOption: command.#builtin.option,
|
|
3428
|
+
hasUserOption: long => command.#hasUserOption(long),
|
|
3429
|
+
});
|
|
3430
|
+
},
|
|
3431
|
+
resolveBuiltinParsedOptions: ({ command, opts }) => command.#optionParser.resolveBuiltinParsedOptions({
|
|
3432
|
+
opts,
|
|
3433
|
+
builtinOption: command.#builtin.option,
|
|
3434
|
+
hasUserOption: long => command.#hasUserOption(long),
|
|
3435
|
+
}),
|
|
3436
|
+
applyOptionCallbacks: ({ opts, allOptions, ctx }) => {
|
|
3437
|
+
for (const option of allOptions) {
|
|
3438
|
+
if (option.apply && opts[option.long] !== undefined) {
|
|
3439
|
+
option.apply(opts[option.long], ctx);
|
|
3440
|
+
}
|
|
3441
|
+
}
|
|
3442
|
+
},
|
|
3443
|
+
getLocalOptions: command => command.#options,
|
|
3444
|
+
getSubcommands: command => command.#subcommandsList,
|
|
3445
|
+
getArguments: command => command.#arguments,
|
|
3446
|
+
getCommandPath: command => command.#getCommandPath(),
|
|
3447
|
+
withUnknownSubcommandIssue: error => this.#diagnostics.withErrorIssue(error, {
|
|
3448
|
+
stage: 'parse',
|
|
3449
|
+
scope: 'command',
|
|
3450
|
+
source: { primary: 'user' },
|
|
2226
3451
|
}),
|
|
3452
|
+
freezeInputSources: sources => this.#freezeInputSources(sources),
|
|
2227
3453
|
});
|
|
2228
3454
|
}
|
|
3455
|
+
#hasUserOption(long) {
|
|
3456
|
+
return this.#options.some(option => option.long === long);
|
|
3457
|
+
}
|
|
3458
|
+
#supportsBuiltinVersion() {
|
|
3459
|
+
return this.#version !== undefined && this.#builtin.option.version;
|
|
3460
|
+
}
|
|
3461
|
+
#resolveOptionPolicy() {
|
|
3462
|
+
return resolveOptionPolicy({
|
|
3463
|
+
builtinOption: this.#builtin.option,
|
|
3464
|
+
localOptions: this.#options,
|
|
3465
|
+
});
|
|
3466
|
+
}
|
|
3467
|
+
#buildOptionPolicyMap(chain) {
|
|
3468
|
+
return buildOptionPolicyMap({
|
|
3469
|
+
chain,
|
|
3470
|
+
resolveOptionPolicy: command => command.#resolveOptionPolicy(),
|
|
3471
|
+
});
|
|
3472
|
+
}
|
|
3473
|
+
#mustGetOptionPolicy(optionPolicyMap, cmd) {
|
|
3474
|
+
return mustGetOptionPolicy({
|
|
3475
|
+
optionPolicyMap,
|
|
3476
|
+
command: cmd,
|
|
3477
|
+
resolveOptionPolicy: command => command.#resolveOptionPolicy(),
|
|
3478
|
+
});
|
|
3479
|
+
}
|
|
3480
|
+
#resolveHelpColorFromTailArgv(tailArgv, envs, policy = this.#resolveOptionPolicy()) {
|
|
3481
|
+
return this.#helpRenderer.resolveHelpColorFromTailArgv({
|
|
3482
|
+
tailArgv,
|
|
3483
|
+
envs,
|
|
3484
|
+
options: policy.mergedOptions,
|
|
3485
|
+
commandPath: this.#getCommandPath(),
|
|
3486
|
+
});
|
|
3487
|
+
}
|
|
3488
|
+
#freezeInputSources(sources) {
|
|
3489
|
+
return freezeInputSources(sources);
|
|
3490
|
+
}
|
|
2229
3491
|
#getCommandPath() {
|
|
2230
3492
|
const parts = [];
|
|
2231
3493
|
let current = this;
|
|
@@ -2391,4 +3653,4 @@ class Coerce {
|
|
|
2391
3653
|
|
|
2392
3654
|
setDefaultCommandRuntime(createBrowserCommandRuntime());
|
|
2393
3655
|
|
|
2394
|
-
export { Coerce, Command, CommanderError, createBrowserCommandRuntime, getDefaultCommandRuntime, isDomain, isIp, isIpv4, isIpv6, logColorfulOption, logDateOption,
|
|
3656
|
+
export { Coerce, Command, CommanderError, createBrowserCommandRuntime, getDefaultCommandRuntime, isDomain, isIp, isIpv4, isIpv6, logColorfulOption, logDateOption, setDefaultCommandRuntime, silentOption };
|