标签 gflags 下的文章

“”

使用GetCommandLineFlagInfo即可例如判断portno是否设置:

#include "bits/stdc++.h"
#include <gflags/gflags.h>
#include "flags.h"

using namespace std;

DEFINE_int32(portno, 0, "portno,默认为0");

int main(int argc, char* argv[])
{
    google::SetVersionString("V1.0.0@const.net.cn");
    google::SetUsageMessage("-help ");
    google::ParseCommandLineFlags(&argc, &argv, true);

    google::CommandLineFlagInfo info;
    if (GetCommandLineFlagInfo("portno", &info) && info.is_default) 
    {
        std::cout << "portno is not set." << std::endl;
    } 
    else 
    {
        std::cout << "portno is set." << FLAGS_portno <<std::endl;
    }


    std::cout << FLAGS_string_var<< std::endl;
    std::cout << FLAGS_int32_var<< std::endl;
    std::cout << FLAGS_bool_var<< std::endl;
    return 0;
}

./test_gflags -portno=11
portno is set.11

0
1

extern void GetAllFlags(std::vector<CommandLineFlagInfo>* OUTPUT)接口

示例代码:

#include "bits/stdc++.h"
#include <gflags/gflags.h>
#include "flags.h"

using namespace std;

DEFINE_int32(portno, 0, "portno,默认为0");

int main(int argc, char* argv[])
{
    google::SetVersionString("V1.0.0@const.net.cn");
    google::SetUsageMessage("-help ");
    google::ParseCommandLineFlags(&argc, &argv, true);

    std::vector<google::CommandLineFlagInfo> OUTPUT;
    GetAllFlags(&OUTPUT);
    for(auto i=0;i<OUTPUT.size();i++)
    {
        cout<<OUTPUT[i].name<<"="<<OUTPUT[i].current_value<<endl;
    }
    return 0;
}

 ./test_gflags -portno=11

flagfile=
fromenv=
tryfromenv=
undefok=
tab_completion_columns=80
tab_completion_word=
help=false
helpfull=false
helpmatch=
helpon=
helppackage=false
helpshort=false
helpxml=false
version=false
bool_var=true
double_var=0
int32_var=0
int64_var=0
string_var=
uint32_var=0
uint64_var=0
portno=11