The flag package is Go’s standard-library tool for configuring command-line programs without changing their source code. It defines options, parses terminal input, and exposes the resulting values to the application. section 10.2
A typical CLI uses the package in three stages:
- Define typed options with functions such as flag.Bool, flag.String, and flag.Int. section 10.2
- Call flag.Parse() to process the command-line input. section 10.2
- Read parsed values through the returned variables, then retrieve remaining positional arguments with flag.Args(). section 10.2
JSON/XML conversion CLI
A conversion tool can define -help as a Boolean flag and -banner as an optional string flag, parse them, print help or a banner when requested, and use flag.Args() for input and output filenames. The standard package accepts short and long spellings but does not distinguish between them; advanced aliasing, nested subcommands, and automatic help require packages such as pflag or cobra. section 10.2 section 10.10
Figure 10.5 The complete CLI tool architecture, showing all layers and components
Shows how command-line parsing fits into the complete CLI flow, alongside file handling, format conversion, and validation. section 10.10



