google gflags的参数解析,便捷实用
命令行參數解析,一直是我們后段開發人員需要經常使用的一個功能,用來從終端解析接口的輸入 ,并做出對應的處理。這里為使用C++/python的開發人員推薦一個便捷的命令行解析接口集 gflags。
我們之前使用C的getopt/getopt_long 函數需要自己使用其接口并編寫大量的周邊代碼達到解析參數的目的,如果轉到C++還使用上面的函數,代碼會過于冗余。那么gflags就很好的解決了這個問題,我們不需要再數著冒號 "a🅱?cd:"添加參數了,不需要為每個傳入的參數做類型轉化。
接下來的gflags 測試代碼可以提前向下看一看,耳目一新,清晰明了。
支持的數據類型
- DEFINE_bool
- DEFINE_int32 : 32位的整型
- DEFINE_uint32:無符號32位整型
- DEFINE_int64:64位整型
- DEFINE_uint64:無符號64位整型
- DEFINE_double:浮點類型
- DEFINE_string: C++ 的string類型
安裝
詳情可以參考:
install gflags
- mac : brew install gflags
- 其他linux系統:
$ 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)
以上如果不進行對應的編譯參數設置,默認頭文件和動態庫是安裝在/usr/local/include/gflags 以及 /usr/local/lib之中
使用
主要使用如下幾個接口進行參數相關的操作。
DEFINE_uint32這樣的接口定義我們的參數類型,參數名稱,參數默認值SetVersionString(const std::string& version)自己設置程序版本,主要是在用gflags編譯的文件的命令行輸入–version 輸出版本信息void SetUsageMessage(const std::string& usage)自己設置運行 --help時的幫助信息,默認會打印gflags相關的幫助信息uint32 ParseCommandLineFlags(int *argc, char*** argv, bool remove_flags)解析傳入的參數
-void ShutDownCommandLineFlags()釋放解析參數過程中分配的空間
編寫如下測試代碼:
#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
這個編譯適用于gflags安裝在默認路徑下,如果gflags安裝在自己指定的目錄,則建議編寫Makefile更方便一點:
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
運行:
以下為我的測試過程
cpp_practice % ./gflags_test #默認參數
count = 100
entry = str_string
judge is false !% ./gflags_test --count 20 --entry test_argv --judge true #指定參數
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
以上同樣的解析過程,如果是getopt_long,則需要至少3倍的代碼量才能夠實現相同的功能。
不過這個只能用在C++或者python中,就有點尷尬了
總結
以上是生活随笔為你收集整理的google gflags的参数解析,便捷实用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上海欢乐谷生日票以身份证那天为准吗
- 下一篇: 拍摄视频有水印怎么办?