refactor: change CommandSchema's options/flags to be Records
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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('>')) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user