Make a cobra Command flag required

前端 未结 3 1787
野性不改
野性不改 2021-02-19 19:03

I\'ve created a cobra command and added a flag:

cmd.Flags().StringVarP(&primaryIP, \"primary-ip\", \"p\", \"\", \"Help text\")

Is there a w

3条回答
  •  被撕碎了的回忆
    2021-02-19 19:49

    A few months ago this behavior was changed in cobra, although the documentation is not very clear on this. Now if you mark a flag as MarkFlagRequired("primary-ip") and you don't supply such a flag, running the command will print the help plus

    required flag(s) "primary-ip"
    exit status 1
    

    at the end where the missing required flags are stated.

提交回复
热议问题