Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of values into a flag in Golang?

Tags:

go

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  
like image 260
Sidharth C. Nadhan Avatar asked Feb 04 '15 13:02

Sidharth C. Nadhan


People also ask

What is flag parse Golang?

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.

How do you use the flag command?

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.

How do you pass command-line arguments in Golang?

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.

How do you parse flags in Golang?

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.

How do I use the flag package in go?

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.

What is flag INT in go?

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.

How do I create a flag in command line?

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.


1 Answers

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() } 
like image 57
serejja Avatar answered Sep 29 '22 15:09

serejja