用Google的gflags轻松的编码解析命令行参数
2015-09-10 17:48
621 查看
支持的参数类型
gflags支持的类型有bool,int32,int64,uint64,double和string。可以说这些基本类型大体上满足了我们的需求。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
比如上文中,我就定义了confPath, port, daemon三个命令行参数,回顾一下:
1 2 3 | DEFINE_string(confPath, "../conf/setup.ini", "program configure file."); DEFINE_int32(port, 9090, "program listen port"); DEFINE_bool(daemon, true, "run daemon mode"); |
第一个字段 confPath就是命令行里要输入的参数名,比如 –confPath=./love.ini
第二个字段”../conf/setup.ini”,就是如果命令行里没指定这个参数,那默认值就是 ../conf/setup.ini
第三个字段”program configure file.”,就是这个参数的帮助说明信息,当用户输入 –hlep 的时候,会显示出来。
代码中使用这个变量
以前我们使用getopt_long函数来自己解析命令行参数的时候,都得内部定义一个变量来保存从命令行得到的值。后续就可以使用这个变量来完成相应的代码逻辑。那其实,DEFINE_string等“指令”就相当于定义了变量,只不过变量名多了个前缀 “FLAGS_“。即,我们可以在代码里面直接操作FLAGS_confPath,FLAGS_port,FLAGS_port,FLAGS_daemon这三个变量。解析命令行参数
gflags是使用ParseCommandLineFlags这个方法来完成命令行参数的解析的。具体如下:1 | gflags::ParseCommandLineFlags(&argc, &argv, true); |
如果不好理解的话,没关系,来一段代码就明白什么意思了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <iostream> #include <gflags/gflags.h> using namespace std; DEFINE_string(confPath, "../conf/setup.ini", "program configure file."); DEFINE_int32(port, 9090, "program listen port"); DEFINE_bool(daemon, true, "run daemon mode"); int main(int argc, char** argv) { for (int i = 0; i < argc; i++) { printf("argv[%d] = %s\n", i, argv[i]); } printf("---------------here--------------\n"); gflags::SetVersionString("1.0.0.0"); gflags::SetUsageMessage("Usage : ./demo "); gflags::ParseCommandLineFlags(&argc, &argv, true); for (int i = 0; i < argc; i++) { printf("argv[%d] = %s\n", i, argv[i]); } printf("---------------there--------------\n"); cout << "confPath = " << FLAGS_confPath << endl; cout << "port = " << FLAGS_port << endl; if (FLAGS_daemon) { cout << "run background ..." << endl; } else { cout << "run foreground ..." << endl; } cout << "good luck and good bye!" << endl; gflags::ShutDownCommandLineFlags(); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 | [amcool@leoox build]$ ./demo --port=8888 --confPath=./happy.ini --daemon argv[0] = ./demo argv[1] = --port=8888 argv[2] = --confPath=./happy.ini argv[3] = --daemon ---------------here-------------- argv[0] = ./demo ---------------there-------------- confPath = ./happy.ini port = 8888 run background ... good luck and good bye! |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [amcool@leoox build]$ ./demo --port=8888 --confPath=./happy.ini --daemon argv[0] = ./demo argv[1] = --port=8888 argv[2] = --confPath=./happy.ini argv[3] = --daemon ---------------here-------------- argv[0] = ./demo argv[1] = --port=8888 argv[2] = --confPath=./happy.ini argv[3] = --daemon ---------------there-------------- confPath = ./happy.ini port = 8888 run background ... good luck and good bye! |
参数检查
按照以前的习惯,我们可以获取到所有参数的值后,再在代码里面进行判断这个参数是否是我们想要的。比如,我们需要端口是36800 到 36888之间的,那我们可以这样检查。1 2 3 4 | if (FLAGS_port < 36800 || FLAGS_port > 36888) { printf("port must [36800, 36888]\n"); return -1; } |
1 2 3 4 5 6 7 8 9 10 11 | static bool ValidatePort(const char* flagname, gflags::int32 value) { if (value >= 36800 && value <= 36888) { printf("param(%s) = (%d) is valid!\n", flagname, value); return true; } printf("param(%s) = (%d) is invalid!\n", flagname, value); return false; } DEFINE_int32(port, 36810, "program listen port"); static const bool validPort = gflags::RegisterFlagValidator(&FLAGS_port, &ValidatePort); |
1 2 3 4 | [amcool@leoox build]$ ./demo --port=36889 --confPath=./happy.ini --daemon param(port) = (36889) is invalid! param(port) = (36810) is valid! ERROR: failed validation of new value '36889' for flag 'port' |
其他代码文件使用参数变量
正常来说,我们的代码不可能只有1个cpp,还会有很多模块。而每个模块可能会使用到不同的参数值。所以我们之前在demo.cpp定义的参数变量(比如FLAGS_port),在其他模块怎么引用和使用呢?so easy,与DEFINE相对应的有DECLARE。声明一下,就可以使用了。DECLARE_bool: boolean
DECLARE_int32: 32-bit integer
DECLARE_int64: 64-bit integer
DECLARE_uint64: unsigned 64-bit integer
DECLARE_double: double
DECLARE_string: C++ string
来一段简单的代码,就一目了然啦。
示例代码目录结构
1 2 3 4 5 6 7 8 9 10 | [amcool@leoox demo]$ tree . |-- CMakeLists.txt |-- build |-- demo.cpp |-- logic.cpp `-- logic.h 1 directory, 4 files [amcool@leoox demo]$ |
logic.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #ifndef _LEOOX_LOGIC_H_ #define _LEOOX_LOGIC_H_ #include <iostream> #include <gflags/gflags.h> using namespace std; DECLARE_string(confPath); DECLARE_int32(port); DECLARE_bool(daemon); int process(); #endif |
logic.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include "logic.h" int process() { printf("----------------- process start ---------------\n"); cout << "confPath = " << FLAGS_confPath << endl; cout << "port = " << FLAGS_port << endl; if (FLAGS_daemon) { cout << "run background ..." << endl; } else { cout << "run foreground ..." << endl; } printf("----------------- process end ---------------\n"); return 0; } |
demo.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include "logic.h" DEFINE_string(confPath, "../conf/setup.ini", "program configure file."); DEFINE_bool(daemon, true, "run daemon mode"); static bool ValidatePort(const char* flagname, gflags::int32 value) { if (value >= 36800 && value <= 36888) { printf("param(%s) = (%d) is valid!\n", flagname, value); return true; } printf("param(%s) = (%d) is invalid!\n", flagname, value); return false; } DEFINE_int32(port, 36810, "program listen port"); static const bool validPort = gflags::RegisterFlagValidator(&FLAGS_port, &ValidatePort); int main(int argc, char** argv) { gflags::SetVersionString("1.0.0.0"); gflags::SetUsageMessage("Usage : ./demo "); gflags::ParseCommandLineFlags(&argc, &argv, false); process(); cout << "good luck and good bye!" << endl; gflags::ShutDownCommandLineFlags(); return 0; } |
CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 | project(demo) cmake_minimum_required(VERSION 2.8) set(CMAKE_VERBOSE_MAKEFILE on) include_directories(".") include_directories("/home/leoox/local/gflags-2.1.1/include") link_directories("/home/leoox/local/gflags-2.1.1/lib") add_executable(demo demo.cpp logic.cpp) target_link_libraries(demo gflags pthread) |
运行结果
1 2 3 4 5 6 7 8 9 10 | [amcool@leoox build]$ ./demo --port=36850 --confPath=./love.ini param(port) = (36850) is valid! param(port) = (36850) is valid! ----------------- process start --------------- confPath = ./love.ini port = 36850 run background ... ----------------- process end --------------- good luck and good bye! [amcool@leoox build]$ |
转自:http://www.leoox.com/?p=275
相关文章推荐
- Algorithms—15.3Sum
- mongo表id自动生成
- Algorithm --> 树中求顶点A和B共同祖先
- google 手机号码校验库libphonenumber
- 【Google Dagger2】配置
- Mangos源码分析(14):Mangos预编译头文件及模块划分随想
- Mangos源码分析(13):再谈登录服的实现
- Mangos源码分析(12):服务器公共组件实现之事件与信号
- Mangos源码分析(9):服务器公共组件实现之环形缓冲区
- Mangos源码分析(8):服务器公共组件实现之消息队列
- Django学习笔记1
- go sample - hello world
- Golang资源
- Golang 常用字符串处理方法汇总
- Mangos源码分析(7):服务器公共组件实现之游戏主循环
- Mangos源码分析(6):登录服的设计之功能需求
- Google下拉刷新组件---SwipeRefreshLayout(一)
- Mangos源码分析(4):服务器结构探讨之继续世界服
- Mangos源码分析(3):简单的世界服实现
- Mangos源码分析(2):服务器结构探讨之登录服的负载均衡