UNIX/POSIX Standards CLI Best Practices
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).
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
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|----------|----------|--------|--------|
| 1 | Argument & Flag Design | CRITICAL | args- |
| 2 | Exit Codes | CRITICAL | exit- |
| 3 | Output Streams | CRITICAL | output- |
| 4 | Error Handling | HIGH | error- |
| 5 | I/O & Composition | HIGH | io- |
| 6 | Help & Documentation | MEDIUM-HIGH | help- |
| 7 | Signals & Robustness | MEDIUM | signal- |
| 8 | Configuration & Environment | MEDIUM | config- |
Quick Reference
1. Argument & Flag Design (CRITICAL)
args-use-getopt- Use standard argument parsing librariesargs-provide-long-options- Provide long options for all short optionsargs-support-double-dash- Support double-dash to terminate optionsargs-require-help-version- Implement --help and --version optionsargs-prefer-flags-over-positional- Prefer flags over positional argumentsargs-use-standard-flag-names- Use standard flag namesargs-never-read-secrets-from-flags- Never read secrets from command-line flagsargs-support-option-bundling- Support option bundling
2. Exit Codes (CRITICAL)
exit-zero-for-success- Return zero for success onlyexit-use-standard-codes- Use standard exit codesexit-signal-codes- Use 128+N for signal terminationexit-partial-success- Handle partial success consistentlyexit-distinguish-error-types- Distinguish error types with different exit codes
3. Output Streams (CRITICAL)
output-stdout-for-data- Write data to stdout onlyoutput-stderr-for-errors- Write errors and diagnostics to stderroutput-detect-tty- Detect TTY for human-oriented outputoutput-provide-machine-format- Provide machine-readable output formatoutput-line-based-text- Use line-based output for text streamsoutput-respect-no-color- Respect NO_COLOR environment variable
4. Error Handling (HIGH)
error-include-program-name- Include program name in error messageserror-actionable-messages- Make error messages actionableerror-use-strerror- Use strerror for system errorserror-avoid-stack-traces- Avoid stack traces in user-facing errorserror-validate-early- Validate input early and fail fast
5. I/O & Composition (HIGH)
io-support-stdin- Support reading from stdinio-write-to-stdout- Write output to stdout by defaultio-be-stateless- Design stateless operationsio-handle-binary-safely- Handle binary data safelyio-atomic-writes- Use atomic file writesio-handle-multiple-files- Handle multiple input files consistently
6. Help & Documentation (MEDIUM-HIGH)
help-show-usage-on-error- Show brief usage on argument errorshelp-structure-help-output- Structure help output consistentlyhelp-show-defaults- Show default values in helphelp-include-examples- Include practical examples in helphelp-version-format- Format version output correctly
7. Signals & Robustness (MEDIUM)
signal-handle-sigint- Handle SIGINT gracefullysignal-handle-sigterm- Handle SIGTERM for clean shutdownsignal-handle-sigpipe- Handle SIGPIPE for broken pipessignal-cleanup-on-second-interrupt- Skip cleanup on second interrupt
8. Configuration & Environment (MEDIUM)
config-follow-xdg- Follow XDG Base Directory Specificationconfig-precedence-order- Apply configuration in correct precedence orderconfig-env-naming- Use consistent environment variable namingconfig-never-store-secrets- Never store secrets in config files or environmentconfig-respect-standard-vars- Respect standard environment variables
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 |