unix-cli
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUNIX/POSIX Standards CLI Best Practices
UNIX/POSIX标准CLI最佳实践
Comprehensive guidelines for building command-line tools that follow UNIX conventions, designed for AI agents and LLMs. Contains 44 rules across 8 categories, prioritized by impact from critical (argument handling, exit codes, output streams) to incremental (configuration and environment).
这是为AI Agent和LLM打造的、构建遵循UNIX规范的命令行工具的综合指南。包含8个类别共44条规则,按影响程度从关键级别(参数处理、退出码、输出流)到增量级别(配置与环境)排序。
When to Apply
适用场景
Reference these guidelines when:
- Writing new CLI tools in any language
- Parsing command-line arguments and flags
- Deciding what goes to stdout vs stderr
- Choosing appropriate exit codes
- Handling signals like SIGINT and SIGTERM
在以下场景中参考本指南:
- 用任意语言编写新的CLI工具
- 解析命令行参数与标志
- 决定输出内容发送到stdout还是stderr
- 选择合适的退出码
- 处理SIGINT和SIGTERM等信号
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Argument & Flag Design | CRITICAL | |
| 2 | Exit Codes | CRITICAL | |
| 3 | Output Streams | CRITICAL | |
| 4 | Error Handling | HIGH | |
| 5 | I/O & Composition | HIGH | |
| 6 | Help & Documentation | MEDIUM-HIGH | |
| 7 | Signals & Robustness | MEDIUM | |
| 8 | Configuration & Environment | MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 参数与标志设计 | 关键 | |
| 2 | 退出码 | 关键 | |
| 3 | 输出流 | 关键 | |
| 4 | 错误处理 | 高 | |
| 5 | I/O与组合 | 高 | |
| 6 | 帮助与文档 | 中高 | |
| 7 | 信号与健壮性 | 中 | |
| 8 | 配置与环境 | 中 | |
Quick Reference
快速参考
1. Argument & Flag Design (CRITICAL)
1. 参数与标志设计(关键)
- - Use standard argument parsing libraries
args-use-getopt - - Provide long options for all short options
args-provide-long-options - - Support double-dash to terminate options
args-support-double-dash - - Implement --help and --version options
args-require-help-version - - Prefer flags over positional arguments
args-prefer-flags-over-positional - - Use standard flag names
args-use-standard-flag-names - - Never read secrets from command-line flags
args-never-read-secrets-from-flags - - Support option bundling
args-support-option-bundling
- - 使用标准参数解析库
args-use-getopt - - 为所有短选项提供长选项
args-provide-long-options - - 支持双短横线来终止选项解析
args-support-double-dash - - 实现--help和--version选项
args-require-help-version - - 优先使用标志而非位置参数
args-prefer-flags-over-positional - - 使用标准标志名称
args-use-standard-flag-names - - 绝不从命令行标志中读取敏感信息
args-never-read-secrets-from-flags - - 支持选项捆绑
args-support-option-bundling
2. Exit Codes (CRITICAL)
2. 退出码(关键)
- - Return zero for success only
exit-zero-for-success - - Use standard exit codes
exit-use-standard-codes - - Use 128+N for signal termination
exit-signal-codes - - Handle partial success consistently
exit-partial-success - - Distinguish error types with different exit codes
exit-distinguish-error-types
- - 仅在成功时返回0
exit-zero-for-success - - 使用标准退出码
exit-use-standard-codes - - 信号终止时使用128+N作为退出码
exit-signal-codes - - 一致处理部分成功的场景
exit-partial-success - - 用不同退出码区分错误类型
exit-distinguish-error-types
3. Output Streams (CRITICAL)
3. 输出流(关键)
- - Write data to stdout only
output-stdout-for-data - - Write errors and diagnostics to stderr
output-stderr-for-errors - - Detect TTY for human-oriented output
output-detect-tty - - Provide machine-readable output format
output-provide-machine-format - - Use line-based output for text streams
output-line-based-text - - Respect NO_COLOR environment variable
output-respect-no-color
- - 仅将数据写入stdout
output-stdout-for-data - - 将错误与诊断信息写入stderr
output-stderr-for-errors - - 检测TTY以输出面向人类的内容
output-detect-tty - - 提供机器可读的输出格式
output-provide-machine-format - - 文本流使用基于行的输出
output-line-based-text - - 遵循NO_COLOR环境变量设置
output-respect-no-color
4. Error Handling (HIGH)
4. 错误处理(高)
- - Include program name in error messages
error-include-program-name - - Make error messages actionable
error-actionable-messages - - Use strerror for system errors
error-use-strerror - - Avoid stack traces in user-facing errors
error-avoid-stack-traces - - Validate input early and fail fast
error-validate-early
- - 错误消息中包含程序名称
error-include-program-name - - 使错误消息具备可操作性
error-actionable-messages - - 系统错误使用strerror输出
error-use-strerror - - 面向用户的错误中避免显示堆栈跟踪
error-avoid-stack-traces - - 尽早验证输入并快速失败
error-validate-early
5. I/O & Composition (HIGH)
5. I/O与组合(高)
- - Support reading from stdin
io-support-stdin - - Write output to stdout by default
io-write-to-stdout - - Design stateless operations
io-be-stateless - - Handle binary data safely
io-handle-binary-safely - - Use atomic file writes
io-atomic-writes - - Handle multiple input files consistently
io-handle-multiple-files
- - 支持从stdin读取内容
io-support-stdin - - 默认将输出写入stdout
io-write-to-stdout - - 设计无状态操作
io-be-stateless - - 安全处理二进制数据
io-handle-binary-safely - - 使用原子文件写入
io-atomic-writes - - 一致处理多个输入文件
io-handle-multiple-files
6. Help & Documentation (MEDIUM-HIGH)
6. 帮助与文档(中高)
- - Show brief usage on argument errors
help-show-usage-on-error - - Structure help output consistently
help-structure-help-output - - Show default values in help
help-show-defaults - - Include practical examples in help
help-include-examples - - Format version output correctly
help-version-format
- - 参数错误时显示简要用法
help-show-usage-on-error - - 一致组织帮助输出结构
help-structure-help-output - - 帮助中显示默认值
help-show-defaults - - 帮助中包含实用示例
help-include-examples - - 正确格式化版本输出
help-version-format
7. Signals & Robustness (MEDIUM)
7. 信号与健壮性(中)
- - Handle SIGINT gracefully
signal-handle-sigint - - Handle SIGTERM for clean shutdown
signal-handle-sigterm - - Handle SIGPIPE for broken pipes
signal-handle-sigpipe - - Skip cleanup on second interrupt
signal-cleanup-on-second-interrupt
- - 优雅处理SIGINT信号
signal-handle-sigint - - 处理SIGTERM以实现干净关闭
signal-handle-sigterm - - 处理SIGPIPE以应对管道断裂
signal-handle-sigpipe - - 第二次中断时跳过清理操作
signal-cleanup-on-second-interrupt
8. Configuration & Environment (MEDIUM)
8. 配置与环境(中)
- - Follow XDG Base Directory Specification
config-follow-xdg - - Apply configuration in correct precedence order
config-precedence-order - - Use consistent environment variable naming
config-env-naming - - Never store secrets in config files or environment
config-never-store-secrets - - Respect standard environment variables
config-respect-standard-vars
- - 遵循XDG基础目录规范
config-follow-xdg - - 按正确的优先级顺序应用配置
config-precedence-order - - 使用一致的环境变量命名规则
config-env-naming - - 绝不将敏感信息存储在配置文件或环境变量中
config-never-store-secrets - - 遵循标准环境变量设置
config-respect-standard-vars
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构与影响级别
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |