google gflag使用方法举例

时间:2023-12-30 14:50:32

前言:

  1. gflag是一种命令行编码参数解析工具,开源地址: https://github.com/gflags/gflags , 在caffe框架也使用了gflag来编码解析命令行.

那么什么是gflag呢? 下面简单描述一下gflag:

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

 #include<iostream>
#include<gflags/gflags.h>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib> using namespace std;
using namespace google; static bool check( const char * flagname , google::int32 age )
{ std::cout<<"the age "<< age <<std::ends;
if(age>)
{
std::cout<<" is valid ~"<<std::endl;
return true;
}
std::cout<<" is invalid~"<<std::endl;
return false;
} DEFINE_string(username , "xijun.gong" , "the student of name");
DEFINE_int32(age , , "the student of age");
DEFINE_double(grade , ,"the student of grade"); static const bool validate = google::RegisterFlagValidator(&FLAGS_age , &check);
int main(int argc, char** argv) {
google::SetVersionString("0.0.0.1");
google::SetUsageMessage("Usage: ./gflags");
google::ParseCommandLineFlags(&argc, &argv, true);
std::cout <<"Student Infomation: "<<std::endl;
std::cout << "username : " << FLAGS_username <<std::endl;
std::cout <<"age: " << FLAGS_age << std::endl;
std::cout <<"grade: "<< FLAGS_grade <<std::endl;
return ;
}

使用命令编译:

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

执行命令:

google gflag使用方法举例

解析:

   当我们age<16是,check返回的是False,gflag注册失败,程序启动失败. 当大于16时,程序正常启动.