google gflags的参数解析,便捷实用
命令行參數(shù)解析,一直是我們后段開發(fā)人員需要經(jīng)常使用的一個(gè)功能,用來(lái)從終端解析接口的輸入 ,并做出對(duì)應(yīng)的處理。這里為使用C++/python的開發(fā)人員推薦一個(gè)便捷的命令行解析接口集 gflags。
我們之前使用C的getopt/getopt_long 函數(shù)需要自己使用其接口并編寫大量的周邊代碼達(dá)到解析參數(shù)的目的,如果轉(zhuǎn)到C++還使用上面的函數(shù),代碼會(huì)過(guò)于冗余。那么gflags就很好的解決了這個(gè)問(wèn)題,我們不需要再數(shù)著冒號(hào) "a🅱?cd:"添加參數(shù)了,不需要為每個(gè)傳入的參數(shù)做類型轉(zhuǎn)化。
接下來(lái)的gflags 測(cè)試代碼可以提前向下看一看,耳目一新,清晰明了。
支持的數(shù)據(jù)類型
- DEFINE_bool
- DEFINE_int32 : 32位的整型
- DEFINE_uint32:無(wú)符號(hào)32位整型
- DEFINE_int64:64位整型
- DEFINE_uint64:無(wú)符號(hào)64位整型
- DEFINE_double:浮點(diǎn)類型
- DEFINE_string: C++ 的string類型
安裝
詳情可以參考:
install gflags
- mac : brew install gflags
- 其他linux系統(tǒng):
$ tar xzf gflags-$version-source.tar.gz $ cd gflags-$version $ mkdir build && cd build $ ccmake ..- Press 'c' to configure the build system and 'e' to ignore warnings.- Set CMAKE_INSTALL_PREFIX and other CMake variables and options.- Continue pressing 'c' until the option 'g' is available.- Then press 'g' to generate the configuration files for GNU Make.$ make $ make test (optional) $ make install (optional)
以上如果不進(jìn)行對(duì)應(yīng)的編譯參數(shù)設(shè)置,默認(rèn)頭文件和動(dòng)態(tài)庫(kù)是安裝在/usr/local/include/gflags 以及 /usr/local/lib之中
使用
主要使用如下幾個(gè)接口進(jìn)行參數(shù)相關(guān)的操作。
DEFINE_uint32這樣的接口定義我們的參數(shù)類型,參數(shù)名稱,參數(shù)默認(rèn)值SetVersionString(const std::string& version)自己設(shè)置程序版本,主要是在用gflags編譯的文件的命令行輸入–version 輸出版本信息void SetUsageMessage(const std::string& usage)自己設(shè)置運(yùn)行 --help時(shí)的幫助信息,默認(rèn)會(huì)打印gflags相關(guān)的幫助信息uint32 ParseCommandLineFlags(int *argc, char*** argv, bool remove_flags)解析傳入的參數(shù)
-void ShutDownCommandLineFlags()釋放解析參數(shù)過(guò)程中分配的空間
編寫如下測(cè)試代碼:
#include <gflags/gflags.h>
#include <iostream>
#include <string>using namespace std;
using namespace google;// default argvs from command line
DEFINE_int64(count,100,"count of nums");
DEFINE_string(entry,"str_string","the first of string");
DEFINE_bool(judge, false, "judge something");string g_version;
string g_help;string get_version() {g_version = "1.3";return g_version;
}string get_help_info() {g_help = "help info messages";return g_help;
}int main(int argc, char *argv[]) {// Sets the version stringSetVersionString(get_version());SetUsageMessage(get_help_info());// Looks for flags in argv and parses them.ParseCommandLineFlags(&argc,&argv, true);cout << "count = " << FLAGS_count << endl;cout << "entry = " << FLAGS_entry << endl;if (FLAGS_judge) {cout << "judge is true !" << endl;} else {cout << "judge is false !" << endl;}// Clean up memory allocated by flags.ShutDownCommandLineFlags();return 0;
}
編譯:
g++ -std=c++11 gflags_test.cc -o gflags_test -lgflags
這個(gè)編譯適用于gflags安裝在默認(rèn)路徑下,如果gflags安裝在自己指定的目錄,則建議編寫Makefile更方便一點(diǎn):
GFLAGS_DIR = /usr/local/include/gflags
LIB_DIR = /usr/local/lib/
gflags_test: gflags_test.ccg++ -I${GFLAGS_DIR} -L${LIB_DIR} gflags_test.cc -o gflags_test -lgflags
clean:rm -f gflags_test
運(yùn)行:
以下為我的測(cè)試過(guò)程
cpp_practice % ./gflags_test #默認(rèn)參數(shù)
count = 100
entry = str_string
judge is false !% ./gflags_test --count 20 --entry test_argv --judge true #指定參數(shù)
count = 20
entry = test_argv
judge is true !cpp_practice % ./gflags_test --version #查看版本信息
gflags_test version 1.3cpp_practice % ./gflags_test --help #查看幫助信息
gflags_test: help info messages
以上同樣的解析過(guò)程,如果是getopt_long,則需要至少3倍的代碼量才能夠?qū)崿F(xiàn)相同的功能。
不過(guò)這個(gè)只能用在C++或者python中,就有點(diǎn)尷尬了
總結(jié)
以上是生活随笔為你收集整理的google gflags的参数解析,便捷实用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 上海欢乐谷生日票以身份证那天为准吗
- 下一篇: g-git 相关命令 及其 基本原理探索