首页 > google gflags的参数解析,便捷实用

google gflags的参数解析,便捷实用

命令行参数解析,一直是我们后段开发人员需要经常使用的一个功能,用来从终端解析接口的输入 ,并做出对应的处理。这里为使用C++/python的开发人员推荐一个便捷的命令行解析接口集 gflags。

我们之前使用C的getopt/getopt_long 函数需要自己使用其接口并编写大量的周边代码达到解析参数的目的,如果转到C++还使用上面的函数,代码会过于冗余。那么gflags就很好的解决了这个问题,我们不需要再数着冒号 "a🅱️cd:"添加参数了,不需要为每个传入的参数做类型转化。

接下来的gflags 测试代码可以提前向下看一看,耳目一新,清晰明了。

支持的数据类型

  • DEFINE_bool
  • DEFINE_int32 : 32位的整型
  • DEFINE_uint32:无符号32位整型
  • DEFINE_int64:64位整型
  • DEFINE_uint64:无符号64位整型
  • DEFINE_double:浮点类型
  • DEFINE_string: C++ 的string类型

安装

详情可以参考:

install gflags

  • mac : brew install gflags
  • 其他linux系统:
    $ tar xzf gflags-$version-source.tar.gz
    $ cd gflags-$version
    $ mkdir build && cd build
    $ ccmake ..- Press 'c' to configure the build system and 'e' to ignore warnings.- Set CMAKE_INSTALL_PREFIX and other CMake variables and options.- Continue pressing 'c' until the option 'g' is available.- Then press 'g' to generate the configuration files for GNU Make.$ make
    $ make test    (optional)
    $ make install (optional)
    

以上如果不进行对应的编译参数设置,默认头文件和动态库是安装在/usr/local/include/gflags 以及 /usr/local/lib之中

使用

主要使用如下几个接口进行参数相关的操作。

  • DEFINE_uint32 这样的接口定义我们的参数类型,参数名称,参数默认值
  • SetVersionString(const std::string& version) 自己设置程序版本,主要是在用gflags编译的文件的命令行输入–version 输出版本信息
  • void SetUsageMessage(const std::string& usage) 自己设置运行 --help时的帮助信息,默认会打印gflags相关的帮助信息
  • uint32 ParseCommandLineFlags(int *argc, char*** argv, bool remove_flags)解析传入的参数

    -void ShutDownCommandLineFlags()释放解析参数过程中分配的空间

编写如下测试代码:

#include 
#include 
#include using namespace std;
using namespace google;// default argvs from command line
DEFINE_int64(count,100,"count of nums");
DEFINE_string(entry,"str_string","the first of string");
DEFINE_bool(judge, false, "judge something");string g_version;
string g_help;string get_version() { g_version = "1.3";return g_version;
}string get_help_info() { g_help = "help info messages";return g_help;
}int main(int argc, char *argv[]) { // Sets the version stringSetVersionString(get_version());SetUsageMessage(get_help_info());// Looks for flags in argv and parses them.ParseCommandLineFlags(&argc,&argv, true);cout << "count = " << FLAGS_count << endl;cout << "entry = " << FLAGS_entry << endl;if (FLAGS_judge) { cout << "judge is true !" << endl;} else { cout << "judge is false !" << endl;}// Clean up memory allocated by flags.ShutDownCommandLineFlags();return 0;
}

编译:

g++ -std=c++11 gflags_test.cc -o gflags_test -lgflags

这个编译适用于gflags安装在默认路径下,如果gflags安装在自己指定的目录,则建议编写Makefile更方便一点:

GFLAGS_DIR = /usr/local/include/gflags
LIB_DIR = /usr/local/lib/
gflags_test: gflags_test.ccg++ -I${GFLAGS_DIR} -L${LIB_DIR} gflags_test.cc -o gflags_test -lgflags
clean:rm -f gflags_test

运行:

以下为我的测试过程

cpp_practice % ./gflags_test #默认参数
count = 100
entry = str_string
judge is false !% ./gflags_test --count 20 --entry test_argv --judge true #指定参数
count = 20
entry = test_argv
judge is true !cpp_practice % ./gflags_test --version #查看版本信息
gflags_test version 1.3cpp_practice % ./gflags_test --help #查看帮助信息
gflags_test: help info messages

以上同样的解析过程,如果是getopt_long,则需要至少3倍的代码量才能够实现相同的功能。

不过这个只能用在C++或者python中,就有点尴尬了

更多相关:

  • importjava.security.SecureRandom;importjavax.crypto.Cipher;importjavax.crypto.SecretKey;importjavax.crypto.SecretKeyFactory;importjavax.crypto.spec.DESKeySpec;//结果与DES算...

  • 题目:替换空格 请实现一个函数,把字符串 s 中的每个空格替换成"%20"。 输入:s = "We are happy." 输出:"We%20are%20happy." 限制: 0 <= s 的长度 <= 10000 解题: 时间复杂度:O(n) 空间复杂度:O(n) class Solution { public:s...

  • 在C++11标准库中,string.h已经添加了to_string方法,方便从其他类型(如整形)快速转换成字面值。 例如: for (size_t i = 0; i < texArrSize; i++)RTX_Shader.SetInt(string("TexArr[") + to_string(i) + "]", 7 + i);...

  • Ubuntu 14.04安装并升级之后,变成楷体字体非常难看,我昨天搞了一晚上,终于理了个头绪,这里整理一下。 经过网上调研,大家的一致看法是,使用开源字体库文泉驿的微黑字体效果比较理想,甚至效果不输windows平台的雅黑字体。下面我打算微黑来美化Ubuntu 14.04. 1.安装文泉驿微黑字体库 sudo aptitude...

  • 使用string时发现了一些坑。 我们知道stl 容器并不是线程安全的,所以在使用它们的过程中往往需要一些同步机制来保证并发场景下的同步更新。 应该踩的坑还是一个不拉的踩了进去,所以还是记录一下吧。 string作为一个容器,随着我们的append 或者 针对string的+ 操作都会让string内部的数据域动态增加,而动态增加的...

  • 打开 build文件夹下面的webpack.base.conf.js; 找到下面这段代码,并将它注释掉: const createLintingRule = () => ({// test: /.(js|vue)$/,// loader: 'eslint-loader',// enforce: 'pre',// includ...

  • 写一个.cc文件,其中抱哈std::lock_guard以及std::thread等c++11特性,开始使用gcc编译,过程中出现如下问题 gcc test_lock.cc -o test_lock This file requires compiler and library support for the ISO C++ 201...

  • 在阅读ceph源码过程中发现部分C++语法还是不够熟悉,特此做一下笔记。 关于STL中的reserve函数的使用 reserve()是为容器预留空间,即为当前容器设定一个空间分配的阈值,但是并不会为容器直接allocate具体的空间,具体空间的分配是在创建对象时候进行分配得 以vector的reserve函数过程为例,直接看如下代码...

  • 第一种写法: 第二种写法:   转载于:https://www.cnblogs.com/w...

  • Rank() over()的用法 创建一个test表,并插入6条数据。 CREATE TABLE test (a INT,b INT,c CHAR ) INSERT INTO test VALUES(1,3,'E') INSERT INTO test VALUES(2,4,'A') INSERT INTO test VAL...