gflags的使用

gflags是google开源的一套命令行参数解析工具,需要安装作为第三方库

gflags使用方法:
1.包含头文件

#include<gflags/gflags.h>

2.定义参数
DEFINE_type(参数变量名,参数默认值,”参数帮助信息”)
例如:

DEFINE_int32(port, 80, "listen port");

可以定义以下几种形式:

       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

3.解析命令行
加入main函数(头几行中)

google::ParseCommandLineFlags(&argc, &argv, true);

4.参数访问:FLAGS_变量名

std::cout<<"port:"<<FLAGS_port<<std::endl;

5.命名空间重定向

#ifndef GFLAGS_GFLAGS_H_
    namespace gflags = google;
#endif

猜你喜欢

转载自blog.csdn.net/htt789/article/details/80307344