英文:
How can I use the pflag with the shortflag?
问题
我使用pflag
来解析命令行参数,但是如何使用短标志(shortflag)并设置必需参数或非必需参数呢?
package main
import (
"fmt"
"github.com/spf13/pflag"
"os"
)
var (
prometheus *string = pflag.StringP("prometheus", "p", "", "prometheus url")
cluster *string = pflag.StringP("cluster", "c", "", "k8s cluster name")
namespace *string = pflag.StringP("namespace", "n", "", "namespace")
timeout *time.Duration = pflag.DurationP("timeout", "t", 30*time.Second, "prometheus SQL timeout Duration")
)
func main() {
pflag.Parse()
if *cluster == "" {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
pflag.PrintDefaults()
os.Exit(1)
}
}
你可以使用pflag.StringP()
函数来设置短标志和必需参数或非必需参数。在这个例子中,-p
是prometheus
参数的短标志,-c
是cluster
参数的短标志,-n
是namespace
参数的短标志,-t
是timeout
参数的短标志。
英文:
I'm used pflag
to parsing command line argument, but how can I use the shortflag and set the required argument or non-required argument?
package main
import (
"fmt"
"github.com/spf13/pflag"
"os"
)
var (
prometheus *string = pflag.String("p", "", "prometheus url")
cluster *string = pflag.String("c", "", "k8s cluster name")
namespace *string = pflag.String("n", "", "namespace")
timeout *time.Duration = pflag.Duration("t", 30 * time.Second, "prometheus SQL timeout Duration")
)
func main(){
pflag.Parse()
if *cluster == "" {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
pflag.PrintDefaults()
os.Exit(1)
}
}
答案1
得分: 2
要同时使用简写和长格式参数,请使用 P 变体:
pflag.StringP("longName", "l", "", "desc")
据我所知,pflag 没有“必需”参数的概念。您可以获取标志并测试是否已设置:
pflag.Lookup("name").Changed
英文:
To use both shorthand and long-form args, use the P variant:
pflag.StringP("longName","l","","desc")
As far as I know, pflag does not have the concept of a "required" arg. You can get the flag and test if it was set:
pflag.Lookup("name").Changed
答案2
得分: 0
我修改后的代码如下所示:
package main
import (
"fmt"
"github.com/spf13/pflag"
"os"
"time"
)
var (
prometheus *string = pflag.StringP("prometheus","p", "", "prometheus url")
cluster *string = pflag.StringP("cluster","c", "", "k8s cluster name")
namespace *string = pflag.StringP("namespace","n", "", "namespace")
timeout *time.Duration = pflag.DurationP("timeout","t", 30*time.Second, "prometheus SQL timeout Duration")
)
func main() {
pflag.Parse()
if ! pflag.Lookup("cluster").Changed {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
pflag.PrintDefaults()
os.Exit(1)
}
fmt.Println(*prometheus, *cluster, *namespace, *timeout)
}
希望对你有帮助!
英文:
The code I modified looks like this:
package main
import (
"fmt"
"github.com/spf13/pflag"
"os"
"time"
)
var (
prometheus *string = pflag.StringP("prometheus","p", "", "prometheus url")
cluster *string = pflag.StringP("cluster","c", "", "k8s cluster name")
namespace *string = pflag.StringP("namespace","n", "", "namespace")
timeout *time.Duration = pflag.DurationP("timeout","t", 30*time.Second, "prometheus SQL timeout Duration")
)
func main() {
pflag.Parse()
if ! pflag.Lookup("cluster").Changed {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
pflag.PrintDefaults()
os.Exit(1)
}
fmt.Println(*prometheus, *cluster, *namespace, *timeout)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论