I'm trying to make use of the flag package. My whole issue is that I need to specify groups/multiple values for the same parameter.
For example I need to parse a command as below:
go run mycli.go -action first -point 10 -action
second -point 2 -action 3rd -point something
I need to retrieve each group of action/point param. Is it possible?
package main
import (
"flag"
"fmt"
"strconv"
)
// Define a type named "intslice" as a slice of ints
type intslice []int
// Now, for our new type, implement the two methods of
// the flag.Value interface...
// The first method is String() string
func (i *intslice) String() string {
return fmt.Sprintf("%d", *i)
}
// The second method is Set(value string) error
func (i *intslice) Set(value string) error {
fmt.Printf("%s\n", value)
tmp, err := strconv.Atoi(value)
if err != nil {
*i = append(*i, -1)
} else {
*i = append(*i, tmp)
}
return nil
}
var myints intslice
func main() {
flag.Var(&myints, "i", "List of integers")
flag.Parse()
}
Ref: http://lawlessguy.wordpress.com/2013/07/23/filling-a-slice-using-command-line-flags-in-go-golang/
The flag package won't help you. Closest you'll get is the os package:
[jadekler#Jeans-MacBook-Pro:~/go/src]$ go run temp.go asdasd lkjasd -boom bam -hello world -boom kablam
[/var/folders/15/r6j3mdp97p5247bkkj94p4v00000gn/T/go-build548488797/command-line-arguments/_obj/exe/temp asdasd lkjasd -boom bam -hello world -boom kablam]
So, the first runtime flag key would be os.Args[1], the value would be os.Args[2], the next key would be os.Args[3], and so on.
Related
I've defined a custom flag for accepting a slice of strings as such:
type strSliceFlag []string
func (i *strSliceFlag) String() string {
return fmt.Sprint(*i)
}
func (i *strSliceFlag) Set(value string) error {
*i = append(*i, value)
return nil
}
I then parse it with
...
var tags strSliceFlag
flag.Var(&tags, "t", tFlagExpl)
flag.Parse()
...
When I build this program, and run it with the help flag: main -h, it prints out:
Usage of main:
-t value
Test explanation
My question is, where is the word value coming from? I can't find out how to remove it. I think it maybe has something to do with the default value for the flag.
value is the default argument name chosen by flag.UnquoteUsage for custom types (rendered via flag.(*FlagSet).PrintDefaults).
You can override the default with backquotes in your usage text. The backquotes are stripped from usage text. Eg:
package main
import (
"flag"
"fmt"
)
type stringSlice []string
func (s *stringSlice) String() string {
return fmt.Sprint(*s)
}
func (s *stringSlice) Set(v string) error {
*s = append(*s, v)
return nil
}
func main() {
var s stringSlice
flag.Var(&s, "foo", "append a foo to the list")
flag.Var(&s, "foo2", "append a `foo` to the list")
flag.Parse()
}
Running with -h shows how the argument name changes:
Usage of ./flagusage:
-foo value
append a foo to the list
-foo2 foo
append a foo to the list
I'm trying to make a general purpose debug printer for complex data types because %v has a tendency to just print pointer values rather than what they point at. I've got it working with everything up until I have to deal with structs containing reflect.Value fields.
The following demo code runs without error: (https://play.golang.org/p/qvdRKc40R8k)
package main
import (
"fmt"
"reflect"
)
type MyStruct struct {
i int
R reflect.Value
}
func printContents(value interface{}) {
// Omitted: check if value is actually a struct
rv := reflect.ValueOf(value)
for i := 0; i < rv.NumField(); i++ {
fmt.Printf("%v: ", rv.Type().Field(i).Name)
field := rv.Field(i)
switch field.Kind() {
case reflect.Int:
fmt.Printf("%v", field.Int())
case reflect.Struct:
// Omitted: check if field is actually a reflect.Value to an int
fmt.Printf("reflect.Value(%v)", field.Interface().(reflect.Value).Int())
}
fmt.Printf("\n")
}
}
func main() {
printContents(MyStruct{123, reflect.ValueOf(456)})
}
This prints:
i: 123
R: reflect.Value(456)
However, if I change MyStruct's R field name to r, it fails:
panic: reflect.Value.Interface: cannot return value obtained from unexported field or method
Of course, it's rightly failing because this would otherwise be a way to get an unexported field into proper goland, which is a no-no.
But this leaves me in a quandry: How can I gain access to whatever the unexported reflect.Value refers to without using Interface() so that I can walk its contents and print? I've looked through the reflect documentation and haven't found anything that looks helpful...
After some digging, I've found a solution:
The only way to get at the inner reflect.Value is to call Interface() and type assert it, but this will panic if called on an unexported field. The only way around this is to use the unsafe package to clear the read-only flag so that the Interface() method will think it's exported when it's not (basically, we subvert the type system):
type flag uintptr // reflect/value.go:flag
type flagROTester struct {
A int
a int // reflect/value.go:flagStickyRO
int // reflect/value.go:flagEmbedRO
// Note: flagRO = flagStickyRO | flagEmbedRO
}
var flagOffset uintptr
var maskFlagRO flag
var hasExpectedReflectStruct bool
func initUnsafe() {
if field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag"); ok {
flagOffset = field.Offset
} else {
log.Println("go-describe: exposeInterface() is disabled because the " +
"reflect.Value struct no longer has a flag field. Please open an " +
"issue at https://github.com/kstenerud/go-describe/issues")
hasExpectedReflectStruct = false
return
}
rv := reflect.ValueOf(flagROTester{})
getFlag := func(v reflect.Value, name string) flag {
return flag(reflect.ValueOf(v.FieldByName(name)).FieldByName("flag").Uint())
}
flagRO := (getFlag(rv, "a") | getFlag(rv, "int")) ^ getFlag(rv, "A")
maskFlagRO = ^flagRO
if flagRO == 0 {
log.Println("go-describe: exposeInterface() is disabled because the " +
"reflect flag type no longer has a flagEmbedRO or flagStickyRO bit. " +
"Please open an issue at https://github.com/kstenerud/go-describe/issues")
hasExpectedReflectStruct = false
return
}
hasExpectedReflectStruct = true
}
func canExposeInterface() bool {
return hasExpectedReflectStruct
}
func exposeInterface(v reflect.Value) interface{} {
pFlag := (*flag)(unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + flagOffset))
*pFlag &= maskFlagRO
return v.Interface()
}
There are caveats, in that unsafe isn't allowed or desirable in all environments, not to mention that subverting the type system is rarely the right thing to do. It's recommended that you make such code conditional on build tags, and include a safe alternative.
In Golang, is it possible to change a pointer parameter's value to something else?
For example,
func main() {
i := 1
test(&i)
}
func test(ptr interface{}) {
v := reflect.ValueOf(ptr)
fmt.Println(v.CanSet()) // false
v.SetInt(2) // panic
}
https://play.golang.org/p/3OwGYrb-W-
Is it possible to have test() change i to point to another value 2?
Not sure if this is what you were looking for,
but yes you can change a pointer's value to something else.
The code below will print 2 and 3:
package main
import (
"fmt"
)
func main() {
i := 1
testAsAny(&i)
fmt.Println(i)
testAsInt(&i)
fmt.Println(i)
}
func testAsAny(ptr interface{}) {
*ptr.(*int) = 2
}
func testAsInt(i *int) {
*i = 3
}
Here's now to set the value using the reflect package. The key point is to set the pointer's element, not the pointer itself.
func test(ptr interface{}) {
v := reflect.ValueOf(ptr).Elem()
v.SetInt(2)
}
playground example
Note that the reflect package is not needed for this specific example as shown in another answer.
For some reason, it appears that adding new element to slice using reflection doesn't update slice itself. This is the code to demonstrate:
package main
import (
"fmt"
"reflect"
)
func appendToSlice(arrPtr interface{}) {
valuePtr := reflect.ValueOf(arrPtr)
value := valuePtr.Elem()
value = reflect.Append(value, reflect.ValueOf(55))
fmt.Println(value.Len()) // prints 1
}
func main() {
arr := []int{}
appendToSlice(&arr)
fmt.Println(len(arr)) // prints 0
}
Playground link : https://play.golang.org/p/j3532H_mUL
Is there something I'm missing here?
reflect.Append works like append in that it returns a new slice value.
You are assigning this value to the value variable in the appendToSlice function, which replaces the previous reflect.Value, but does not update the original argument.
To make it more clear what's happening, take the equivalent function to your example without reflection:
func appendToSlice(arrPtr *[]int) {
value := *arrPtr
value = append(value, 55)
fmt.Println(len(value))
}
What you need to use is the Value.Set method to update the original value:
func appendToSlice(arrPtr interface{}) {
valuePtr := reflect.ValueOf(arrPtr)
value := valuePtr.Elem()
value.Set(reflect.Append(value, reflect.ValueOf(55)))
fmt.Println(value.Len())
}
https://play.golang.org/p/Nhabg31Sju
package main
import "fmt"
import "reflect"
type Foo struct {
Name string
}
func main() {
_type := []Foo{}
fmt.Printf("_type: v(%v) T(%T)\n", _type, _type)
reflection := reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(_type).Elem()), 0, 0)
reflectionValue := reflect.New(reflection.Type())
reflectionValue.Elem().Set(reflection)
slicePtr := reflect.ValueOf(reflectionValue.Interface())
sliceValuePtr := slicePtr.Elem()
sliceValuePtr.Set(reflect.Append(sliceValuePtr, reflect.ValueOf(Foo{"a"})))
sliceValuePtr.Set(reflect.Append(sliceValuePtr, reflect.ValueOf(Foo{"b"})))
sliceValuePtr.Set(reflect.Append(sliceValuePtr, reflect.ValueOf(Foo{"c"})))
values := []Foo{Foo{"d"}, Foo{"e"}}
for _, val := range values {
sliceValuePtr.Set(reflect.Append(sliceValuePtr, reflect.ValueOf(val)))
}
result := sliceValuePtr.Interface()
fmt.Printf("result: %T = (%v)\n", result, result)
}
take a look at: https://play.golang.org/p/vXOqTVSEleO
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
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()
}
You can at least have a list of arguments on the end of you command by using the flag.Args() function.
package main
import (
"flag"
"fmt"
)
var one string
func main() {
flag.StringVar(&one, "o", "default", "arg one")
flag.Parse()
tail := flag.Args()
fmt.Printf("Tail: %+q\n", tail)
}
my-go-app -o 1 this is the rest will print Tail: ["this" "is" "the" "rest"]
Use flag.String() to get the entire list of values for the argument you need and then split it up into individual items with strings.Split().
If you have a series of integer values at the end of the command line, this helper function will properly convert them and place them in a slice of ints:
package main
import (
"flag"
"fmt"
"strconv"
)
func GetIntSlice(i *[]string) []int {
var arr = *i
ret := []int{}
for _, str := range arr {
one_int, _ := strconv.Atoi(str)
ret = append(ret, one_int)
}
return ret
}
func main() {
flag.Parse()
tail := flag.Args()
fmt.Printf("Tail: %T, %+v\n", tail, tail)
intSlice := GetIntSlice(&tail)
fmt.Printf("intSlice: %T, %+v\n", intSlice, intSlice)
}
mac:demoProject sx$ go run demo2.go 1 2 3 4
Tail: []string, [1 2 3 4]
intSlice: []int, [1 2 3 4]