Scripts2026年7月2日·1 分钟阅读

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.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 29/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Single
信任
信任等级:Established
入口
urfave/cli
安全暂存命令
npx -y tokrepo@latest install 18577bf5-75ce-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产