简单介绍

gflags 是 google 开源的用于处理命令行参数的项目。

安装编译

项目主页:gflags

➜  ~  git clone https://github.com/gflags/gflags.git # 下载源码
➜  ~  cd gflags
➜  gflags git:(master) ✗ mkdir build && cd build # 建立文件夹
➜  build git:(master) ✗ cmake .. # 使用 cmake 编译生成 Makefile 文件
➜  build git:(master) ✗ make # make 编译
➜  build git:(master) ✗ sudo make install # 安装库

这时 gflags 库会默认安装在 /usr/local/lib/ 下,头文件放在 /usr/local/include/gflags/ 中。

基础使用

我们从一个简单的需求来看 gflags 的使用,只要一分钟。假如我们有个程序,需要知道服务器的 ip 和端口,我们在程序中有默认的指定参数,同时希望可以通过命令行来指定不同的值。

实现如下:

#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;
}

OK, 写完了让我们编译运行。

➜  test g++ gflags_test.cc -o gflags_test -lgflags -lpthread # -l 链接库进行编译➜  test ./gflags_test #不带任何参数
The server host is: 127.0.0.1, the server port is: 12306➜  test ./gflags_test -host 10.123.78.90 #只带 host 参数
The server host is: 10.123.78.90, the server port is: 12306➜  test ./gflags_test -port 8008 # 只带 port 参数
The server host is: 127.0.0.1, the server port is: 8008➜  test ./gflags_test -host 10.123.78.90 -port 8008 # host 和 port 参数
The server host is: 10.123.78.90, the server port is: 8008➜  test ./gflags_test --host 10.123.78.90 --port 8008 # 用 -- 指定
The server host is: 10.123.78.90, the server port is: 8008➜  test ./gflags_test --host=10.123.78.90 --port=8008 # 用 = 连接参数值
The server host is: 10.123.78.90, the server port is: 8008➜  test ./gflags_test --help # 用 help 查看可指定的参数及参数说明
gflags_test: Warning: SetUsageMessage() never calledFlags from /home/rookie/code/gflags/src/gflags.cc:.... # 略Flags from /home/rookie/code/gflags/src/gflags_reporting.cc:..... # 略Flags from gflags_test.cc: #这里是我们定义的参数说明和默认值-host (the server host) type: string default: "127.0.0.1"-port (the server port) type: int32 default: 12306

看,我们不仅快速完成了需求,而且似乎多了很多看起来不错的特性。在上面我们使用了两种类型的参数,string 和 int32,gflags 一共支持 5 种类型的命令行参数定义:

  • DEFINE_bool: 布尔类型
  • DEFINE_int32: 32 位整数
  • DEFINE_int64: 64 位整数
  • DEFINE_uint64: 无符号 64 位整数
  • DEFINE_double: 浮点类型 double
  • DEFINE_string: C++ string 类型

如果你希望支持更复杂的结构,比如 list,你需要通过自己做一定的定义和解析,比如字符串按某个分隔符分割得到一个列表。

每一种类型的定义和使用都跟上面我们的例子相似,有所不同的是 bool 参数,bool 参数在命令行可以不指定值也可以指定值,假如我们定义了一个 bool 参数 debug_switch,可以在命令行这样指定:

➜  test ./gflags_test -debug_switch  # 这样就是 true
➜  test ./gflags_test -debug_switch=true # 这样也是 true
➜  test ./gflags_test -debug_switch=1 # 这样也是 true
➜  test ./gflags_test -debug_switch=false # 0 也是 false

所有我们定义的 gflags 变量都可以通过 FLAGS_ 前缀加参数名访问,gflags 变量也可以被自由修改:

if (FLAGS_consider_made_up_languages)FLAGS_languages += ",klingon";
if (FLAGS_languages.find("finnish") != string::npos)HandleFinnish();

进阶?同样 Easy

定义规范

