Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsJul 2, 2026·3 min de lecture

urfave/cli — Full-Featured CLI Framework for Go

A simple, fast, and fun package for building command-line apps in Go with support for flags, subcommands, shell completion, and middleware.

Prêt pour agents

Staging sûr pour cet actif

Cet actif est d'abord staged. Le prompt copié demande à l'agent d'inspecter les fichiers staged avant d'activer scripts, config MCP ou config globale.

Stage only · 29/100Policy : staging
Surface agent
Tout agent MCP/CLI
Type
CLI Tool
Installation
Single
Confiance
Confiance : Established
Point d'entrée
urfave/cli
Commande de staging sûr
npx -y tokrepo@latest install 18577bf5-75ce-11f1-9bc6-00163e2b0d79 --target codex

Stage les fichiers d'abord; l'activation exige la revue du README et du plan staged.

Introduction

urfave/cli is one of the most widely used packages for building command-line applications in Go. It provides a declarative API for defining commands, flags, arguments, and shell completions without code generation or external tooling.

What urfave/cli Does

  • Defines commands and nested subcommands with a clean declarative struct API
  • Parses typed flags including strings, ints, bools, slices, and maps
  • Generates help text and usage documentation automatically from definitions
  • Provides shell completion scripts for bash, zsh, fish, and PowerShell
  • Supports before/after hooks and middleware for cross-cutting command logic

Architecture Overview

The library centers on the Command struct which holds flags, subcommands, and an Action function. At runtime the parser walks the argument list, matches flags and subcommands, and invokes the matching Action with a populated context. Everything is synchronous and runs in a single goroutine unless your Action launches its own concurrency.

Self-Hosting & Configuration

  • Import the v3 module path for the latest API
  • Define commands as nested Command structs in your main package
  • Set environment variable mappings on flags with the Sources field
  • Generate shell completion scripts with the built-in enable-bash-completion flag
  • No config files or code generation steps are required

Key Features

  • Declarative command tree with unlimited nesting depth
  • Typed flag parsing with automatic environment variable fallback
  • Built-in help, version, and shell completion commands
  • Before and After hooks for setup and teardown logic
  • Lightweight with zero dependencies outside the Go standard library

Comparison with Similar Tools

  • Cobra — uses a builder pattern and code generation; urfave/cli uses declarative structs
  • Kong — struct-tag-based parsing; urfave/cli uses explicit flag definitions
  • Kingpin — archived and unmaintained; urfave/cli is actively developed
  • pflag — flag parsing only; urfave/cli adds commands, help, and completion
  • Bubble Tea — TUI framework for interactive apps; urfave/cli is for traditional CLI parsing

FAQ

Q: How does urfave/cli compare to Cobra? A: Both are mature. Cobra uses builder methods and optional code generation. urfave/cli uses plain struct literals with no generators.

Q: Can I use environment variables for flag defaults? A: Yes. Attach an EnvVars source to any flag and it reads the value at parse time.

Q: Does it support persistent flags across subcommands? A: Flags defined on a parent command are inherited by all subcommands automatically.

Q: Is v3 stable? A: v3 is the current major release and is actively maintained with a stable API.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires