【C++】google gflags详解

参考博客;https://blog.csdn.net/lezardfu/article/details/23753741

0、简介

gflags是google的一个开源的处理命令行参数的库,使用c++开发,具备python接口,可以替代getopt。gflags使用起来比getopt方便,但是不支持参数的简写(例如getopt支持–list缩写成-l,gflags不支持)。
gflags 源码下载地址: https://github.com/gflags/gflags
官方说明文档:https://gflags.github.io/gflags/
ubuntu下使用命令安装:

sudo apt-get install libgflags*
1、命令行标记 和 命令行参数

例如:fgrep -l -f /var/tmp/foo johannes brahms
“-l” 和 “-f /var/tmp/foo”是命令行标记;
“johannes” 和 “brahms”是命令行参数
注意:gflags只能处理命令行标记(Commandline flags ),不会处理命令行参数(commandline arguments)

2、定义命令行标记

头文件:#include <gflags/gflags.h>
支持的数据类型:
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
例如:
DEFINE_bool(quiet, true, “打印详细信息”);
DEFINE_string(infile, “”, “输入文件”);
格式说明:
DEFINE宏有三个参数:标志的名称、默认值以及描述其用法的字符串,当使用–help命令行标记时,会打印这些说明信息

3、使用命令行标记

使用“FLAGS_”前缀的宏,来访问命令行标记
例如:

  //首先通过gflags来分析命令行标记
  gflags::ParseCommandLineFlags(&argc, &argv, true);	
  if (FLAGS_quiet){
	std::cout << "hello world" << std::endl;
    std::cout << "fileName = " << FLAGS_infile << std::endl;;
  }
4、gflgs常用函数(参见头文件《gflags.h》)
//使用--version时,显示版本信息
void SetVersionString(const std::string& version); 
//使用--help时,显示帮助信息,不用添加命令行标志的信息,命令行标志的信息在宏 DEFINE_ 中已经添加了
void SetUsageMessage(const std::string& usage)		
//主动打印帮助信息
void ShowUsageWithFlags(const char *argv0);	
//主动打印帮助信息,参数restrict表示显示哪个文件中使用宏 DEFINE_ 定义的命令行标记
void ShowUsageWithFlagsRestrict(const char *argv0, const char *restrict);	

例如:在flagsV2.cpp中使用宏 DEFINE_ 定义了命令行标记,
1> 如果只打印flagsV2.cpp中定义的命令行标记,使用如下函数

gflags::ShowUsageWithFlagsRestrict(argv[0], "flagsV2");

打印信息:

$ ./flagsV2 
flagsV2: 用法: flags <子命令> <参数>

子命令列表:
  cmd1 这个是子命令


  Flags from flagsV2.cpp:
    -infile (输入文件) type: string default: ""
    -quiet (打印详细信息) type: bool default: false

2> 如果要打印全部信息,包括gflags本身的一些命令行标记,使用如下函数

 gflags::ShowUsageWithFlags(argv[0]); 
//或者 gflags::ShowUsageWithFlagsRestrict(argv[0], ""); 
//只要restrict形参不是flagsV2(或者flagsV2的子串,如flag)

打印信息:

$ ./flagsV2t 
flagsV2: 用法: flags <子命令> <参数>

子命令列表:
  cmd1 这个是子命令


  Flags from flagsV2.cpp:
    -infile (输入文件) type: string default: ""
    -quiet (打印详细信息) type: bool default: false

  Flags from src/gflags.cc:
    -flagfile (load flags from file) type: string default: ""
    -fromenv (set flags from the environment [use 'export FLAGS_flag1=value'])
      type: string default: ""
    -tryfromenv (set flags from the environment if present) type: string
      default: ""
    -undefok (comma-separated list of flag names that it is okay to specify on
      the command line even if the program does not define a flag with that
      name.  IMPORTANT: flags in this list that have arguments MUST use the
      flag=value format) type: string default: ""

  Flags from src/gflags_completions.cc:
    -tab_completion_columns (Number of columns to use in output for tab
      completion) type: int32 default: 80
    -tab_completion_word (If non-empty, HandleCommandLineCompletions() will
      hijack the process and attempt to do bash-style command line flag
      completion on this value.) type: string default: ""

  Flags from src/gflags_reporting.cc:
    -help (show help on all flags [tip: all flags can have two dashes])
      type: bool default: false
    -helpfull (show help on all flags -- same as -help) type: bool
      default: false
    -helpmatch (show help on modules whose name contains the specified substr)
      type: string default: ""
    -helpon (show help on the modules named by this flag value) type: string
      default: ""
    -helppackage (show help on all modules in the main package) type: bool
      default: false
    -helpshort (show help on only the main module for this program) type: bool
      default: false
    -helpxml (produce an xml version of help) type: bool default: false
    -version (show version and build info and exit) type: bool default: false
5、最简单的示例
#include <gflags/gflags.h>
#include <iostream>

#ifndef GFLAGS_GFLAGS_H_
namespace gflags = google;
#endif  // GFLAGS_GFLAGS_H_

DEFINE_bool(quiet, true, "打印详细信息");
DEFINE_string(infile, "", "输入文件");

int main(int argc, char** argv){
	gflags::ParseCommandLineFlags(&argc, &argv, true);
	if (FLAGS_quiet){
		std::cout << "hello world" << std::endl;
		std::cout << "fileName = " << FLAGS_infile << std::endl;;
	}
}

编译时,需要链接gflags: -lgflags

g++ -o flagsV0 flagsV0.cpp -lgflags
6、使用子命令的方法

原理是,利用gflags不会处理命令行参数,只处理命令行标记的特性

#include <gflags/gflags.h>
#include <iostream>

#ifndef GFLAGS_GFLAGS_H_
namespace gflags = google;
#endif  // GFLAGS_GFLAGS_H_

DEFINE_bool(quiet, false, "打印详细信息");
DEFINE_string(infile, "", "输入文件");

int main(int argc, char** argv){
	gflags::SetUsageMessage("用法: flags <子命令> <参数>\n\n"
			"子命令列表:\n"
			"  cmd1 这个是子命令\n");

	gflags::ParseCommandLineFlags(&argc, &argv, true);

	if(argc!=2){
		//gflags::ShowUsageWithFlagsRestrict(argv[0], "aaa");
		gflags::ShowUsageWithFlagsRestrict(argv[0], "flagsV2");
		//gflags::ShowUsageWithFlags(argv[0]);
		return 0;
	}
	
	if (FLAGS_quiet){
		std::cout << "hello world" << std::endl;
		std::cout << "fileName = " << FLAGS_infile << std::endl;;
	}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭老二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值