Golang has a built-in package called flag
that enables one to parse command-line flags. The flag
package allows you to define String
, Bool
, or Int
flags.
To be able to access the parameters passed as arguments to the command line execution of your program, three steps need to be completed.
- Define the flag.
- Bind the flag.
- Parse the flag.
Syntax
An example of how flag
is used with command-line arguments is shown below.
flag.Int("n", def_val, description) |
Parameters
"n"
: the variable to be parsed.def_val
: the default value of the variable.description
: the description of the flag.
Define the flag
var help = flag.Bool("help", false, "Show help") | |
var boolFlag = false | |
var stringFlag = "Hello There!" | |
var intFlag int |
Bind the flag
flag.BoolVar(&boolFlag, "boolFlag", false, "A boolean flag") | |
flag.StringVar(&stringFlag, "stringFlag", "Hello There!", "A string flag") | |
flag.IntVar(&intFlag, "intFlag", 4, "An integer flag") |
Parse the flag
flag.Parse() |
Bringing it all together
package main | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
) | |
// Define the flag | |
var help = flag.Bool("help", false, "Show help") | |
var boolFlag = false | |
var stringFlag = "Hello There!" | |
var intFlag int | |
func main() { | |
// Bind the flag | |
flag.BoolVar(&boolFlag, "boolFlag", false, "A boolean flag") | |
flag.StringVar(&stringFlag, "stringFlag", "Hello There!", "A string flag") | |
flag.IntVar(&intFlag, "intFlag", 4, "An integer flag") | |
// Parse the flag | |
flag.Parse() | |
// Usage Demo | |
if *help { | |
flag.Usage() | |
os.Exit(0) | |
} | |
fmt.Println("Boolean Flag is ", boolFlag) | |
fmt.Println("String Flag is ", stringFlag) | |
fmt.Println("Int Flag is ", intFlag) | |
} |
To run the code in the terminal, the following command is added.
./main -boolFlag=true -stringFlag hi
You can use -h
or --help
flags to get automatically generated help text for the command-line program.
Checking if the flag is set explicitly.
If you would like to check explicitly if a flag has been set in Go, you can use the flag.Visit()
function in Go.
func isFlagPassed(name string) bool { | |
found := false | |
flag.Visit(func(f *flag.Flag) { | |
if f.Name == name { | |
found = true | |
} | |
}) | |
return found | |
} |
This function needs to be called after the flag.Parse()
package main | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
) | |
// Define the flag | |
var help = flag.Bool("help", false, "Show help") | |
var boolFlag = false | |
var stringFlag = "Hello There!" | |
var intFlag int | |
func main() { | |
// Bind the flag | |
flag.BoolVar(&boolFlag, "boolFlag", false, "A boolean flag") | |
flag.StringVar(&stringFlag, "stringFlag", "Hello There!", "A string flag") | |
flag.IntVar(&intFlag, "intFlag", 4, "An integer flag") | |
// Parse the flag | |
flag.Parse() | |
if !isFlagPassed("boolFlag") { | |
fmt.Println("BoolFlag is not passed !!!") | |
} | |
// Usage Demo | |
if *help { | |
flag.Usage() | |
os.Exit(0) | |
} | |
fmt.Println("Boolean Flag is ", boolFlag) | |
fmt.Println("String Flag is ", stringFlag) | |
fmt.Println("Int Flag is ", intFlag) | |
} | |
func isFlagPassed(name string) bool { | |
found := false | |
flag.Visit(func(f *flag.Flag) { | |
if f.Name == name { | |
found = true | |
} | |
}) | |
return found | |
} |
If the boolFlag flag is not passed, the output of the above code would be
BoolFlag is not passed !!! | |
Boolean Flag is false | |
String Flag is Hello There! | |
Int Flag is 4 |