SuperAPI AI 工具集

oclif-patterns

编程与开发

查看项目

更新于 10-16 04:16

技能介绍

oclif-patterns 提供使用 oclif 框架构建生产级命令行工具(CLI)的综合模式,基于 TypeScript。

用途 该技能面向企业级 CLI 开发,帮助开发者按照统一模式创建命令、插件、测试和文档。适用于需要构建可扩展、可维护的命令行工具的场景。

主要能力

  1. 命令结构:支持单命令与多命令 CLI;标志定义(字符串、布尔、整数、自定义);带验证的参数解析;命令继承与基类;异步命令执行。
  2. 插件系统:可安装插件;插件发现与加载;用于扩展的钩子系统;插件命令与生命周期。
  3. 自动文档:自动生成帮助文本、README、命令参考文档、标志与参数文档。
  4. 测试模式:命令单元测试、集成测试、模拟 stdin/stdout、fixture 管理、测试辅助工具。

使用方式 命令创建:使用模板 templates/command-basic.ts(带标志的简单命令)、templates/command-advanced.ts(带验证的复杂命令)、templates/command-async.ts(异步操作)、templates/base-command.ts(自定义基类)。关键模式包括:从 '@oclif/core' 导入 Command;使用 Flags 对象定义标志;使用 Args 对象定义参数;实现 async run() 方法;使用 this.log() 输出;使用 this.error() 处理错误。

标志模式:字符串 Flags.string({ description, required, default });布尔 Flags.boolean({ description, allowNo });整数 Flags.integer({ description, min, max });自定义 Flags.custom<T>({ parse: async (input) => T });多值 Flags.string({ multiple: true })。最佳实践:始终提供清晰描述;为常用短标志使用 char;显式设置 required 与 optional;提供合理默认值;在自定义标志的 parse 函数中验证。

参数模式:定义 static args = { name: Args.string({ description: 'Name', required: true }), file: Args.file({ description: 'File path', exists: true }) };在 run() 中通过 const { args } = await this.parse(MyCommand) 访问。

插件开发:使用模板 templates/plugin-package.json、templates/plugin-command.ts、templates/plugin-hooks.ts。插件结构为 my-plugin/ 下包含 package.json(oclif 配置)、src/commands/(插件命令)、src/hooks/(生命周期钩子)、test/(插件测试)、README.md。

测试设置:使用模板 templates/test-command.ts、templates/test-helpers.ts、templates/test-setup.ts。测试方法:使用 @oclif/test 测试辅助;用 fancy-test 模拟 stdin/stdout;单独测试标志解析;测试命令执行;测试错误处理;对文件操作使用 fixture。

自动文档:通过 --help 标志生成命令帮助;生成含命令参考的 README.md;用法示例;标志与参数表格。使用脚本 scripts/generate-docs.sh 生成全部文档,scripts/update-readme.sh 更新 README。

快速开始示例:创建基本命令 ./scripts/create-command.sh my-command basic,结果在 src/commands/my-command.ts;创建插件 ./scripts/create-plugin.sh my-plugin,生成插件目录结构;运行测试 npm test 或 npm run test:coverage。

验证脚本:scripts/validate-command.sh 检查命令结构;scripts/validate-plugin.sh 验证插件结构;scripts/validate-tests.sh 确保测试覆盖。

模板参考:TypeScript 命令包括 command-basic.ts、command-advanced.ts、command-async.ts、base-command.ts、command-with-config.ts;插件系统包括 plugin-package.json、plugin-command.ts、plugin-hooks.ts、plugin-manifest.json;测试包括 test-command.ts、test-helpers.ts、test-setup.ts、test-integration.ts;配置包括 tsconfig.json、package.json、.eslintrc.json。示例目录 examples/ 包含 basic-cli/、plugin-cli/、enterprise-cli/。

常见模式:错误处理 this.error('Invalid input', { exit: 1 });加载指示器 const spinner = ux.action.start('Processing') 与 ux.action.stop();提示 const answer = await ux.prompt('Continue?');表格输出 ux.table(data, { columns: [...] })。

要求:Node.js 18+;TypeScript 5+;@oclif/core ^3.0.0;@oclif/test 用于测试;了解 TypeScript 装饰器(可选但有帮助)。

最佳实践:命令设计保持专注、单一职责;标志使用描述性名称并提供帮助文本;测试分别测试命令解析与执行;文档让 oclif 生成并保持更新;插件从一开始就为扩展性设计;错误消息提供可操作信息;TypeScript 使用严格模式并定义合适类型;异步使用 async/await 并正确处理 promise。

高级功能:自定义标志类型,为复杂验证创建可复用的自定义标志解析器;钩子系统,实现 init、prerun、postrun、command_not_found 钩子;主题命令,将命令组织为主题(如 mycli topic:command);自动更新,使用 @oclif/plugin-update 实现 CLI 自动更新;分析,集成分析以跟踪命令使用情况。

限制 资料未提供关于收费、价格、平台支持范围、实际效果或具体使用步骤的更多信息。