Gflags使用

google用于命令行参数解析的一个库,其主要致力于解决麻烦的参数解析问题。

声明参数

参数的声明很简单,形如

#include <gflags/gflags.h>

DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing");
DEFINE_string(languages, "english,french,german",
             "comma-separated list of languages to offer in the 'lang' menu");

其中,gflag/gflags.h是必要的头文件。在早期的版本里面在google/gflags.h中定义。
通过DEFINE_bool定义一个bool类型的参数。其参数名字为big_menu,默认值true,参数解释为”Include ‘advanced’ options in the menu listing”。
当然,参数也可以设定为其他类型,主要可以使用的参数类型包括:

  • DEFINE_bool: boolean
  • DEFINE_int32: 32-bit integer
  • DEFINE_int64: 64-bit integer
  • DEFINE_uint64: unsigned 64-bit integer
  • DEFINE_double: double
  • DEFINE_string: C++ string

使用参数

在参数定义之后,就可以像其他普通变量一样使用它。
例如:
我们有 FLAGS_big_menu (a bool), and FLAGS_languages (a C++ string).这两个变量,使用时可以如下:

   if (FLAGS_consider_made_up_languages)
     FLAGS_languages += ",klingon";   // implied by --consider_made_up_languages
   if (FLAGS_languages.find("finnish") != string::npos)
     HandleFinnish();

定义参数

在工程任意的文件中声明参数都是有效的,并且参数只能够被定义一次。如果想要在不同文文件中使用参数,需要并只能在在一个文件中定义,在其他文件中声明即可使用。如果未定义即使用,会出现’unknown variable’错误。
定义变量可以用如下形式:

   DECLARE_bool(big_menu);

检查参数

在定义了一个Flag之后,可以随同该flag注册一个 validator function 。如果这样的话,在程序解析了输入参数之后,不管它的值在什么时候通过setCommandLineOption()改变,validator function都会以新的参数值作为输入进行一次调用。这个有些类似QT中的参量绑定机制。validator function会在成功修改变量时返回true。如果其返回为false,说明变量更新失败,程序将维持原有变量的值。
样例如下:

static bool ValidatePort(const char* flagname, int32 value) {
   if (value > 0 && value < 32768)   // value is ok
     return true;
   printf("Invalid value for --%s: %d\n", flagname, (int)value);
   return false;
}
DEFINE_int32(port, 0, "What port to listen on");
static const bool port_dummy = RegisterFlagValidator(&FLAGS_port, &ValidatePort);

解析参数

通过调用

 gflags::ParseCommandLineFlags(&argc, &argv, true);

让系统进行参数的解析并赋值。

这个函数有最后的参数叫做”remove_flags”,如果其为

  • true 函数会将argc中的命令行参数全部抹去,只保留命令本身,并修改argv到对应数目。
  • false argc不会改变,但是会重新排布argv,让所有的flag全部排在前面,比如输入 “/bin/foo” “arg1” “-q” “arg2” ,输出将为”/bin/foo”, “-q”, “arg1”, “arg2”.

在命令行中设置flag

比如,在:

app_containing_foo --nobig_menu -languages="chinese,japanese,korean"

将会设置FLAGS_big_menu = false, FLAGS_languages = “chinese,japanese,korean”。对于bool型的设置来说,只需要在参量前面加no就可以。其他类型可以直接设置,下列几种设置的格式都是允许的:

app_containing_foo --languages="chinese,japanese,korean"
app_containing_foo -languages="chinese,japanese,korean"
app_containing_foo --languages "chinese,japanese,korean"
app_containing_foo -languages "chinese,japanese,korean"

改变参量的默认值

有的参量是在lib中定义的,然而你却想更改它的默认值,只需要如下:

   DECLARE_bool(lib_verbose);   // mylib has a lib_verbose flag, default is false
   int main(int argc, char** argv) {
     FLAGS_lib_verbose = true;  // in my app, I want a verbose lib by default
     ParseCommandLineFlags(...);
   }

特殊的flag

–help shows all flags from all files, sorted by file and then by name; shows the flagname, its default value, and its help string
–helpfull same as -help, but unambiguously asks for all flags (in case -help changes in the future)
–helpshort shows only flags for the file with the same name as the executable (usually the one containing main())
–helpxml like –help, but output is in xml for easier parsing
–helpon=FILE shows only flags defined in FILE.*
–helpmatch=S shows only flags defined in S.*
–helppackage shows flags defined in files in same directory as main()
–version prints version info for the executable
另详见 https://gflags.github.io/gflags/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值