gflags(google开源的一套命令行参数解析工具)
2017-11-08 14:44
423 查看
gflags是google开源的一套命令行参数解析工具,比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
#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <gflags/gflags.h>
using namespace std;
using namespace google;
static bool ValidatePort(const char *flagname, int32_t val)
{
if(val > 0 && val < 32768)
return true;
return false;
}
DEFINE_int32(port, 6379, "what is the port ?");
int main(int argc, char **argv)
{
ParseCommandLineFlags(&argc, &argv, true);
bool isPort = RegisterFlagValidator(&FLAGS_port, &ValidatePort);
return 0;
}
建议在定义参数后,立即注册检查函数。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
#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <gflags/gflags.h>
using namespace std;
using namespace google;
static bool ValidatePort(const char *flagname, int32_t val)
{
if(val > 0 && val < 32768)
return true;
return false;
}
DEFINE_int32(port, 6379, "what is the port ?");
DEFINE_string(ip, "127.0.0.1", "My ip ?");
int main(int argc, char **argv)
{
ParseCommandLineFlags(&argc, &argv, true);
bool isPort = RegisterFlagValidator(&FLAGS_port, &ValidatePort);
int32_t Port = FLAGS_port;
string Ip = FLAGS_ip;
cout << Port << endl;
cout << Ip << endl;
return 0;
}
参数可以通过下面方式指定,参数之间可以没有顺序。
(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来包含其他的文件。
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
#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <gflags/gflags.h>
using namespace std;
using namespace google;
static bool ValidatePort(const char *flagname, int32_t val)
{
if(val > 0 && val < 32768)
return true;
return false;
}
DEFINE_int32(port, 6379, "what is the port ?");
int main(int argc, char **argv)
{
ParseCommandLineFlags(&argc, &argv, true);
bool isPort = RegisterFlagValidator(&FLAGS_port, &ValidatePort);
return 0;
}
建议在定义参数后,立即注册检查函数。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
#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <gflags/gflags.h>
using namespace std;
using namespace google;
static bool ValidatePort(const char *flagname, int32_t val)
{
if(val > 0 && val < 32768)
return true;
return false;
}
DEFINE_int32(port, 6379, "what is the port ?");
DEFINE_string(ip, "127.0.0.1", "My ip ?");
int main(int argc, char **argv)
{
ParseCommandLineFlags(&argc, &argv, true);
bool isPort = RegisterFlagValidator(&FLAGS_port, &ValidatePort);
int32_t Port = FLAGS_port;
string Ip = FLAGS_ip;
cout << Port << endl;
cout << Ip << endl;
return 0;
}
参数可以通过下面方式指定,参数之间可以没有顺序。
(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来包含其他的文件。
相关文章推荐
- Google开源解析命令行参数的“神器”gflags
- Google开源命令行参数解析库gflags
- Google开源命令行参数解析库gflags
- 用Google的gflags解析命令行参数
- breakpad是Google开源的一套跨平台工具
- 用Google的gflags优雅的解析命令行参数(一)
- 用Google的gflags优雅的解析命令行参数
- 用Google的gflags优雅的解析命令行参数
- 用Google的gflags优雅的解析命令行参数
- 用Google的gflags轻松的编码解析命令行参数(二)
- 用Google的gflags轻松的编码解析命令行参数
- 用Google的gflags优雅的解析命令行参数
- Google Closure Compiler开源的javascript压缩加密工具
- 【善用工具】Google的gflags
- 源自Google、Facebook、Netflix和Cisco的10款开源安全工具很值得回味
- Google开源基于Deep Learning的word2vec工具
- Google开源网页加速工具Page Speed
- 利用开源工具搭一套汉英翻译系统(一):预处理工具
- 利用开源工具搭一套汉英翻译系统(二):词对齐
- 深度解析】Google第二代深度学习引擎TensorFlow开源(CMU邢波独家点评、白皮书全文、视频翻译)