Googlegflags使用说明
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Googlegflags使⽤说明
⼀、gflags是什么
gflags是google的⼀个开源的处理命令⾏参数的库,使⽤c++开发,具备python接⼝。
⼆、下载安装
1.下载:
2.解压安装
tar zxvf gflags-2.0.tar.gz && cd gflags-2.0 && ./configure && make && make install
这时 gflags 库会默认安装在 /usr/local/lib/ 下,头⽂件放在 /usr/local/include/gflags/ 中。
三、gflags⽀持以下类型
将需要的命令⾏参数使⽤gflags的宏,DEFINE_xxxxx(变量名,默认值,help-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
四、若需要在其它⽂件中使⽤gflags的变量,可以使⽤宏定义声明下:DECLARE_xxx(变量名),之后在代码中便可以使⽤FLAGS_XXX格式使⽤命令⾏的参数了。
五、定制⾃⼰的help和version信息
version信息:使⽤google::SetVersionString(const std::string& usage)设定,使⽤google::VersionString访问
help信息:使⽤google::SetUsageMessage(const std::string& usage)设定,使⽤google::ProgramUsage访问
注意:google::SetUsageMessage和google::SetVersionString必须在google::ParseCommandLineFlags之前执⾏⼀般在main函数的头⼏⾏编写这些信息。
六、特殊的flags
--flagfile:--flagfile=f 告诉commandlineflags从这个⽂件中读取出所有的命令⾏参数,f⽂件应该有特殊的格式要求,是⼀个参数的list,每⾏⼀个参数,格式如下:
--languages=english
(更详细的介绍请参考官⽹)
七、简单使⽤
1 #include <iostream>
2 #include <gflags/gflags.h>
3using namespace std;
4using namespace google;
5
6 DEFINE_string(host, "127.0.0.1", "the server port");
7 DEFINE_int32(port, 9090, "program listen port");
8 DEFINE_bool(sign, true, "switch mode");
9
10static std::string g_version;
11static std::string g_help;
12
13 std::string& getVersion() {
14 g_version = "0.1";
15return g_version;
16 }
17
18 std::string& getHelp() {
19 g_help = "help info";
20return g_help;
21 }
22
23int main(int argc, char** argv) {
24 google::SetVersionString(getVersion());
25 google::SetUsageMessage(getHelp());
26 google::ParseCommandLineFlags(&argc, &argv, true);
27 cout << "host = " << FLAGS_host << endl;
28 cout << "port = " << FLAGS_port << endl;
29if (FLAGS_sign) {
30 cout << "sign is true ..." << endl;
31 }
32else {
33 cout << "sign is false ..." << endl;
34 }
35 google::ShutDownCommandLineFlags();
36return0;
37 }
编译,使⽤makefile
GFLAGS_DIR = /usr/local/include/gflags/
LIB_DIR = /usr/local/lib/
a.out: simple_flags.cpp
g++ -I${GFLAGS_DIR} -L${LIB_DIR} simple_flags.cpp -lgflags clean:
$(RM) -r a.out
执⾏,不给参数:
./a.out
host = 127.0.0.1
port = 9090
sign is true ...
给参数:
./a.out -host 172.168.16.8 -port 2356
host = 172.168.16.8
port = 2356
sign is true ...
使⽤⽂件file,⽂件内容如下:
--host=172.16.12.10
--port=8955
--sign=false
执⾏:
./a.out --flagfile=flag
host = 172.16.12.10
port = 8955
sign is false ...
查看version和help信息:
./a.out -version
a.out version 0.1
./a.out -help
a.out: help info
Flags from simple_flags.cpp:
-host (the server port) type: string default: "127.0.0.1"
-port (program listen port) type: int32 default: 9090
-sign (switch mode) type: bool default: true
Flags from src/:
-flagfile (load flags from file) type: string default: ""
-fromenv (set flags from the environment [use 'export FLAGS_flag1=value']) type: string default: ""
-tryfromenv (set flags from the environment if present) type: string
default: ""
-undefok (comma-separated list of flag names that it is okay to specify on
the command line even if the program does not define a flag with that
name. IMPORTANT: flags in this list that have arguments MUST use the flag=value format) type: string default: ""
Flags from src/gflags_:
-tab_completion_columns (Number of columns to use in output for tab
completion) type: int32 default: 80
-tab_completion_word (If non-empty, HandleCommandLineCompletions() will hijack the process and attempt to do bash-style command line flag
completion on this value.) type: string default: ""
Flags from src/gflags_:
-help (show help on all flags [tip: all flags can have two dashes])
type: bool default: false currently: true
-helpfull (show help on all flags -- same as -help) type: bool
default: false
-helpmatch (show help on modules whose name contains the specified substr) type: string default: ""
-helpon (show help on the modules named by this flag value) type: string
default: ""
-helppackage (show help on all modules in the main package) type: bool
default: false
-helpshort (show help on only the main module for this program) type: bool
default: false
-helpxml (produce an xml version of help) type: bool default: false
-version (show version and build info and exit) type: bool default: false
⼋、多⽂件引⽤(DECLARE_XXX使⽤)
gflagdef.h⽂件
1 #ifndef _GFLAG_DEF_H_
2#define _GFLAG_DEF_H_
3 #include <gflags/gflags.h>
4
5 DECLARE_int32(port);
6 DECLARE_string(host);
7 DECLARE_bool(sign);
8
9#endif
gflagdef.cpp⽂件
#include <gflags/gflags.h>
DEFINE_int32(port, 9001, "The server port");
DEFINE_string(host, "127.0.0.1", "listen port");
DEFINE_bool(sign, true, "switch mode");
main.cpp⽂件
#include <iostream>
#include "gflagdef.h"
using namespace std;
int main(int argc, char** argv) {
google::ParseCommandLineFlags(&argc, &argv, true);
cout << "host = " << FLAGS_host << endl;
cout << "port = " << FLAGS_port << endl;
if (FLAGS_sign) {
cout << "sign is true ..." << endl;
}
else {
cout << "sign is false ..." << endl;
}
google::ShutDownCommandLineFlags();
return0;
}
makefile⽂件
GFLAGS_DIR = /usr/local/include/gflags/
LIB_DIR = /usr/local/lib/
main: main.o flag.o
g++ main.o flag.o -o main -lgflags
main.o: main.cpp
g++ -c -I${GFLAGS_DIR} -L${LIB_DIR} main.cpp -lgflags -o main.o flag.o: gflagdef.cpp
g++ -c -I${GFLAGS_DIR} -L${LIB_DIR} gflagdef.cpp -lgflags -o flag.o clean:
$(RM) -r main *.o
编译&&执⾏:make&&./main
host = 127.0.0.1
port = 9001
sign is true ...。