google好用的gFlags——解析命令行参数(支持c++和python)

一、简介

GFlags是Google开源的一套命令行参数处理的开源库,包括C++的版本和python 版本。
和 getopt() 之类的库不同,flag的定义可以散布在各个源码中,而不用放在一起。一个源码文件可以定义一些它自己的flag,链接了该文件的应用都能使用这些flag。这样就能非常方便地复用代码。如果不同的文件定义了相同的flag,链接时会报错。

二、在程序中定义Flags

定义一个flag是简单的:只需要使用你想用的类型的相应的宏就可以。
example:

// foo.cc
#include <gflags/glags.h>

DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing");
DEFINE_string(languages, "english,french,german",
              "comma-separated list of languages to offer in the 'lang' menu");

支持的类型:

  • 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宏包含三个参数:flag名、默认值、描述方法的帮助。帮助会在执行 --help flag时显示。
    可以在任何源文件中定义flag,但是每个只能定义一次。如果需要在多处使用,那么在一个文件中 DEFINE ,在其他文件中 DECLARE 。比较好的方法是在 .cc 文件中 DEFINE ,在 .h 文件中 DECLARE ,这样包含头文件即可使用flag了。

三、使用flag

定义的flag可以像正常的变量一样使用,只需在前面加上 FLAGS_前缀。如前面例子中的定义了 FLAGS_big_menuFLAGS_languages两个变量。可以像一般变量一样读写:

if (FLAGS_consider_made_up_languages)
  FLAGS_languages += ",klingon";   // implied by --consider_made_up_languages
if (FLAGS_languages.find("finnish") != string::npos)
  HandleFinnish();

四、初始化所有参数

定义好参数后,最后要告诉执行程序去处理命令行传入的参数,使得定义的FLAG参数得到正确赋值。
通常是在main()函数中调用

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

argcargv就是 main 的入口参数,因为这个函数会改变他们的值,所以都是以指针传入。
第三个参数被称为remove_flags。如果它是trueParseCommandLineFlags会从argv中移除标识和它们的参数,相应减少argc的值。然后argv 只保留命令行参数。
相反,remove_flagsfalseParseCommandLineFlags会保留argc不变,但将会重新调整它们的顺序,使得标识再前面。
Note: ./foo --big_menu=false arg1中再big_menu是标识,false是它的参数,arg1是命令行参数。

五、特殊标识


--help	shows all flags from all files, sorted by file and then by name; shows the flagname, its default value, and its help string
--helpfull	same as -help, but unambiguously asks for all flags (in case -help changes in the future)
--helpshort	shows only flags for the file with the same name as the executable (usually the one containing main())
--helpxml	like --help, but output is in xml for easier parsing
--helpon=FILE  	shows only flags defined in FILE.*
--helpmatch=S	shows only flags defined in *S*.*
--helppackage	shows flags defined in files in same directory as main()
--version	prints version info for the executable

六、示例代码

 需要先下载gflags代码,然后构建、安装

main.cpp

#include <iostream>

#include <gflags/gflags.h>

DEFINE_string(data_path,"./config/","data path");
DEFINE_string(config_path,"./data/","config path");
DEFINE_bool(is_check,false,"check flag");

using namespace std;

int main(int argc,char *argv[])
{
    google::SetUsageMessage("usage:");
    google::ParseCommandLineFlags(&argc,&argv,true);

    cout<<"argc="<<argc<<endl;
    cout<<"data_path="<<FLAGS_data_path<<endl;
    cout<<"config_path="<<FLAGS_config_path<<endl;
    cout<<"is_check="<<FLAGS_is_check<<endl;

    return 0;
}

构建命令:

g++ -Wall -g main.cpp -o main -I/usr/local/include/gflags -lgflags -lpthread

运行效果:

                

               

更详细的资料:

1、github:https://github.com/gflags/gflags

2、官方文档:https://gflags.github.io/gflags/

猜你喜欢

转载自blog.csdn.net/lianshaohua/article/details/108646664
今日推荐