簡介
Google 的 gflags 是一套命令行參數處理的開源庫。比 getopt 更方便,更功能強大,從 C++的庫更好的支持 C++(如 C++的 string 類型)。
example 源代碼先看 example 源代碼,然后逐步介紹。
example.cc
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <gflags/gflags.h> DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing"); DEFINE_string(languages, "english,french,german", "comma-separated list of languages to offer in the 'lang' menu"); int main(int argc, char **argv) { google::ParseCommandLineFlags(&argc, &argv, true); cout << "argc=" << argc << endl; if (FLAGS_big_menu) { cout << "big menu is ture" << endl; } else { cout << "big menu is flase" << endl; } cout << "languages=" << FLAGS_languages << endl; return 0; } |
運行程序
run
1
2 3 4 |
➜ bin ./sample argc=1 big menu is ture languages=english,french,german |
run
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
➜ bin ./sample --help sample: Warning: SetUsageMessage() never called Flags from /home/shougang/workspace/drive/Google/cmake_cpp_gflags/src/sample.cc: -big_menu (Include 'advanced' options in the menu listing) type: bool default: true -languages (comma-separated list of languages to offer in the 'lang' menu) type: string default: "english,french,german" Flags from src/gflags.cc: -flagfile (load flags from file) type: string default: "" ......... ➜ bin ./sample --helpshort sample: Warning: SetUsageMessage() never called Flags from /home/shougang/workspace/drive/Google/cmake_cpp_gflags/src/sample.cc: -big_menu (Include 'advanced' options in the menu listing) type: bool default: true -languages (comma-separated list of languages to offer in the 'lang' menu) type: string default: "english,french,german" |
在程序里定義參數
### 包含頭文件
header_file
1
|
#include <gflags/gflags.h> |
利用 gflag 提供的宏定義參數該宏的 3 個參數分別為命令行參數名,參數默認值,參數的幫助信息。
define_flags
1
2 |
DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing"); DEFINE_string(languages, "english,french,german", "comma-separated list of languages to offer in the 'lang' menu"); |
gflags 暫時支持如下參數的類型:
supported_types
1
2 3 4 5 6 |
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 |
訪問參數
通過 FLAGS_name 像正常變量一樣訪問標志參數。在這個程序中,通過 FLAGS_big_menu
和FLAGS_languages
訪問它們。
不同文件訪問參數
如果想再另外一個不是定義這個參數的文件訪問這個參數的話,以參數 FLAGS_big_menu
為例,用宏DECLARE_bool(big_menu)
來聲明引入這個參數。這個宏相當於做了extern FLAGS_big_menu
.
整合一起,初始化所有參數
定義號參數后,最后要告訴執行程序去處理命令行傳入的參數,使得 FLAGS_*
參數們得到正確賦值。
通常就是再main()
函數中調用;
set_up_flag
1
|
google::ParseCommandLineFlags(&argc, &argv, true); |
argc
和argv
就是 main 的入口參數,因為這個函數會改變他們的值,所以都是以指針傳入。
第三個參數被稱為remove_flags
。如果它是true
,ParseCommandLineFlags
會從argv
中移除標識和它們的參數,相應減少argc
的值。然后 argv 只保留命令行參數。
相反,remove_flags
是false
,ParseCommandLineFlags
會保留argc
不變,但將會重新調整它們的順序,使得標識再前面。
Note: ./sample --big_menu=false arg1
中再big_menu
是標識,false
是它的參數,arg1
是命令行參數。
命令行設置參數
gflags 提供多種命令行設置參數。
string
和int
之類,可以用如下方式:
set_languages
1
2 3 4 |
app_containing_foo --languages="chinese,japanese,korean" app_containing_foo -languages="chinese,japanese,korean" app_containing_foo --languages "chinese,japanese,korean" app_containing_foo -languages "chinese,japanese,korean" |
對於boolean
的標識來說,用如下方式:
set_boolean
1
2 3 4 |
app_containing_foo --big_menu app_containing_foo --nobig_menu app_containing_foo --big_menu=true app_containing_foo --big_menu=false |
和getopt()
一樣,--
將會終止標識的處理。所以在foo -f1 1 -- -f2 2
中, f1
被認為是一個標識,但f2
不會。
特殊標識
special_flags
1
2 3 4 5 6 7 |
--help 顯示文件中所有標識的完整幫助信息 --helpfull 和-help 一樣, --helpshort 只顯示當前執行文件里的標志 --helpxml 以 xml 凡是打印,方便處理 --version 打印版本信息,由 google::SetVersionString()設定 --flagfile -flagfile=f 從文件 f 中讀取命令行參數 ... |
具體見:http://gflags.googlecode.com/svn/trunk/doc/gflags.html