命令行参数解析 gflags - 环境准备

说明

  • gflags是google实现的命令行参数解析库,支持C++和python语言。
  • 相对于标准库中的getopt, getopt_long,getopt_long_only,优势如下
  1. 同时支持长短参数,不需要使用多个函数解析。
  2. 使用简单,解析过程进行封装了,不需要无意义的干涉。
  3. 自动生成帮助信息,也可以自定义。

环境安装

  1. 下载地址
https://github.com/gflags/gflags/releases
* 当前最新版本是:gflags 2.2.2
  1. 编译安装
  • 使用cmake编译工具编译和安装。
编译:
mkdir build && cd build && cmake .. && make 
安装:
sudo make install

简单使用

  • 示例代码
#include <gflags/gflags.h> //gflags 头文件
#include <iostream>

using namespace std;

DEFINE_string(dst, "xxxx.bmp", "dst bmp file"); //定义命令行参数
int main(int argc, char *args[]){
    gflags::ParseCommandLineFlags(&argc, &args, true); //解析命令行参数
    
    cout << FLAGS_dst << endl; //获取解析出的命令行参数

    return 0;
}
  • 编译
使用带线程版本:
g++ gflagsTest.cpp -lgflags -lpthread -o test
使用不带线程版本:
g++ gflagsTest.cpp -lgflags_nothreads -o test
  • 运行
root@ubuntu:~/workspace$ ./test -dst=a.bmp
a.bmp
root@ubuntu:~/workspace$ ./test --dst=a.bmp
a.bmp
root@ubuntu:~/workspace$ ./test --dst a.bmp
a.bmp
root@ubuntu:~/workspace$ ./test -dst a.bmp
a.bmp
root@ubuntu:~/workspace$ ./test 
xxxx.bmp
root@ubuntu:~/workspace$ ./test -dst
ERROR: flag '-dst' is missing its argument; flag description: dst bmp file
root@ubuntu:~/workspace$ ./test -help
test: Warning: SetUsageMessage() never called

  Flags from /home/lwh/workspace/gflags-2.2.2/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 /home/lwh/workspace/gflags-2.2.2/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 /home/lwh/workspace/gflags-2.2.2/src/gflags_reporting.cc:
    -help (show help on all flags [tip: all flags can have two dashes])
      type: bool default: false currently: true
    -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



  Flags from gflagsTest.cpp:
    -dst (dst bmp file) type: string default: "xxxx.bmp"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值