跳到正文
cccskills
免费在 GitHub 公开

building-clis

Build 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 distribution.

中文简介正在准备中。当前显示原文说明。

集成工具GitHubKubernetes
查看安装方法

包含的文件(20)

  • SKILL.md12.5 KB
  • examples/go/cobra_basic.go929 B
  • examples/go/cobra_subcommands.go2.3 KB
  • examples/go/cobra_viper.go2.0 KB
  • examples/python/test_cli.py1.9 KB
  • examples/python/typer_basic.py911 B
  • examples/python/typer_config.py2.2 KB
  • examples/python/typer_progress.py1.1 KB
  • examples/python/typer_subcommands.py2.7 KB
  • examples/rust/clap_builder.rs1.3 KB
  • examples/rust/clap_derive.rs806 B
  • examples/rust/clap_subcommands.rs2.0 KB
  • outputs.yaml8.4 KB
  • references/argument-patterns.md12.9 KB
  • references/configuration-management.md14.2 KB
  • references/distribution.md7.2 KB
  • references/framework-selection.md11.7 KB
  • references/output-formatting.md6.2 KB
  • references/shell-completion.md5.6 KB
  • references/subcommand-design.md13.8 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Building CLIs

Build professional command-line interfaces across Python, Go, and Rust using modern frameworks with robust argument parsing, configuration management, and shell integration.

When to Use This Skill

Use this skill when:

  • Building developer tooling or automation CLIs
  • Creating infrastructure management tools (deployment, monitoring)
  • Implementing API client command-line tools
  • Adding CLI capabilities to existing projects
  • Packaging utilities for distribution (PyPI, Homebrew, binary releases)

Common triggers: "create a CLI tool", "build a command-line interface", "add CLI arguments", "parse command-line options", "generate shell completions"

Framework Selection

Quick Decision Guide

Python Projects:

  • Typer (recommended): Modern type-safe CLIs with minimal boilerplate
  • Click: Mature, flexible CLIs for complex command hierarchies

Go Projects:

  • Cobra (recommended): Industry standard for enterprise tools (Kubernetes, Docker, GitHub CLI)
  • urfave/cli: Lightweight alternative for simple CLIs

Rust Projects:

  • clap v4 (recommended): Type-safe with derive API or builder API for runtime flexibility

For detailed framework comparison and selection criteria, see references/framework-selection.md.

Core Patterns

Arguments vs. Options vs. Flags

Positional Arguments:

  • Primary input, identified by position
  • Use for required inputs (max 2-3 arguments)
  • Example: convert input.jpg output.png

Options:

  • Named parameters with values
  • Use for configuration and optional inputs
  • Example: --output file.txt, --config app.yaml

Flags:

  • Boolean options (presence = true)
  • Use for switches and toggles
  • Example: --verbose, --dry-run, --force

Decision Matrix:

Use CaseTypeExample
Primary required inputPositional Argumentgit commit -m "message"
Optional configurationOption--config app.yaml
Boolean settingFlag--verbose, --force
Multiple valuesVariadic Argumentfiles...

See references/argument-patterns.md for comprehensive parsing patterns.

Subcommand Organization

Flat Structure (1 Level):

app command1 [args]
app command2 [args]

Use for: Small CLIs with 5-10 operations

Grouped Structure (2 Levels):

app group subcommand [args]

Use for: Medium CLIs with logical groupings (10-30 commands) Example: kubectl get pods, kubectl create deployment

Nested Structure (3+ Levels):

app group subgroup command [args]

Use for: Large CLIs with deep hierarchies (30+ commands) Example: gcloud compute instances create

See references/subcommand-design.md for structuring strategies.

Configuration Management

Standard Precedence (Highest to Lowest):

  1. CLI Arguments/Flags (explicit user input)
  2. Environment Variables (session overrides)
  3. Config File - Local (./config.yaml)
  4. Config File - User (~/.config/app/config.yaml)
  5. Config File - System (/etc/app/config.yaml)
  6. Built-in Defaults (hardcoded)

Best Practices:

  • Document precedence in --help
  • Validate config files before execution
  • Provide --print-config to show effective configuration
  • Use XDG Base Directory (~/.config/app/) for config files

See references/configuration-management.md for implementation patterns across languages.

Output Formatting

Format Selection:

Use CaseFormatWhen
Human consumptionColored text, tablesDefault interactive mode
Machine consumptionJSON, YAML--output json, piping
Logging/debuggingPlain text--verbose, stderr
Progress trackingProgress bars, spinnersLong operations

Best Practices:

  • Default to human-readable output
  • Provide --output flag (json, yaml, table)
  • Use stderr for logs, stdout for data
  • Auto-detect TTY (disable colors if not interactive)
  • Use exit codes: 0 = success, 1 = error, 2 = usage error

