1. 程式人生 > >google gflag使用方法舉例

google gflag使用方法舉例

.com span http std tor 使用方法 方法 ogl log

前言:

  1. gflag是一種命令行編碼參數解析工具,開源地址: https://github.com/gflags/gflags , 在caffe框架也使用了gflag來編碼解析命令行.

那麽什麽是gflag呢? 下面簡單描述一下gflag:

gflag支持如下數據格式:string ,double,int32, int64,uint64,bool需求:

 1 #include<iostream>
 2 #include<gflags/gflags.h>
 3 #include<string>
 4 #include<cstring>
 5
#include<cstdio> 6 #include<cstdlib> 7 8 using namespace std; 9 using namespace google; 10 11 static bool check( const char * flagname , google::int32 age ) 12 { 13 14 std::cout<<"the age "<< age <<std::ends; 15 if(age>16) 16 { 17 std::cout<<"
is valid ~"<<std::endl; 18 return true; 19 } 20 std::cout<<" is invalid~"<<std::endl; 21 return false; 22 } 23 24 DEFINE_string(username , "xijun.gong" , "the student of name"); 25 DEFINE_int32(age , 14 , "the student of age"); 26 DEFINE_double(grade , 89
,"the student of grade"); 27 28 static const bool validate = google::RegisterFlagValidator(&FLAGS_age , &check); 29 int main(int argc, char** argv) { 30 google::SetVersionString("0.0.0.1"); 31 google::SetUsageMessage("Usage: ./gflags"); 32 google::ParseCommandLineFlags(&argc, &argv, true); 33 std::cout <<"Student Infomation: "<<std::endl; 34 std::cout << "username : " << FLAGS_username <<std::endl; 35 std::cout <<"age: " << FLAGS_age << std::endl; 36 std::cout <<"grade: "<< FLAGS_grade <<std::endl; 37 return 0; 38 }

使用命令編譯:

g++ gflag.cc -o gflags -lgflags  -lpthread

執行命令:

技術分享

解析:

   當我們age<16是,check返回的是False,gflag註冊失敗,程序啟動失敗. 當大於16時,程序正常啟動.

google gflag使用方法舉例