一、gflags是什么
gflags是google的一個開源的處理命令行參數的庫,使用c++開發,具備python接口。
二、下載安裝
1.下載:https://gflags.github.io/gflags/
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
(更詳細的介紹請參考官網 https://gflags.github.io/gflags/)
七、簡單使用
1 #include <iostream> 2 #include <gflags/gflags.h> 3 using namespace std; 4 using 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 10 static std::string g_version; 11 static std::string g_help; 12 13 std::string& getVersion() { 14 g_version = "0.1"; 15 return g_version; 16 } 17 18 std::string& getHelp() { 19 g_help = "help info"; 20 return g_help; 21 } 22 23 int 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; 29 if (FLAGS_sign) { 30 cout << "sign is true ..." << endl; 31 } 32 else { 33 cout << "sign is false ..." << endl; 34 } 35 google::ShutDownCommandLineFlags(); 36 return 0; 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/gflags.cc:
-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_completions.cc:
-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_reporting.cc:
-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(); return 0; }
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 ...
