liveBook · Manning

Flag package

As explained in Go in Practice, Second Edition

The Go flag package parses command-line flags and provides APIs for defining flags and generating their help text.

A small Go command-line program can use the standard-library `flag` package to define options, parse their values, and generate help text. Register each flag before calling `flag.Parse()`, then read the resulting value. section 2.1.1

A greeting with two flags

```go package main import ( "flag" "fmt" ) func main() { name := flag.String("name", "World", "A name to say hello to.") var inSpanish bool flag.BoolVar(&inSpanish, "spanish", false, "Use Spanish language.") flag.BoolVar(&inSpanish, "s", false, "Use Spanish language.") flag.Parse() if inSpanish { fmt.Printf("Hola %s!\n", *name) } else { fmt.Printf("Hello %s!\n", *name) } } ``` `flag.String` returns a pointer, so the greeting reads `*name`. `flag.BoolVar` instead stores a parsed value in an existing variable. Registering both `spanish` and `s` against `inSpanish` provides two explicit names for the same choice. section 2.1.1

Keep the built-in syntax in mind:

  • A flag can begin with `-` or `--`; the package does not assign separate short- and long-option meanings to those prefixes. section 2.1.1
  • Each flag is separate. Grouped short options such as `-la` are not supported by the built-in behavior. section 2.1.1 section 2.1.4
  • Typed registration functions follow a similar pattern for other basic value types; `StringVar` and `IntVar`, for instance, store values in supplied variables. section 2.1.1

Descriptions supplied during registration appear in generated usage output, alongside flag names and applicable defaults. An invalid flag can reveal that output. For a different presentation, `flag.VisitAll` lets you iterate over registered flags and format their names, descriptions, and default values yourself. section 2.1.1

Listing 2.4 Custom flag help text
flag.VisitAll(func(flag *flag.Flag) {
     format := "\t-%s: %s (Default: '%s')\n"
     fmt.Printf(format, flag.Name, flag.Usage, flag.DefValue)
})

The `VisitAll` listing shows custom help formatting with each flag’s `Name`, `Usage`, and `DefValue`. section 2.1.1

For a CLI whose users expect grouped UNIX-style short options or a richer command interface, consider a command-line library rather than maintaining a modified flag parser. Cobra is one shown alternative: it defines explicit long and short flag names and supports commands. section 2.1.4

How Go in Practice, Second Edition covers it

How deep it goes
Works through typed registration, pointer access, parsing, aliases, and help formatting, but leaves richer command interfaces to frameworks.
Examples
Uses a bilingual greeting CLI, invalid-flag usage output, and a `VisitAll` help formatter.
What it assumes
Assumes basic Go variables and functions while demonstrating flag registration, pointer access, and parsing.
What you can do after
Apply it via Go's flag package to parse typed options, add explicit aliases, and format help text.
How it connects
Connects command-line parsing to generated usage text, UNIX-style options, and Cobra.

Also explained in

Compare how 4 books explain flag package →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.