Can command line flags in Go be set to mandatory?

前端 未结 7 1454
-上瘾入骨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:29

    I like github.com/jessevdk/go-flags package to use in CLI. It provides a required attribute, to set a mandatory flag:

    var opts struct {
    ...
        // Example of a required flag
        Name string `short:"n" long:"name" description:"A name" required:"true"`
    ...
    }
    

提交回复
热议问题