refactor: change CommandSchema's options/flags to be Records

This commit is contained in:
2026-04-02 08:29:40 +08:00
parent f1b1741db8
commit 281cbf845d
7 changed files with 64 additions and 101 deletions
+2 -2
View File
@@ -71,7 +71,7 @@ function commandMatchesSchema(command: Command, schema: CommandSchema): boolean
return false;
}
const requiredOptions = schema.options.filter(o => o.required);
const requiredOptions = Object.values(schema.options).filter(o => o.required);
for (const opt of requiredOptions) {
const hasOption = opt.name in command.options || (opt.short && opt.short in command.options);
if (!hasOption) {
@@ -131,7 +131,7 @@ function handleGeneratorResult<T>(
ctx.state = 'invoking';
return invokeChildRule(host, yielded.rule, yielded.command, ctx as RuleContext<unknown>);
} else {
ctx.schema = parseYieldedSchema({ name: '', params: [], options: [], flags: [] });
ctx.schema = parseYieldedSchema({ name: '', params: [], options: {}, flags: {} });
ctx.state = 'yielded';
}
} else {
+2 -2
View File
@@ -19,7 +19,7 @@ function validateCommandCore(command: Command, schema: CommandSchema): string[]
errors.push(`参数过多:最多 ${schema.params.length} 个参数,实际 ${command.params.length}`);
}
const requiredOptions = schema.options.filter(o => o.required);
const requiredOptions = Object.values(schema.options).filter(o => o.required);
for (const opt of requiredOptions) {
const hasOption = opt.name in command.options || (opt.short && opt.short in command.options);
if (!hasOption) {
@@ -58,7 +58,7 @@ export function applyCommandSchema(
const parsedOptions: Record<string, unknown> = { ...command.options };
for (const [key, value] of Object.entries(command.options)) {
const optSchema = schema.options.find(o => o.name === key || o.short === key);
const optSchema = schema.options[key] ?? (key.length === 1 ? Object.values(schema.options).find(o => o.short === key) : undefined);
if (optSchema?.schema && typeof value === 'string') {
try {
parsedOptions[key] = optSchema.schema.parse(value);
+14 -14
View File
@@ -5,8 +5,8 @@ export function parseCommandSchema(schemaStr: string, name?: string): CommandSch
const schema: CommandSchema = {
name: name ?? '',
params: [],
options: [],
flags: [],
options: {},
flags: {},
};
const tokens = tokenizeSchema(schemaStr);
@@ -27,28 +27,28 @@ export function parseCommandSchema(schemaStr: string, name?: string): CommandSch
if (inner.startsWith('--')) {
const result = parseOptionToken(inner.slice(2), false);
if (result.isFlag) {
schema.flags.push({ name: result.name, short: result.short });
schema.flags[result.name] = { name: result.name, short: result.short };
} else {
schema.options.push({
schema.options[result.name] = {
name: result.name,
short: result.short,
required: false,
defaultValue: result.defaultValue,
schema: result.schema,
});
};
}
} else if (inner.startsWith('-') && inner.length > 1 && !inner.includes('--')) {
const result = parseOptionToken(inner.slice(1), false);
if (result.isFlag) {
schema.flags.push({ name: result.name, short: result.short || result.name });
schema.flags[result.name] = { name: result.name, short: result.short || result.name };
} else {
schema.options.push({
schema.options[result.name] = {
name: result.name,
short: result.short || result.name,
required: false,
defaultValue: result.defaultValue,
schema: result.schema,
});
};
}
} else {
const isVariadic = inner.endsWith('...');
@@ -78,29 +78,29 @@ export function parseCommandSchema(schemaStr: string, name?: string): CommandSch
} else if (token.startsWith('--')) {
const result = parseOptionToken(token.slice(2), true);
if (result.isFlag) {
schema.flags.push({ name: result.name, short: result.short });
schema.flags[result.name] = { name: result.name, short: result.short };
} else {
schema.options.push({
schema.options[result.name] = {
name: result.name,
short: result.short,
required: true,
defaultValue: result.defaultValue,
schema: result.schema,
});
};
}
i++;
} else if (token.startsWith('-') && token.length > 1 && !/^-?\d+$/.test(token)) {
const result = parseOptionToken(token.slice(1), true);
if (result.isFlag) {
schema.flags.push({ name: result.name, short: result.short || result.name });
schema.flags[result.name] = { name: result.name, short: result.short || result.name };
} else {
schema.options.push({
schema.options[result.name] = {
name: result.name,
short: result.short || result.name,
required: true,
defaultValue: result.defaultValue,
schema: result.schema,
});
};
}
i++;
} else if (token.startsWith('<') && token.endsWith('>')) {
+2 -2
View File
@@ -30,8 +30,8 @@ export type CommandFlagSchema = {
export type CommandSchema = {
name: string;
params: CommandParamSchema[];
options: CommandOptionSchema[];
flags: CommandFlagSchema[];
options: Record<string, CommandOptionSchema>;
flags: Record<string, CommandFlagSchema>;
}
export interface ParsedOptionResult {