命令解析
先选择命令,再执行命令,可以构建帮助、记录选择结果或实现自定义路由。普通 CLI 可从 createTerminalApp() 或 createCommand().registry(...) 开始。
直接定义命令
defineCommand(...) 是 command.define(...) 的直接形式。本例将定义放入注册表,用于独立的查找函数。
import {
createCommands,
defineCommand,
defineCommandRegistry,
type CommandAcceptedPath,
type CommandName
} from 'icore';
type PublicCommandName = CommandName<typeof runJobCommand>;
const listJobsCommand = defineCommand({
path: ['jobs', 'list'],
options: {
status: {
type: 'string',
choices: ['queued', 'running', 'done', 'failed']
}
} as const,
handle({ options }) {
return `status=${options.status ?? 'any'}\n`;
}
});
const runJobCommand = defineCommand({
path: ['jobs', 'run'],
options: {
'job-id': {
type: 'string',
required: true
}
} as const,
handle({ options }) {
return `run ${options['job-id']}\n`;
}
});
const registry = defineCommandRegistry([
listJobsCommand,
runJobCommand
] as const);
PublicCommandName 从命令路径推断得出。
type PublicCommandName = 'jobs run';
要使用 commands.prepare(...) 和 commands.run(...),可以通过 createCommands(...) 创建命令对象。
为命令指定别名
在主命令上声明备用路径。它们共享主命令的选项、prepare 处理函数、说明和命令函数。
type AcceptedAccountPath =
CommandAcceptedPath<typeof listAccountsCommand>;
const listAccountsCommand = defineCommand({
path: ['account', 'list'],
aliases: [
['users', 'get-accounts'],
['accounts']
],
options: {
format: {
type: 'string',
choices: ['table', 'json'],
default: 'table'
}
} as const,
handle({ options }) {
return `format=${options.format}\n`;
}
});
const accountCommands = createCommands([
listAccountsCommand
] as const);
const canonical = accountCommands.resolve([
'account',
'list'
]);
const aliased = accountCommands.resolve([
'users',
'get-accounts'
]);
name 和 path 始终描述主命令。matchedPath 保存用户输入的路径。
canonical.name;
canonical.path;
canonical.matchedPath; // ['account', 'list']
aliased.name;
aliased.path;
aliased.matchedPath;
使用主名称时,满足 matchedPath === path。TypeScript 将 AcceptedAccountPath 推断为主路径及两个别名的联合类型。accountCommands.names 只包含 'account list',而 accountCommands.definitions 只有一个定义。解析会选择最长的匹配路径。两个定义的路径完全相同时会触发 DUPLICATE_COMMAND。
遇到意外的位置参数时,消息会显示用户输入的别名。UNEXPECTED_POSITIONAL 的详情中,command 保存主名称,matchedPath 保存输入路径。
从现有定义构建命令对象
createCommands(...) 创建的命令对象与 createCommand().registry(...) 相同。可以传入在其他模块中准备的定义。
const commands = createCommands([
listJobsCommand,
runJobCommand
] as const);
commands.names;
commands.registry;
根据位置参数选择命令
当命令和子命令已分离为独立参数时,commands.resolve(...) 适合用于帮助等场合。它查找给定的路径。选项在命令准备阶段验证。
const resolved = commands.resolve([
'jobs',
'run'
]);
resolved.name;
resolved.path;
resolved.matchedPath;
resolved.positionals;
结果包含命令名称。
jobs run
根据原始参数选择命令
commands.resolveFromArgs(...) 和 resolveCommandFromArgs(...) 根据命令模式解析终端原始参数。
import { resolveCommandFromArgs } from 'icore';
const resolved = commands.resolveFromArgs([
'jobs',
'run',
'--job-id',
'job-42'
]);
const sameResolved = resolveCommandFromArgs(commands.registry, [
'jobs',
'run',
'--job-id',
'job-42'
]);
在一次非严格查找中,无论有多少别名,每个主命令的选项最多解析一次。其他命令的选项解析错误仍允许查找到匹配的命令。所选命令的解析错误以及无效的选项定义会导致失败。如果没有命令匹配,则报告已捕获的解析错误(如果有)。
默认情况下,选项可以放在命令名前。
await accountCommands.prepare([
'--format=json',
'users',
'get-accounts'
]);
如果 help 将 --verbose 定义为布尔标志并接受位置参数,调用 --verbose help status 会将 verbose: true 和 positionals: ['status'] 传给其处理函数。如果 system status 是 status 的别名,调用 --verbose help system status 会将 positionals: ['system', 'status'] 传给 help。两种注册顺序的结果相同。
在 strict: true 模式下,主名称或别名必须放在开头。解析全局选项会保留参数原有顺序。
await accountCommands.prepare([
'users',
'get-accounts',
'--format=json'
], {
strict: true
});
使用独立查找函数
resolveCommand(...) 和 commands.resolve(...) 采用相同的名称查找方式。只需查找时传入注册表即可。
import { resolveCommand } from 'icore';
const resolved = resolveCommand(registry, [
'jobs',
'list'
]);
检查命令名称
isCommandName(...) 检查未知值是否为注册表中的主名称。可以据此选择显示命令帮助还是通用帮助。别名可通过命令定义和 matchedPath 取得。有关验证和运行,请参阅分两步执行。
import { isCommandName } from 'icore';
function renderHelpPage(name: unknown): string {
if (!isCommandName(commands.registry, name)) {
return renderTopLevelHelp();
}
return renderCommandHelp(name);
}