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

本文链接地址:https://const.net.cn/562.html

标签: gflags

添加新评论