简介
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_menu和FLAGS_languages访问它们。
不同文件访问参数
如果想再另外一个不是定义这个参数的文件访问这个参数的话,以参数 FLAGS_big_menu为例,用宏DECLARE_bool(big_menu)来声明引入这个参数。这个宏相当于做了extern FLAGS_big_menu.
整合一起,初始化所有参数
定义号参数后,最后要告诉执行程序去处理命令行传入的参数,使得 FLAGS_*参数们得到正确赋值。
通常就是再main()函数中调用;
set_up_flag
1
google::ParseCommandLineFlags(&argc, &argv, true); 
argc和argv就是 main 的入口参数,因为这个函数会改变他们的值,所以都是以指针传入。
第三个参数被称为remove_flags。如果它是true,ParseCommandLineFlags会从argv中移除标识和它们的参数,相应减少argc的值。然后 argv 只保留命令行参数。
相反,remove_flags是false,ParseCommandLineFlags会保留argc不变,但将会重新调整它们的顺序,使得标识再前面。
Note: ./sample --big_menu=false arg1中再big_menu是标识,false是它的参数,arg1是命令行参数。
命令行设置参数
gflags 提供多种命令行设置参数。
string和int之类,可以用如下方式:
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 中读取命令行参数 ... 
 
 
 
 
 