gflags.h的用法

2021-03-02  本文已影响0人  习惯了千姿百态
#include <iostream>
#include <gflags/gflags.h>

/**
 *  定义命令行参数变量
 *  默认的主机地址为 127.0.0.1,变量解释为 'the server host'
 *  默认的端口为 12306,变量解释为 'the server port'
 */
DEFINE_string(host, "127.0.0.1", "the server host");
DEFINE_int32(port, 12306, "the server port");

int main(int argc, char** argv) {
    // 解析命令行参数,一般都放在 main 函数中开始位置
    gflags::ParseCommandLineFlags(&argc, &argv, true);
    // 访问参数变量,加上 FLAGS_
    std::cout << "The server host is: " << FLAGS_host
        << ", the server port is: " << FLAGS_port << std::endl;
    return 0;
}
————————————————
版权声明:本文为CSDN博主「willinux」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/willinux20130812/article/details/87863938

命令行下使用:

g++ gflags_test.cc -o gflags_test -lgflags -lpthread
./gflags_test --host=10.123.78.90 --port=22

输出:

The server host is: 10.123.78.90, the server port is: 22

也可以将命令行参数写进flags.txt文件


flags.txt
./gflags_test --flagfile=flags.txt
上一篇 下一篇

猜你喜欢

热点阅读