gflags(google开源的一套命令行参数解析工具)

来源:互联网 发布:阿里云 sap hana 编辑:程序博客网 时间:2024/06/05 11:26

gflagsgoogle开源的一套命令行参数解析工具,比getopt()函数功能要强大,使用起来更加方便,gflags还支持从环境变量和配置文件中读取参数。目前有C++Python版本。本文就来详细介绍C++版本gflags的使用,主要分如下两个部分

 

 

Contents

 

    1. gflags的安装

    2. gflags的使用介绍


Github上code:

C++:https://github.com/gflags/gflags

python:https://github.com/gflags/python-gflags

 

1. gflags的安装

 

   上面我们已经对gflags有个大概的认识,接下来开始在Linux环境中安装gflags。首先下载如下安装包

 

   链接:https://github.com/schuhschuh/gflags/releases

 

   在线帮助文档:http://gflags.googlecode.com/svn/trunk/doc/gflags.html

 

   使用的版本是gflags-1.0rc2.tar.gz,然后进行解压,执行如下3

 

   (1)./configure

   (2)make

   (3)make install

 

 

   介绍两个比较重要的文件

 

   doc :         gflags的帮助文件,里面介绍了gflags的一些使用方法。

   src :         gflags的代码源文件。

 

  

   为了程序在运行时能加载libgflags.so文件,还需要在/usr/lib目录下建立软链接,如下

 

      

 

 

   现在就彻底安装好gflags了,可以安心使用。

 

 

2. gflags的使用介绍

  

  (1)定义参数

 

      使用gflags需要包含头文件#include <gflags/gflags.h>。而gflags支持的参数类型有


       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

 

      定义方式如下

 

      

 

      即DEFINE_type就可以了,该宏的三个参数分别代表命令行参数名,参数默认值,参数的帮助信息。

 

      gflags不支持列表,但是可以借助string类型实现,比如

 

      

 

  

  (2)参数访问

 

      参数被定义后,可以通过FLAGS_name来访问,比如

 

      

 

      

 

      注意一点,通过DEFINE定义的参数,要保证访问变量的文件和定义参数的文件是同一个文件或者是头文件

      包含关系,否则将会访问不到定义的参数。在其它文件使用定义的参数通过DECLARE实现。

  

 

  (3)参数检查

 

      参数定义后,可以为参数注册一个检查函数,当从命令行指定参数或通过SetCommandLineOption()指定

      参数时,检查函数就会被调用,两个参数分别为命令行参数名,以及设置的参数值。

 

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <string>  
  4. #include <stdio.h>  
  5.   
  6. #include <gflags/gflags.h>  
  7.   
  8. using namespace std;  
  9. using namespace google;  
  10.   
  11. static bool ValidatePort(const char *flagname, int32_t val)  
  12. {  
  13.     if(val > 0 && val < 32768)  
  14.         return true;  
  15.     return false;  
  16. }  
  17. DEFINE_int32(port, 6379, "what is the port ?");  
  18.   
  19. int main(int argc, char **argv)  
  20. {  
  21.     ParseCommandLineFlags(&argc, &argv, true);  
  22.     bool isPort = RegisterFlagValidator(&FLAGS_port, &ValidatePort);    
  23.     return 0;  
  24. }  

 

 

      建议在定义参数后,立即注册检查函数。RegisterFlagValidator()在检查函数注册成功时返回true;

      如果参数已经注册了检查函数,或者检查函数类型不匹配,返回false。

 

 

  (4)初始化参数

 

      在引用程序的main()里通过调用ParseCommandLineFlags(&argc, &argv, true)实现,即完成对gflags参数的初始化,最后一个参数如果为true,gflags会移除Parse过的参数,如果为false则会保留。可能会对参数顺序进行调整。 比如 "/bin/foo" "arg1" "-q" "arg2"  会被调整为 "/bin/foo", "-q", "arg1", "arg2",这样更好理解。

 

  (5)在命令行指定参数

 

    可以通过:--参数名=参数值 来指定。

  • 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"

对于bool类型,则可通过如下几种方式指定参数

  • app_containing_foo --big_menu
  • app_containing_foo --nobig_menu
  • app_containing_foo --big_menu=true
  • app_containing_foo --big_menu=false

  例如下面代码

 

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <string>  
  4. #include <stdio.h>  
  5.   
  6. #include <gflags/gflags.h>  
  7.   
  8. using namespace std;  
  9. using namespace google;  
  10.   
  11. static bool ValidatePort(const char *flagname, int32_t val)  
  12. {  
  13.     if(val > 0 && val < 32768)  
  14.         return true;  
  15.     return false;  
  16. }  
  17. DEFINE_int32(port, 6379, "what is the port ?");  
  18. DEFINE_string(ip, "127.0.0.1""My ip ?");  
  19.   
  20. int main(int argc, char **argv)  
  21. {  
  22.     ParseCommandLineFlags(&argc, &argv, true);  
  23.     bool isPort = RegisterFlagValidator(&FLAGS_port, &ValidatePort);    
  24.     int32_t Port = FLAGS_port;  
  25.     string Ip = FLAGS_ip;  
  26.     cout << Port << endl;  
  27.     cout << Ip << endl;  
  28.     return 0;  
  29. }  


 

         参数可以通过下面方式指定,参数之间可以没有顺序。

 

      

 

 

  (5)一些特殊的参数认识

 

      --help

 

      打印定义过的所有参数的帮助信息


      --version

 

      打印版本信息 通过google::SetVersionString()指定


      --nodefok 

 

      但命令行中出现没有定义的参数时,并不退出(error-exit)


      --fromenv

 

      从环境变量读取参数值 --fromenv=foo,bar表明要从环境变量读取foo,bar两个参数的值。通过

      export FLAGS_foo=xxx; export FLAGS_bar=yyy 程序就可读到foo,bar的值分别为xxx,yyy。


      --tryfromenv

 

      与--fromenv类似,当参数的没有在环境变量定义时,不退出(fatal-exit)


      --flagfile

 

      从文件读取参数值,--flagfile=my.conf表明要从my.conf文件读取参数的值。在配置文件中指定参

      数值与在命令行方式类似,另外在flagfile里可进一步通过--flagfile来包含其他的文件。


原创粉丝点击