前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >google gflag使用方法举例

google gflag使用方法举例

作者头像
Gxjun
发布2018-03-27 11:57:07
1.5K0
发布2018-03-27 11:57:07
举报
文章被收录于专栏:mlml

前言:

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

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

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

代码语言:javascript
复制
 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 }

使用命令编译:

代码语言:javascript
复制
g++ gflag.cc -o gflags -lgflags  -lpthread

执行命令:

解析:

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-06-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档