google gflags 库的下载和使用

一、作用

  • gflags 是用来处理命令行参数的库
  • 假如我们有个程序,需要知道服务器的 ip 和端口,我们在程序中有默认的指定参数,同时希望可以通过命令行来指定不同的值

二、安装

wget https://github.com/gflags/gflags/archive/v2.1.1.tar.gz   //下载源码
cd gflags
mkdir build && cd build  // 建立编译文件夹,用于存放临时文件
cmake .. -DCMAKE_INSTALL_PREFIX=/usr  // 使用 cmake 进行动态编译生成 Makefile 文件,安装路径为/usr
make # make 编译
sudo make install # 安装库

三、使用介绍

3.1 代码和效果

  • 代码

    #include <iostream>
    #include <gflags/gflags.h>
    /**
     *  *  定义命令行参数变量
     *   *  默认的主机地址为 127.0.0.1,变量解释为 'the server host'
     *    *  默认的端口为 12306,变量解释为 'the server port'
     *     */
    
    
    DEFINE_string(host, "127.0.0.1", "the server host");
    DEFINE_int32(port, 12306, "the server port");
    
    int main(int argc, char** argv) {
    				    // 解析命令行参数,一般都放在 main 函数中开始位置
    								gflags::ParseCommandLineFlags(&argc, &argv, true);
    						// 访问参数变量,加上 FLAGS_
    						std::cout << "The server host is: " << FLAGS_host
    						<< ", the server port is: " << FLAGS_port << std::endl;
    						return 0;
    }
    
    
    
    
    ------------makefile----------
    
    now:now.cpp
    				g++ -o $@ $^  -lgflags # 编译的时候需要-l链接gflags库
    				
    .PHONY:clean
    clean:
    				rm -rf now
    
  • 效果

    ➜  test2 ./now  
    The server host is: 127.0.0.1, the server port is: 12306
    

3.2参数的定义

  • 使用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,该宏的三个参数分别代表命令行参数名、参数默认值、参数的帮助信息(注释)
    • 使用方式为FGLAGS_name
    • 注意一点,通过DEFINE定义的参数,要保证访问变量的文件和定义参数的文件是同一个文件或者是头文件,包含关系,否则将会访问不到定义的参数。在其它文件使用定义的参数通过DECLARE实现
  • 初始化参数

    // 解析命令行参数,一般都放在 main 函数中开始位置
    gflags::ParseCommandLineFlags(&argc, &argv, true);
    
    • 如果最后一个参数为true,gflags会移除初始化过的参数,如果为false则会进行保留
  • 在命令行中指定参数

    • 可以通过: --参数名=参数值来指定
      在这里插入图片描述
  • 跨文件访问

    • 如果你想要访问在另一个文件定义的 gflags 变量呢?使用 DECLARE_,它的作用就相当于用 extern 声明变量
    • 比如在头文件中声明DECLARE_string(name),其它包含该头文件的文件,也可以对name变量进行访问了
  • 参数检查

    • 参数定义之后,可以为参数注册一个检查函数,当从命令行指定参数或者通过SetCommandLineOption指定参数时,检查函数就会被调用,两个参数分别为命令行参数名,以及设置的参数值

      #include <iostream>
      using namespace std;
      #include <gflags/gflags.h>
      using namespace gflags;
      #include <stdio.h>
      
      DEFINE_int32(port,111,"Host name");
      
      
      //检查函数,检查函数是否符合要求
      static bool ValidatePort(const char*name,int32_t val)
      {
      				if(val>0 && val < 2000)
      								return true;
      
      				printf("Invalid value for --%s: %d\n", name, (int)val);
      				return false;
      }
      
      
      // 使用全局 static 变量来注册函数,static 变量会在 main 函数开始时就调用
      //必须在main开始时进行注册,否则无法监测命令行参数
      static const bool port_dummy = gflags::RegisterFlagValidator(&FLAGS_port, &ValidatePort);
      
      
      int main(int argc,char **argv)
      {
      				   //初始化参数
      				     ParseCommandLineFlags(&argc,&argv,true);
      
      			   	 // 使用 SetCommandLineOption 函数对参数进行设置才会调用检查函数
      				   //调用函数更改参数,并且将结果打印出来
      
      						 gflags::SetCommandLineOption("port", "888");
      				     cout<< ", the server port is: " << FLAGS_port << std::endl;
      
      						 gflags::SetCommandLineOption("port", "-3");
      				     cout<< ", the server port is: " << FLAGS_port << std::endl;
      
      				return 0;
      }
      

在这里插入图片描述

3.3一些特殊参数

  • –help:打印定义过的所有参数的帮助信息

    ➜  test2 ./now --help
    now: Warning: SetUsageMessage() never called
    		…………
        -helpshort (show help on only the main module for this program) type: bool
          default: false
        -helpxml (produce an xml version of help) type: bool default: false
        -version (show version and build info and exit) type: bool default: false
    		//中间是一些信息
        …………
                   
     //这里是定义参数的说明和默认值
      Flags from now.cpp:
        -host (the server host) type: string default: "127.0.0.1"
        -port (the server port) type: int32 default: 12306
        -test (this is test) type: double default: 21.3232
    
  • –version:打印版本信息

  • –nodefok:当命令行中没有参数时,并不退出

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

    数值与在命令行方式类似,另外在flagfile里可进一步通过–flagfile来包含其他的文件。
    在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值