Gflags 简明使用

简介

Google 的 gflags 是一套命令行参数处理的开源库。比 getopt 更方便,更功能强大,从 C++的库更好的支持 C++(如 C++的 string 类型)。

example 源代码先看 example 源代码,然后逐步介绍。

example.cc
1
2
3
4
5
6 7 8 9 10 11 12 13 14 15 16 17 18 
#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");  int main(int argc, char **argv) {  google::ParseCommandLineFlags(&argc, &argv, true);   cout << "argc=" << argc << endl;  if (FLAGS_big_menu) {  cout << "big menu is ture" << endl;  } else {  cout << "big menu is flase" << endl;  }   cout << "languages=" << FLAGS_languages << endl;  return 0; } 

运行程序

  • 直接运行
run
1
2
3
4
  ➜  bin  ./sample
  argc=1
 big menu is ture  languages=english,french,german 
  • help 命令
run
1
2
3
4
5
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 
  ➜  bin  ./sample --help
  sample: Warning: SetUsageMessage() never called

 Flags from /home/shougang/workspace/drive/Google/cmake_cpp_gflags/src/sample.cc:  -big_menu (Include 'advanced' options in the menu listing) type: bool  default: true  -languages (comma-separated list of languages to offer in the 'lang' menu)  type: string default: "english,french,german"     Flags from src/gflags.cc:  -flagfile (load flags from file) type: string default: ""  .........   ➜ bin ./sample --helpshort  sample: Warning: SetUsageMessage() never called   Flags from /home/shougang/workspace/drive/Google/cmake_cpp_gflags/src/sample.cc:  -big_menu (Include 'advanced' options in the menu listing) type: bool  default: true  -languages (comma-separated list of languages to offer in the 'lang' menu)  type: string default: "english,french,german" 

在程序里定义参数

### 包含头文件

header_file
1
 #include <gflags/gflags.h> 

利用 gflag 提供的宏定义参数该宏的 3 个参数分别为命令行参数名,参数默认值,参数的帮助信息。

define_flags
1
2
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"); 

gflags 暂时支持如下参数的类型:

supported_types
1
2
3
4
5
6 
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_name 像正常变量一样访问标志参数。在这个程序中,通过 FLAGS_big_menuFLAGS_languages访问它们。

不同文件访问参数

如果想再另外一个不是定义这个参数的文件访问这个参数的话,以参数 FLAGS_big_menu为例,用宏DECLARE_bool(big_menu)来声明引入这个参数。这个宏相当于做了extern FLAGS_big_menu.

整合一起,初始化所有参数

定义号参数后,最后要告诉执行程序去处理命令行传入的参数,使得 FLAGS_*参数们得到正确赋值。

通常就是再main()函数中调用;

set_up_flag
1
google::ParseCommandLineFlags(&argc, &argv, true); 

argcargv就是 main 的入口参数,因为这个函数会改变他们的值,所以都是以指针传入。

第三个参数被称为remove_flags。如果它是true,ParseCommandLineFlags会从argv中移除标识和它们的参数,相应减少argc的值。然后 argv 只保留命令行参数。

相反,remove_flagsfalse,ParseCommandLineFlags会保留argc不变,但将会重新调整它们的顺序,使得标识再前面。

Note: ./sample --big_menu=false arg1中再big_menu是标识,false是它的参数,arg1是命令行参数。

命令行设置参数

gflags 提供多种命令行设置参数。

stringint之类,可以用如下方式:

set_languages
1
2
3
4
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" 

对于boolean的标识来说,用如下方式:

set_boolean
1
2
3
4
app_containing_foo --big_menu
app_containing_foo --nobig_menu
app_containing_foo --big_menu=true app_containing_foo --big_menu=false 

getopt()一样,--将会终止标识的处理。所以在foo -f1 1 -- -f2 2中, f1被认为是一个标识,但f2不会。

特殊标识

special_flags
1
2
3
4
5
6 7 
--help  显示文件中所有标识的完整帮助信息
--helpfull  和-help 一样,
--helpshort  只显示当前执行文件里的标志
--helpxml  以 xml 凡是打印,方便处理
--version 打印版本信息,由 google::SetVersionString()设定 --flagfile -flagfile=f 从文件 f 中读取命令行参数 ... 

具体见:http://gflags.googlecode.com/svn/trunk/doc/gflags.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值