How can I use the pflag with the shortflag?

huangapple go评论71阅读模式
英文:

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()函数来设置短标志和必需参数或非必需参数。在这个例子中,-pprometheus参数的短标志,-ccluster参数的短标志,-nnamespace参数的短标志,-ttimeout参数的短标志。

英文:

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)
}

huangapple
  • 本文由 发表于 2021年6月29日 11:35:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/68172307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定