API Reference

urfave/cli

Declarative Go CLI framework for building commands, flags, and subcommands with a small API surface.

Official Documentation ↗
CompatibilityThis documentation covers version v2. Check the official docs for the latest API surface.

Installation

gogo get github.com/urfave/cli/v2

Methods

Overview

urfave/cli is a declarative Go CLI framework for building commands, flags, and subcommands with a small API surface. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • cli.App — Declares the root application with flags, an action, and subcommands.
  • cli.StringFlag — Declares a string flag with an alias, default value, and help text.
  • app.Run — Parses the arguments and dispatches to the matching action or subcommand.

Example

app := &cli.App{
	Name:  "greet",
	Flags: []cli.Flag{&cli.StringFlag{Name: "name", Value: "world"}},
	Action: func(c *cli.Context) error {
		fmt.Println("hello", c.String("name"))
		return nil
	},
}

Notes

Flags are declared as pointers to typed flag structs and read from cli.Context. v2 replaced v1 global subcommand helpers.

Verification

Run the app with a named flag and confirm the action reads the supplied value through the context.

FAQ

How do I add subcommands?
Populate the Commands slice with *cli.Command values, each with its own Flags and Action.
How do I read a flag inside an action?
Use the cli.Context accessors such as c.String, c.Int, and c.Bool with the flag name.
What changed between v1 and v2?
v2 moves subcommands to a Commands slice and drops the global flag helpers, so v1 code needs modest migration.