UNIX-style flag processing parses command-line options using conventions familiar from Linux- and BSD-based tools, including short flags that can be combined, such as ls -la. Go’s built-in flag package provides basics but may not match these interaction expectations. section 2.1.4
A pragmatic implementation strategy is to:
- Use an existing command-line library rather than maintaining a project-specific fork of Go’s flag package. section 2.1.4
- Define long and short aliases, defaults, help text, required flags, commands, and subcommands through the library interface. section 2.1.4
- Use built-in flag and parser tooling when an advanced case only needs to recognize known tokens from a single argument. section 2.1.4
Listing 2.6 Running Hello World in CLI with flags
$ hello_cli Hello World! $ hello_cli --name Inigo Hello Inigo! $ hello_cli –n Inigo Hello Inigo! $ hello_cli –help NAME: hello_cli - Print hello world USAGE: hello_cli [global options] command [command options] [arguments...] VERSION: 0.0.0 COMMANDS: help, h Shows a list of commands or help for one command GLOBAL OPTIONS: --name, -n 'World' Who to say hello to. --help, -h show help --version, -v print the version
A runnable CLI interaction demonstrates long and short name flags, help output, defaults, and command metadata. section 2.1.4
