Go中的命令行标记可以设置为强制性吗? [英] Can command line flags in Go be set to mandatory?

查看:76
本文介绍了Go中的命令行标记可以设置为强制性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法设置某些标志是强制性的,还是我必须自己检查它们的存在?

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

推荐答案

flag 包没有支持强制性或必需标志(意味着必须明确指定标志)。

The flag package does not support mandatory or required flags (meaning the flag must be specified explicitly).

您可以做的是对所有标志使用合理的默认值。如果标志没有合理的默认值,请在应用程序启动时检查该值,并显示一条错误消息以使其停止。无论如何,您都应该进行标志值验证(不仅仅针对必需的标志),因此这并不意味着任何(大)开销,这通常是个好习惯。

What you can do is use sensible default values for (all) flags. And if a flag is something like there is no sensible default, check the value at the start of your application and halt with an error message. You should do flag value validation anyway (not just for required flags), so this shouldn't mean any (big) overhead, and this is a good practice in general.

这篇关于Go中的命令行标记可以设置为强制性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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