Go flag package¶
The Go flag package (flag) is a standard library package that provides built-in support for command-line argument parsing^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md]. It allows developers to define flags, parse command-line inputs, and handle subcommands in a structured manner^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md].
Usage¶
To use the package, it must be imported^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md].
import (
"flag"
)
Defining Flags¶
The package offers specific functions to define flags of different types, such as Bool, String, and Int^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md]. These functions typically accept a flag name, a default value, and a usage description string, returning a pointer to the variable that will hold the input value^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md].
// Example definitions for a 'get' command
getAll := getCmd.Bool("all", false, "Get all videos")
getID := getCmd.String("id", "", "YouTube video ID")
Subcommands¶
Subcommands (e.g., get, add) are implemented using flag.NewFlagSet^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md]. This function creates a new set of flags associated with a specific name, allowing different commands to have distinct arguments and behaviors^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md].
// Defining a subcommand
getCmd := flag.NewFlagSet("get", flag.ExitOnError)
Routing logic¶
Handling subcommands typically involves inspecting os.Args to determine the user's intent^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md]. A switch statement can be used to route execution based on the argument at a specific index^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md].
switch os.Args[1] {
case "get":
// handle get
case "add":
// handle add
default:
// handle unknown
}
Parsing¶
The Parse method processes the arguments provided to the application^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md]. When working with subcommands, os.Args is typically sliced (e.g., os.Args[2:]) to pass only the relevant portion of the command line to the specific FlagSet^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md].
getCmd.Parse(os.Args[2:])
Validation¶
Input validation ensures that required fields are present and constraints are met^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md].
Checking required fields¶
Validation logic often checks the pointer values returned by flag functions^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md]. For example, to ensure that either an ID is provided or an --all flag is set:
if *all == false && *id == "" {
fmt.Print("id is required or specify --all for all videos")
// ...
}
Displaying usage¶
If validation fails, the PrintDefaults() method outputs the defined flags and their help text to the console^[400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md].
getCmd.PrintDefaults()
Sources¶
400-devops__09-Scripting-Language__golang__introduction__part-4.commandline__readme.md
Related¶
- Go programming language
- [[Command-line interface]]
- [[Docker]]
- [[JSON]]