如果你想要访问在另一个文件定义的 gflags 变量呢?使用 DECLARE_,它的作用就相当于用 extern 声明变量。为了方便的管理变量,我们推荐在 .cc 或者 .cpp 文件中 DEFINE 变量,然后只在对应 .h 中或者单元测试中 DECLARE 变量。例如,在 foo.cc 定义了一个 gflags 变量 DEFINE_string(name, 'bob', ''),假如你需要在其他文件中使用该变量,那么在 foo.h 中声明 DECLARE_string(name),然后在使用该变量的文件中 include "foo.h" 就可以。当然,这只是为了更好地管理文件关联,如果你不想遵循也是可以的。

参数检查

如果你定义的 gflags 参数很重要,希望检查其值是否符合预期,那么可以定义并注册参数的值的检查函数。如果采用 static 全局变量来确保检查函数会在 main 开始时被注册,可以保证注册会在 ParseCommandLineFlags 函数之前。如果默认值检查失败,那么 ParseCommandLineFlags 将会使程序退出。如果之后使用 SetCommandLineOption() 来改变参数的值,那么检查函数也会被调用,但是如果验证失败,只会返回 false,然后参数保持原来的值,程序不会结束。看下面的程序示例:

#include <stdint.h>
#include <stdio.h>
#include <iostream>#include <gflags/gflags.h>// 定义对 FLAGS_port 的检查函数
static bool ValidatePort(const char* name, int32_t value) {if (value > 0 && value < 32768) {return true;}printf("Invalid value for --%s: %d\n", name, (int)value);return false;
}/***  设置命令行参数变量*  默认的主机地址为 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");// 使用全局 static 变量来注册函数,static 变量会在 main 函数开始时就调用
static const bool port_dummy = gflags::RegisterFlagValidator(&FLAGS_port, &ValidatePort);int main(int argc, char** argv) {// 解析命令行参数,一般都放在 main 函数中开始位置gflags::ParseCommandLineFlags(&argc, &argv, true);std::cout << "The server host is: " << FLAGS_host<< ", the server port is: " << FLAGS_port << std::endl;// 使用 SetCommandLineOption 函数对参数进行设置才会调用检查函数gflags::SetCommandLineOption("port", "-2");std::cout << "The server host is: " << FLAGS_host<< ", the server port is: " << FLAGS_port << std::endl;return 0;
}

让我们运行一下程序,看看怎么样:

#命令行指定非法值,程序解析参数时直接退出
➜  test ./gflags_test -port -2
Invalid value for --port: -2
ERROR: failed validation of new value '-2' for flag 'port'
# 这里参数默认值合法,但是 SetCommandLineOption 指定的值不合法,程序不退出,参数保持原来的值
➜  test ./gflags_test
The server host is: 127.0.0.1, the server port is: 12306
Invalid value for --port: -2
The server host is: 127.0.0.1, the server port is: 12306

使用 flagfile

如果我们定义了很多参数,那么每次启动时都在命令行指定对应的参数显然是不合理的。gflags 库已经很好的解决了这个问题。你可以把 flag 参数和对应的值写在文件中,然后运行时使用 -flagfile 来指定对应的 flag 文件就好。文件中的参数定义格式与通过命令行指定是一样的。

例如,我们可以定义这样一个文件,文件后缀名没有关系,为了方便管理可以使用 .flags:

--host=10.123.14.11
--port=23333

然后命令行指定:

➜  test ./gflags_test --flagfile server.flags
The server host is: 10.123.14.11, the server port is: 23333

google 工具 gflags相关推荐

  1. 谷歌命令行解析工具gflags的使用

    谷歌命令行解析工具gflags的使用 Gflags简介 GFlags是Google开源的一套命令行参数处理的开源库. 在一个源码文件定义的flag,链接了该文件的应用就可以使用该flag,可以方便的复 ...

  2. GOOGLE工具大全+搜索引擎免费登陆入口

    GOOGLE工具大全+搜索引擎免费登陆入口 42个著名搜索引擎免费登陆入口经过作者测试,大部分可以登陆,想提高访问量的朋友可以参考一二: 中国论坛之家登陆入http://www.vt123.com/l ...

  3. [转]google工具大全

    Technorati 标签: google 原文地址:http://sifxx.blogbus.com/logs/56293105.html google工具 谷歌的产品很多,有的真的没有听说过,你有 ...

  4. 57个你不知道的google工具

    57个你不知道的google工具谷歌的产品很多,有的真的没有听说过,你有多少个没有听说过的谷歌产品呢? 必备 01.谷歌阅读器(Google Reader):网页版RSS阅读器,方便订阅,组织和分享新 ...

  5. 试用Google工具条的网页翻译!

    安装Google后一直没怎么注意,今天发现在网页上点右键,选择"网页资料"-〉"将网页翻译成简体中文",也可以直接点工具条上的翻译里的子菜单"将网页翻 ...

  6. Python 编程中常用的12种基础知识总结

    Python 编程中常用的12 种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序.去重,字典排序,字典.列表.字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出 ...

  7. 《GOOGLE HACKS巧妙使用网络搜索的技巧和工具(第二版)》

    全书分为网页.高级网页.图像.新闻组和论坛.附加功能.Gmail.广告.Web管理和Google编程9章,不仅阐述了用Google搜索的方方面面,而且能帮你最大程度地挖掘Google的潜能,即挖掘出每 ...

  8. GOOGLE HACKS巧妙使用网络搜索的技巧和工具(第二版)已经出版

    全书分为网页.高级网页.图像.新闻组和论坛.附加功能.Gmail.广告.Web管理和Google编程9章,不仅阐述了用Google搜索的方方面面,而且能帮你最大程度地挖掘Google的潜能,即挖掘出每 ...

  9. 【翻译】Google在构建静态代码分析工具方面的经验教训

    软件bug耗费开发者和软件公司大量的时间和金钱. 以2014年为例,被广泛使用的SSL协议实现中的一个("goto fail")bug导致可接受无效的SSL证书,另外一个与日期格式 ...

最新文章

  1. 一个 Mybatis 开发神器:Fast MyBatis 超好用
  2. js中常用的算法排序
  3. 用80386汇编来编写asp.net页面。
  4. 迁移学习之域自适应理论简介(Domain Adaptation Theory)
  5. X265整体流程-Create
  6. django-restframework使用
  7. 移动开发(C#、VB.NET)Smobiler开发平台——GifView控件的使用方式
  8. 测试-LoadRunner
  9. 浏览器兼容的JS写法总结
  10. css基础 -文本溢出 text-overflow:ellipsis;
  11. C++小白课本练习3
  12. 【渝粤题库】陕西师范大学200071 古代汉语 作业(高起本、高起专)
  13. 服务器自带raid功能吗,服务器的 RAID 功能介绍
  14. linux下删除编译安装的软件,Linux 中卸载编译安装的软件
  15. Array Implementation of min-Heaps 最小堆数组实现
  16. 面试系列-2 我终于弄清楚了redis数据结构之string应用场景
  17. 古月居 ROS 入门21讲--PA17 ROS中坐标系管理系统笔记
  18. apktool下载及“安装”(windows系统)
  19. Java 流的使用总结
  20. 一文读懂什么是CTO、技术VP、技术总监、首席架构师

热门文章

  1. html多图自动展示,基于echarts+html+css+jq的数据可视化大屏展示炫酷看板
  2. 浙江经信公布人工智能5大榜单 网易易盾内容安全解决方案上榜
  3. android 经纬度 百度地图,05-04【咨询】安卓开发百度地图输入地址,将得到的经纬度存储...
  4. jquery 插件 rater 星星评论
  5. 【Matlab】M文件编写PID调节传递函数
  6. 第一套微信小程序教程目录(转载侵删)
  7. 实训PHP的目的,实习的目的和意义
  8. MySql的相关资操作
  9. win10linux远程命令,IT之家学院:在Win10下管理远程命令行
  10. D17-读论文D17算法D17-复习