gflags提供了一个检查传入flag值是否有效的功能,只要定义检测函数,并且注册就可以了。
检测函数以及注册方式的例子:

static bool ValidatePort(const char* flagname, int32 value) {
if (value > 0 && value < 32768) // value is ok

 return true;

printf("Invalid value for --%s: %dn", flagname, (int)value);
return false;
}
DEFINE_int32(port, 0, "What port to listen on");
static const bool port_dummy = RegisterFlagValidator(&FLAGS_port, &ValidatePort);
如果注册成功,regist函数返回值为ture。否则返回false,注册失败一般是一下两种原因:

第一个参数不是flag
该flag已经注册过

本文链接地址:https://const.net.cn/561.html

标签: gflags

添加新评论