cli-building
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCLI Building
CLI 构建
Guidelines for building command-line interfaces with modern patterns and best practices.
采用现代模式与最佳实践构建命令行界面的指南。
When to Use
适用场景
- Creating new CLI tools or commands
- Building interactive terminal applications
- Adding commands to existing projects
- Implementing command-line interfaces
- Working with CLI frameworks
- 创建新的CLI工具或命令
- 构建交互式终端应用程序
- 为现有项目添加命令
- 实现命令行界面
- 使用CLI框架进行开发
Core Principles
核心原则
- Async-first: All I/O operations should be async/await, avoid blocking operations
- Composable commands: Commands should be modular and reusable, use command composition
- Strategy pattern: Use strategy pattern for branching workflows or task-based commands
- Output formatting: Proper formatting with unicode symbols and color support
- 异步优先:所有I/O操作均应使用async/await,避免阻塞操作
- 可组合命令:命令应具备模块化和可复用性,采用命令组合方式
- 策略模式:针对分支工作流或基于任务的命令使用策略模式
- 输出格式:使用Unicode符号和颜色支持实现规范的格式
Framework Selection
框架选择
TypeScript/JavaScript
TypeScript/JavaScript
stricli (, recommended for modern async-first CLIs):
@bloomberg/stricli- Built for async/await from ground up
- Type-safe command definitions with full type inference
- Lazy loading for startup performance
- Zero dependencies
oclif (alternative):
- Mature framework with extensive features
- Plugin system
- Good for complex CLIs
stricli(,推荐用于现代异步优先CLI):
@bloomberg/stricli- 从底层为async/await构建
- 具备完整类型推断的类型安全命令定义
- 延迟加载以提升启动性能
- 零依赖
oclif(替代方案):
- 功能丰富的成熟框架
- 插件系统
- 适用于复杂CLI
Python
Python
cyclopts (recommended for async-first):
- Modern async-first CLI framework
- Type-safe with excellent async support
- Clean API design
typer (when fully async support available):
- Based on Python type hints
- Clean and intuitive
- Good for simple to medium complexity CLIs
cyclopts(推荐用于异步优先场景):
- 现代异步优先CLI框架
- 类型安全且具备出色的异步支持
- 简洁的API设计
typer(当需要完整异步支持时):
- 基于Python类型提示
- 简洁直观
- 适用于简单到中等复杂度的CLI
Command Architecture
命令架构
Composable Commands
可组合命令
Design commands as reusable modules:
- Shared command utilities
- Command middleware
- Reusable command modules
- Command composition patterns
将命令设计为可复用模块:
- 共享命令工具
- 命令中间件
- 可复用命令模块
- 命令组合模式
Strategy Pattern
策略模式
Use strategy pattern for:
- Workflow branching
- Task-based commands
- Dynamic command routing
- Conditional command execution
策略模式适用于:
- 工作流分支
- 基于任务的命令
- 动态命令路由
- 条件命令执行
Output Formatting
输出格式
- No emojis: Do not use emojis unless explicitly directed
- Unicode symbols: Use unicode symbols (✓, ✗, →, ⚠) for status indicators
- Color support: Use color libraries, never hardcoded ANSI codes
- NO_COLOR: Always respect environment variable
NO_COLOR - Formatting: Use formatting for better readability (bold, dim, etc.)
- 禁止使用表情符号:除非明确要求,否则不要使用表情符号
- Unicode符号:使用Unicode符号(✓, ✗, →, ⚠)作为状态指示器
- 颜色支持:使用颜色库,切勿硬编码ANSI代码
- NO_COLOR:始终遵循环境变量
NO_COLOR - 格式设置:使用格式设置提升可读性(加粗、变暗等)
Async Patterns
异步模式
Async-First Design
异步优先设计
All I/O should be async:
- File operations: use async file APIs
- Network requests: use async HTTP clients
- Process execution: use async process APIs
- Database operations: use async database clients
所有I/O操作均应采用异步方式:
- 文件操作:使用异步文件API
- 网络请求:使用异步HTTP客户端
- 进程执行:使用异步进程API
- 数据库操作:使用异步数据库客户端
Error Handling
错误处理
Handle async errors properly:
- Use try/catch with await
- Handle promise rejections
- Provide clear error messages
- Exit with appropriate codes
正确处理异步错误:
- 结合await使用try/catch
- 处理Promise拒绝
- 提供清晰的错误消息
- 使用适当的退出码退出
Command Structure
命令结构
Basic Command
基础命令
typescript
// stricli example
import { createCli } from '@bloomberg/stricli';
async function myCommand() {
// Async implementation
}
const cli = createCli({
name: 'my-cli',
commands: {
'my-command': myCommand
}
});
cli.run();python
undefinedtypescript
// stricli example
import { createCli } from '@bloomberg/stricli';
async function myCommand() {
// Async implementation
}
const cli = createCli({
name: 'my-cli',
commands: {
'my-command': myCommand
}
});
cli.run();python
undefinedcyclopts example
cyclopts example
from cyclopts import App
app = App()
@app.default
async def my_command():
# Async implementation
pass
if name == 'main':
app()
undefinedfrom cyclopts import App
app = App()
@app.default
async def my_command():
// Async implementation
pass
if name == 'main':
app()
undefinedBest Practices
最佳实践
- Async by default: All operations should be async
- Composable design: Build reusable command modules
- Strategy pattern: Use for workflow branching
- Proper formatting: Unicode symbols and color with NO_COLOR support
- Error handling: Clear error messages and exit codes
- Type safety: Use TypeScript types or Python type hints
- Testing: Test commands in isolation
- 默认异步:所有操作均应采用异步方式
- 可组合设计:构建可复用的命令模块
- 策略模式:用于工作流分支
- 规范格式:使用Unicode符号和颜色并支持NO_COLOR
- 错误处理:提供清晰的错误消息和退出码
- 类型安全:使用TypeScript类型或Python类型提示
- 测试:独立测试命令
References
参考资料
For detailed guidance, see:
- - Async/await best practices
references/async-patterns.md - - Command composition patterns
references/composable-commands.md - - Strategy pattern for workflows
references/strategy-pattern.md - - Output formatting guidelines
references/output-formatting.md - - Framework comparisons and selection
references/frameworks.md
如需详细指导,请参阅:
- - Async/await最佳实践
references/async-patterns.md - - 命令组合模式
references/composable-commands.md - - 工作流策略模式
references/strategy-pattern.md - - 输出格式指南
references/output-formatting.md - - 框架对比与选择
references/frameworks.md