What is Golang's equivalent of the below python commands ?
import argparse parser = argparse.ArgumentParser(description="something") parser.add_argument("-getList1",nargs='*',help="get 0 or more values") parser.add_argument("-getList2",nargs='?',help="get 1 or more values")
I have seen that the flag package allows argument parsing in Golang. But it seems to support only String, Int or Bool. How to get a list of values into a flag in this format :
go run myCode.go -getList1 value1 value2
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.
Command-line flags Bool() , flag.Int() , etc. After you've defined your flags, you need to call flag. Parse() . This call will execute command-line parsing and will let you use the flags.
To access all command-line arguments in their raw format, we need to use Args variables imported from the os package . This type of variable is a string ( [] ) slice. Args is an argument that starts with the name of the program in the command-line. The first value in the Args slice is the name of our program, while os.
Golang flag package implements command-line flag parsing. Command-line flags are the common way to specify options for command-line programs. We can define flags using flag.String (), flag.Bool (), flag.Int (), etc. This declares the integer flag, -flagname, stored in the pointer ip, with type *int.
Using the flag package involves three steps: First, define variables to capture flag values, then define the flags your Go application will use, and finally, parse the flags provided to the application upon execution. Most of the functions within the flag package are concerned with defining flags and binding them to variables that you have defined.
Go flag.String & flag.Int The flag.String defines a string flag with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the flag. The flag.Int defines an int flag with specified name, default value, and usage string.
Command-line flags are the common way to specify options for command-line programs. We can define flags using flag.String (), flag.Bool (), flag.Int (), etc. This declares the integer flag, -flagname, stored in the pointer ip, with type *int. Add import “flag” to the import section of your package, and it’s ready to use.
You can define your own flag.Value
and use flag.Var()
for binding it.
The example is here.
Then you can pass multiple flags like following:
go run your_file.go --list1 value1 --list1 value2
UPD: including code snippet right there just in case.
package main import "flag" type arrayFlags []string func (i *arrayFlags) String() string { return "my string representation" } func (i *arrayFlags) Set(value string) error { *i = append(*i, value) return nil } var myFlags arrayFlags func main() { flag.Var(&myFlags, "list1", "Some description for this param.") flag.Parse() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With