Google gflags使用说明

时间:2024-02-25 12:52:05

一、gflags是什么

gflags是google的一个开源的处理命令行参数的库,使用c++开发,具备python接口。

 

二、下载安装

1.下载:https://gflags.github.io/gflags/

2.解压安装

tar zxvf gflags-2.0.tar.gz && cd gflags-2.0 && ./configure && make && make install

这时 gflags 库会默认安装在 /usr/local/lib/ 下,头文件放在 /usr/local/include/gflags/ 中。

 

三、gflags支持以下类型

将需要的命令行参数使用gflags的宏,DEFINE_xxxxx(变量名,默认值,help-string) 定义在文件当中。

  • 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

 

、若需要在其它文件中使用gflags的变量,可以使用宏定义声明下:DECLARE_xxx(变量名),之后在代码中便可以使用FLAGS_XXX格式使用命令行的参数了。

 

五、定制自己的help和version信息

  • version信息:使用google::SetVersionString(const std::string& usage)设定,使用google::VersionString访问
  • help信息:使用google::SetUsageMessage(const std::string& usage)设定,使用google::ProgramUsage访问
  • 注意:google::SetUsageMessage和google::SetVersionString必须在google::ParseCommandLineFlags之前执行

     一般在main函数的头几行编写这些信息。

 

六、特殊的flags

--flagfile:--flagfile=f 告诉commandlineflags从这个文件中读取出所有的命令行参数,f文件应该有特殊的格式要求,是一个参数的list,每行一个参数,格式如下:

--languages=english

(更详细的介绍请参考官网 https://gflags.github.io/gflags/

 

七、简单使用

 1 #include <iostream>
 2 #include <gflags/gflags.h>
 3 using namespace std;
 4 using namespace google;
 5 
 6 DEFINE_string(host, "127.0.0.1", "the server port"); 
 7 DEFINE_int32(port, 9090, "program listen port");
 8 DEFINE_bool(sign, true, "switch mode");
 9 
10 static std::string g_version;
11 static std::string g_help;
12 
13 std::string& getVersion() {
14     g_version = "0.1";
15     return g_version;
16 }
17 
18 std::string& getHelp() {
19     g_help = "help info";
20     return g_help;
21 }
22 
23 int main(int argc, char** argv) {
24     google::SetVersionString(getVersion());
25     google::SetUsageMessage(getHelp());
26     google::ParseCommandLineFlags(&argc, &argv, true);
27     cout << "host = " << FLAGS_host << endl;
28     cout << "port = " << FLAGS_port << endl;
29     if (FLAGS_sign) {
30       cout << "sign is true ..." << endl;
31     }   
32     else {
33       cout << "sign is false ..." << endl;
34     }   
35     google::ShutDownCommandLineFlags();
36     return 0;
37 }

编译,使用makefile

GFLAGS_DIR = /usr/local/include/gflags/
LIB_DIR = /usr/local/lib/
a.out: simple_flags.cpp
        g++ -I${GFLAGS_DIR} -L${LIB_DIR} simple_flags.cpp -lgflags
clean:
        $(RM) -r a.out

执行,不给参数:

./a.out

host = 127.0.0.1
port = 9090
sign is true ...

给参数:

./a.out -host 172.168.16.8 -port 2356

host = 172.168.16.8
port = 2356
sign is true ...

使用文件file,文件内容如下:

--host=172.16.12.10
--port=8955
--sign=false

 执行:

./a.out --flagfile=flag

host = 172.16.12.10
port = 8955
sign is false ...

查看version和help信息:

./a.out -version

a.out version 0.1

./a.out -help

a.out: help info

  Flags from simple_flags.cpp:
    -host (the server port) type: string default: "127.0.0.1"
    -port (program listen port) type: int32 default: 9090
    -sign (switch mode) type: bool default: true

  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 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

 

八、多文件引用(DECLARE_XXX使用)

gflagdef.h文件

1 #ifndef _GFLAG_DEF_H_
2 #define _GFLAG_DEF_H_
3 #include <gflags/gflags.h>
4 
5 DECLARE_int32(port);
6 DECLARE_string(host);
7 DECLARE_bool(sign);
8 
9 #endif

 

gflagdef.cpp文件

#include <gflags/gflags.h>
DEFINE_int32(port, 9001, "The server port");
DEFINE_string(host, "127.0.0.1", "listen port");
DEFINE_bool(sign, true, "switch mode");

 

main.cpp文件

#include <iostream>
#include "gflagdef.h"
using namespace std;
int main(int argc, char** argv) {
    google::ParseCommandLineFlags(&argc, &argv, true);
    cout << "host = " << FLAGS_host << endl;
    cout << "port = " << FLAGS_port << endl;
    if (FLAGS_sign) {
        cout << "sign is true ..." << endl;
    }   
    else {
        cout << "sign is false ..." << endl;
    }   
    google::ShutDownCommandLineFlags();
    return 0;
}

 

makefile文件

GFLAGS_DIR = /usr/local/include/gflags/
LIB_DIR = /usr/local/lib/
main: main.o flag.o
        g++ main.o flag.o -o main -lgflags
main.o: main.cpp
        g++ -c -I${GFLAGS_DIR} -L${LIB_DIR} main.cpp -lgflags -o main.o
flag.o: gflagdef.cpp
        g++ -c -I${GFLAGS_DIR} -L${LIB_DIR} gflagdef.cpp -lgflags -o flag.o
clean:
        $(RM) -r main *.o

 

编译&&执行:make&&./main

host = 127.0.0.1
port = 9001
sign is true ...