See references/output-formatting.md for formatting strategies.

Language-Specific Quick Starts

Python with Typer

Installation:

pip install "typer[all]"  # Includes rich for colored output

Basic Example:

import typer
from typing import Annotated

app = typer.Typer()

@app.command()
def greet(
    name: Annotated[str, typer.Argument(help="Name to greet")],
    formal: Annotated[bool, typer.Option(help="Use formal greeting")] = False
):
    """Greet someone with a message."""
    greeting = "Good day" if formal else "Hello"
    typer.echo(f"{greeting}, {name}!")

if __name__ == "__main__":
    app()

Key Features:

  • Type hints for automatic validation
  • Minimal boilerplate with decorators
  • Auto-generated help text
  • Rich integration for colored output

See examples/python/ for complete working examples including subcommands, config management, and interactive features.

Go with Cobra

Installation:

go get -u github.com/spf13/cobra@latest

Basic Example:

var rootCmd = &cobra.Command{
    Use:   "greet [name]",
    Args:  cobra.ExactArgs(1),
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Printf("Hello, %s!\n", args[0])
    },
}

rootCmd.Flags().Bool("formal", false, "Use formal greeting")
rootCmd.Execute()

Key Features:

  • POSIX-compliant flags
  • Viper integration for configuration
  • Subcommand architecture
  • Shell completion generation

See examples/go/ for complete working examples including Viper config and multi-level subcommands.

Rust with clap

Installation (Cargo.toml):

[dependencies]
clap = { version = "4.5", features = ["derive"] }

Basic Example (Derive API):

use clap::Parser;

#[derive(Parser)]
#[command(about = "Greet someone")]
struct Cli {
    /// Name to greet
    name: String,

    /// Use formal greeting
    #[arg(long)]
    formal: bool,
}

fn main() {
    let cli = Cli::parse();
    let greeting = if cli.formal { "Good day" } else { "Hello" };
    println!("{}, {}!", greeting, cli.name);
}

Key Features:

  • Compile-time type safety
  • Derive API (declarative) or Builder API (programmatic)
  • Comprehensive validation
  • Performance optimized

See examples/rust/ for complete working examples including subcommands and builder API patterns.

Interactive Features

Progress Indicators

Python (rich):

from rich.progress import track
for _ in track(range(100), description="Processing..."):
    time.sleep(0.01)

Go (progressbar):

import "github.com/schollz/progressbar/v3"
bar := progressbar.Default(100)
for i := 0; i < 100; i++ {
    bar.Add(1)
}

Rust (indicatif):

use indicatif::ProgressBar;
let bar = ProgressBar::new(100);
for _ in 0..100 {
    bar.inc(1);
}

Prompts and Confirmations

Python:

confirm = typer.confirm("Are you sure?")
if not confirm:
    raise typer.Abort()

Go:

reader := bufio.NewReader(os.Stdin)
fmt.Print("Are you sure? (y/n): ")
response, _ := reader.ReadString('\n')

Rust:

use dialoguer::Confirm;
if Confirm::new().with_prompt("Are you sure?").interact()? {
    // Proceed
}

Shell Completion

Generating Completions

Python (Typer):

_MYAPP_COMPLETE=bash_source myapp > ~/.myapp-complete.bash
_MYAPP_COMPLETE=zsh_source myapp > ~/.myapp-complete.zsh

Go (Cobra):

rootCmd.AddCommand(&cobra.Command{
    Use:   "completion [bash|zsh|fish|powershell]",
    Args:  cobra.ExactArgs(1),
    Run: func(cmd *cobra.Command, args []string) {
        switch args[0] {
        case "bash":
            rootCmd.GenBashCompletion(os.Stdout)
        case "zsh":
            rootCmd.GenZshCompletion(os.Stdout)
        }
    },
})

Rust (clap):

use clap_complete::{generate, shells::Bash};
generate(Bash, &mut Cli::command(), "myapp", &mut io::stdout())

See references/shell-completion.md for installation instructions.

Distribution and Packaging

Python (PyPI)

pyproject.toml:

[project]
name = "myapp"
version = "1.0.0"
scripts = { myapp = "myapp.cli:app" }

Publish:

