使眼镜蛇命令标记为必需 [英] Make a cobra Command flag required

查看:40
本文介绍了使眼镜蛇命令标记为必需的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个眼镜蛇命令并添加了一个标志:

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

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

除了自己检查值并返回错误之外,是否有办法使其成为必需项?

Is there a way to make it required other than checking the value myself and returning an error?

推荐答案

几个月前,这种行为在 cobra 中已更改,尽管文档对此并不十分清楚.现在,如果您将标志标记为 MarkFlagRequired("primary-ip"),而您不提供此类标志,则运行命令将显示帮助加

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.

这篇关于使眼镜蛇命令标记为必需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