前言:
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时,程序正常启动.
时间: 2024-10-14 14:09:41