pip install build twine
python -m build
twine upload dist/*

Go (Homebrew)

Formula:

class Myapp < Formula
  desc "My CLI application"
  url "https://github.com/user/myapp/archive/v1.0.0.tar.gz"

  def install
    system "go", "build", "-o", bin/"myapp"
  end
end

Rust (Cargo)

Publish:

cargo login
cargo publish

Installation:

cargo install myapp

See references/distribution.md for comprehensive packaging strategies including binary releases.

Best Practices

Universal CLI Conventions

Always Provide:

  • --help and -h for usage information
  • --version and -V for version display
  • Clear error messages with actionable suggestions

Argument Handling:

  • Use -- separator for options vs. positional args
  • Support both short (-v) and long (--verbose) forms
  • Validate and sanitize all user inputs

Error Handling:

  • Exit code 0 for success
  • Exit code 1 for general errors
  • Exit code 2 for usage errors
  • Write errors to stderr, data to stdout

Interactivity:

  • Detect TTY (interactive vs. piped input)
  • Provide --yes/--force to skip prompts for automation
  • Show progress for operations longer than 2 seconds

Configuration Best Practices

File Formats:

  • Use YAML, TOML, or JSON consistently
  • Separate files per environment (dev, staging, prod)
  • Validate configuration in CI/CD with --check-config

Secret Management:

  • Never commit secrets to config files
  • Use environment variables or secret managers
  • Document required environment variables

Precedence:

  • CLI args > env vars > config file > defaults
  • Document precedence in help text
  • Provide --print-config to show effective configuration

Integration with Other Skills

testing-strategies:

building-ci-pipelines:

api-patterns:

  • Building API client CLIs
  • Authentication and token management
  • Formatting API responses

secret-management:

  • Secure credential storage
  • Environment variable integration
  • Vault/secrets manager integration

Reference Files

Decision Frameworks:

Implementation Guides:

Code Examples:

Quick Reference

Framework Recommendations:

  • Python: Typer (modern) or Click (mature)
  • Go: Cobra (enterprise) or urfave/cli (simple)
  • Rust: clap v4 (derive or builder)

Common Patterns:

  • Arguments: Primary inputs (max 2-3)
  • Options: Named parameters with values
  • Flags: Boolean switches
  • Subcommands: Group related operations
  • Config: CLI args > env vars > files > defaults

Output Standards:

  • Default: Human-readable (colored, tables)
  • Machine: JSON/YAML via --output flag
  • Errors: stderr, data: stdout
  • Exit: 0 = success, 1 = error, 2 = usage

Distribution:

  • Python: PyPI (pip install)
  • Go: Homebrew, binary releases
  • Rust: Cargo (cargo install), binary releases

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Manage Linux systems covering systemd services, process management, filesystems, networking, performance tuning, and troubleshooting. Use when deploying applications, optimizing server performance, diagnosing production issues, or managing users and security on Linux servers.

中文简介正在准备中。当前显示原文说明。

ancoleman/ai-design-components5252025年12月11日 更新

Data pipelines, feature stores, and embedding generation for AI/ML systems. Use when building RAG pipelines, ML feature serving, or data transformations. Covers feature stores (Feast, Tecton), embedding pipelines, chunking strategies, orchestration (Dagster, Prefect, Airflow), dbt transformations, data versioning (LakeFS), and experiment tracking (MLflow, W&B).

中文简介正在准备中。当前显示原文说明。

ancoleman/ai-design-components5252025年12月11日 更新

Strategic guidance for designing modern data platforms, covering storage paradigms (data lake, warehouse, lakehouse), modeling approaches (dimensional, normalized, data vault, wide tables), data mesh principles, and medallion architecture patterns. Use when architecting data platforms, choosing between centralized vs decentralized patterns, selecting table formats (Iceberg, Delta Lake), or designing data governance frameworks.

中文简介正在准备中。当前显示原文说明。

ancoleman/ai-design-components5252025年12月11日 更新

Design cloud network architectures with VPC patterns, subnet strategies, zero trust principles, and hybrid connectivity. Use when planning VPC topology, implementing multi-cloud networking, or establishing secure network segmentation for cloud workloads.

中文简介正在准备中。当前显示原文说明。

ancoleman/ai-design-components5252025年12月11日 更新

Design comprehensive security architectures using defense-in-depth, zero trust principles, threat modeling (STRIDE, PASTA), and control frameworks (NIST CSF, CIS Controls, ISO 27001). Use when designing security for new systems, auditing existing architectures, or establishing security governance programs.

中文简介正在准备中。当前显示原文说明。

ancoleman/ai-design-components5252025年12月11日 更新

Assembles component outputs from AI Design Components skills into unified, production-ready component systems with validated token integration, proper import chains, and framework-specific scaffolding. Use as the capstone skill after running theming, layout, dashboard, data-viz, or feedback skills to wire components into working React/Next.js, Python, or Rust projects.

中文简介正在准备中。当前显示原文说明。

ancoleman/ai-design-components5252025年12月11日 更新

查看 ancoleman 的全部技能

报告此技能的问题