Can command line flags in Go be set to mandatory?

前端 未结 7 1452
-上瘾入骨i
-上瘾入骨i 2021-02-05 00:04

Is there a way how to set that certain flags are mandatory, or do I have to check for their presence on my own?

7条回答
  •  执笔经年
    2021-02-05 00:37

    go-flags lets you declare both required flags and required positional arguments:

    var opts struct {
        Flag string `short:"f" required:"true" name:"a flag"`
        Args struct {
            First   string `positional-arg-name:"first arg"`
            Sencond string `positional-arg-name:"second arg"`
        } `positional-args:"true" required:"2"`
    }
    args, err := flags.Parse(&opts)
    

提交回复
热议问题