本文介绍了如何在Golang中将值列表放入标记中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Golang与以下python命令等效吗?
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")
我已经看到flag包允许在Golang中解析参数.但它似乎仅支持String,Int或Bool.如何以这种格式将值列表转换成标志:
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
推荐答案
您可以定义自己的flag.Value
并使用flag.Var()
进行绑定.
You can define your own flag.Value
and use flag.Var()
for binding it.
示例为此处.
然后您可以传递多个标志,如下所示:
Then you can pass multiple flags like following:
go run your_file.go --list1 value1 --list1 value2
UPD:为了防止万一,请在其中添加代码段.
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()
}
这篇关于如何在Golang中将值列表放入标记中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!