$npx -y skills add ancoleman/ai-design-components --skill building-clisBuild professional command-line interfaces in Python, Go, and Rust using modern frameworks like Typer, Cobra, and clap. Use when creating developer tools, automation scripts, or infrastructure management CLIs with robust argument parsing, interactive features, and multi-platform
| 1 | # Building CLIs |
| 2 | |
| 3 | Build professional command-line interfaces across Python, Go, and Rust using modern frameworks with robust argument parsing, configuration management, and shell integration. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Building developer tooling or automation CLIs |
| 9 | - Creating infrastructure management tools (deployment, monitoring) |
| 10 | - Implementing API client command-line tools |
| 11 | - Adding CLI capabilities to existing projects |
| 12 | - Packaging utilities for distribution (PyPI, Homebrew, binary releases) |
| 13 | |
| 14 | Common triggers: "create a CLI tool", "build a command-line interface", "add CLI arguments", "parse command-line options", "generate shell completions" |
| 15 | |
| 16 | ## Framework Selection |
| 17 | |
| 18 | ### Quick Decision Guide |
| 19 | |
| 20 | **Python Projects:** |
| 21 | - **Typer** (recommended): Modern type-safe CLIs with minimal boilerplate |
| 22 | - **Click**: Mature, flexible CLIs for complex command hierarchies |
| 23 | |
| 24 | **Go Projects:** |
| 25 | - **Cobra** (recommended): Industry standard for enterprise tools (Kubernetes, Docker, GitHub CLI) |
| 26 | - **urfave/cli**: Lightweight alternative for simple CLIs |
| 27 | |
| 28 | **Rust Projects:** |
| 29 | - **clap v4** (recommended): Type-safe with derive API or builder API for runtime flexibility |
| 30 | |
| 31 | For detailed framework comparison and selection criteria, see [references/framework-selection.md](references/framework-selection.md). |
| 32 | |
| 33 | ## Core Patterns |
| 34 | |
| 35 | ### Arguments vs. Options vs. Flags |
| 36 | |
| 37 | **Positional Arguments:** |
| 38 | - Primary input, identified by position |
| 39 | - Use for required inputs (max 2-3 arguments) |
| 40 | - Example: `convert input.jpg output.png` |
| 41 | |
| 42 | **Options:** |
| 43 | - Named parameters with values |
| 44 | - Use for configuration and optional inputs |
| 45 | - Example: `--output file.txt`, `--config app.yaml` |
| 46 | |
| 47 | **Flags:** |
| 48 | - Boolean options (presence = true) |
| 49 | - Use for switches and toggles |
| 50 | - Example: `--verbose`, `--dry-run`, `--force` |
| 51 | |
| 52 | **Decision Matrix:** |
| 53 | |
| 54 | | Use Case | Type | Example | |
| 55 | |----------|------|---------| |
| 56 | | Primary required input | Positional Argument | `git commit -m "message"` | |
| 57 | | Optional configuration | Option | `--config app.yaml` | |
| 58 | | Boolean setting | Flag | `--verbose`, `--force` | |
| 59 | | Multiple values | Variadic Argument | `files...` | |
| 60 | |
| 61 | See [references/argument-patterns.md](references/argument-patterns.md) for comprehensive parsing patterns. |
| 62 | |
| 63 | ### Subcommand Organization |
| 64 | |
| 65 | **Flat Structure (1 Level):** |
| 66 | ``` |
| 67 | app command1 [args] |
| 68 | app command2 [args] |
| 69 | ``` |
| 70 | Use for: Small CLIs with 5-10 operations |
| 71 | |
| 72 | **Grouped Structure (2 Levels):** |
| 73 | ``` |
| 74 | app group subcommand [args] |
| 75 | ``` |
| 76 | Use for: Medium CLIs with logical groupings (10-30 commands) |
| 77 | Example: `kubectl get pods`, `kubectl create deployment` |
| 78 | |
| 79 | **Nested Structure (3+ Levels):** |
| 80 | ``` |
| 81 | app group subgroup command [args] |
| 82 | ``` |
| 83 | Use for: Large CLIs with deep hierarchies (30+ commands) |
| 84 | Example: `gcloud compute instances create` |
| 85 | |
| 86 | See [references/subcommand-design.md](references/subcommand-design.md) for structuring strategies. |
| 87 | |
| 88 | ### Configuration Management |
| 89 | |
| 90 | **Standard Precedence (Highest to Lowest):** |
| 91 | |
| 92 | 1. CLI Arguments/Flags (explicit user input) |
| 93 | 2. Environment Variables (session overrides) |
| 94 | 3. Config File - Local (`./config.yaml`) |
| 95 | 4. Config File - User (`~/.config/app/config.yaml`) |
| 96 | 5. Config File - System (`/etc/app/config.yaml`) |
| 97 | 6. Built-in Defaults (hardcoded) |
| 98 | |
| 99 | **Best Practices:** |
| 100 | - Document precedence in `--help` |
| 101 | - Validate config files before execution |
| 102 | - Provide `--print-config` to show effective configuration |
| 103 | - Use XDG Base Directory (`~/.config/app/`) for config files |
| 104 | |
| 105 | See [references/configuration-management.md](references/configuration-management.md) for implementation patterns across languages. |
| 106 | |
| 107 | ### Output Formatting |
| 108 | |
| 109 | **Format Selection:** |
| 110 | |
| 111 | | Use Case | Format | When | |
| 112 | |----------|--------|------| |
| 113 | | Human consumption | Colored text, tables | Default interactive mode | |
| 114 | | Machine consumption | JSON, YAML | `--output json`, piping | |
| 115 | | Logging/debugging | Plain text | `--verbose`, stderr | |
| 116 | | Progress tracking | Progress bars, spinners | Long operations | |
| 117 | |
| 118 | **Best Practices:** |
| 119 | - Default to human-readable output |
| 120 | - Provide `--output` flag (json, yaml, table) |
| 121 | - Use stderr for logs, stdout for data |
| 122 | - Auto-detect TTY (disable colors if not interactive) |
| 123 | - Use exit codes: 0 = success, 1 = error, 2 = usage error |
| 124 | |
| 125 | See [references/output-formatting.md](references/output-formatting.md) for formatting strategies. |
| 126 | |
| 127 | ## Language-Specific Quick Starts |
| 128 | |
| 129 | ### Python with Typer |
| 130 | |
| 131 | **Installation:** |
| 132 | ```bash |
| 133 | pip install "typer[all]" # Includes rich for colored output |
| 134 | ``` |
| 135 | |
| 136 | **Basic Example:** |
| 137 | ```python |
| 138 | import typer |
| 139 | from typing import Annotated |
| 140 | |
| 141 | app = typer.Typer() |
| 142 | |
| 143 | @app.command() |
| 144 | def greet( |
| 145 | name: Annotated[str, typer.Argument(help="Name to greet")], |
| 146 | formal: Annotated[bool, typer.Option(help="Use formal greeting")] = False |
| 147 | ): |
| 148 | """Gre |