--- name: cli-design description: Design command-line interfaces — POSIX conventions, subcommand structure, flag naming, --help patterns, stdin/stdout/stderr usage, exit codes, shell completion. license: MIT --- ## What I do Guide CLI design following established conventions. Use when designing new CLIs or improving existing ones. ## Core Principles 1. **Human-first**: Design for humans, machines second 2. **Consistency**: Follow existing conventions 3. **Saying enough**: Not too much, not too little 4. **Ease of discovery**: Help users learn and remember 5. **Conversation**: CLI is a dialogue—errors help users fix mistakes 6. **Robustness**: Handle errors gracefully ## Argument Parsing **Always use a parsing library**: - Python: `argparse`, `click` - Rust: `clap` - Go: `cobra`, `urfav` - JS: `commander`, `yargs` ### Exit Codes - 0: Success - Normal completion - 1: General error - Something went wrong - 2: Misuse - Invalid arguments - 126: Cannot execute - Permission problem - 127: Not found - Command not found - 130: Ctrl+C - Terminated by SIGINT ### stdout vs stderr - stdout: Primary output (the "answer") - stderr: Errors, warnings, progress ```bash echo "Processing..." >&2 # Progress to stderr cat file.json # Data to stdout echo "Error" >&2 # Errors to stderr ``` ## Arguments and Flags ### Best Practices ``` # Bad: Positional args ./deploy production us-east-1 # Good: Flags - self-documenting ./deploy --environment=production --region=us-east-1 ``` ### Standard Flags - `-h`, `--help`: Show help - `--version`: Show version - `-v`, `--verbose`: More output - `-q`, `--quiet`: Less output - `-f`, `--force`: Skip confirmation - `-o`, `--output`: Output file ### Defaults Default should be the right thing for most users: ```bash # Good: Safe default rm --interactive=always file.txt # Bad: Destructive default rm file.txt ``` ### Dangerous Operations Always confirm destructive actions: ```bash rm --force --interactive file.txt # Prompt deploy --force --yes # Skip prompt in scripts ``` ## Help ### Two-Tier Help Default (no args) - concise: ``` $ jq jq - commandline JSON processor [version 1.7] Usage: jq [options] [file...] Example: echo '{"foo":1}' | jq '.foo' Run: jq --help for full options ``` With `-h`/`--help` - full: ``` $ jq --help jq - commandline JSON processor [version 1.7] Usage: jq [options] [file...] Options: -c, --compact Compact output ... ``` ### Help Best Practices - Lead with examples - Make scannable (bold headings, bullets) - Include version ## Output ### Human-Readable by Default ``` $ kubectl get pods NAME READY STATUS RESTARTS AGE myapp 1/1 Running 0 2d ``` ### Machine-Readable on Request ```bash kubectl get pods --json ``` ### Respect Terminal ```python use_color = sys.stdout.isatty() no_color = os.environ.get('NO_COLOR') ``` When piped: - Strip formatting - Output JSON/YAML ## Error Handling ### Rewrite for Humans ``` # Bad: Stack trace $ python script.py Traceback (most recent call last): File "script.py", line 42, in FileNotFoundError: [Errno 2] No such file: 'config.json' # Good: Human-readable $ myapp --config missing.json Error: Config file not found: missing.json The config file must exist. ``` ### Explain the Fix ``` # Bad $ rm /etc/passwd rm: cannot unlink '/etc/passwd': Operation not permitted # Good $ rm /etc/passwd Error: Cannot remove '/etc/passwd': Permission denied Try: sudo rm /etc/passwd ``` ### Suggest Corrections ``` $ git puhs maste git: 'puhs' is not a git command. Did you mean 'push'? ``` ## Interactivity ### Check for TTY ```python if not sys.stdin.isatty(): # Don't prompt in scripts if os.environ.get('NO_INPUT'): # Batch mode ``` ### Passwords ```python import getpass password = getpass.getpass("Password: ") ``` --- Use this skill when designing CLIs.