背景

我们可以通过给可执行文件传输命令行参数来修改flag的默认值,只需要在main函数开头,调用如下函数:

gflags::ParseCommandLineFlags(&argc, &argv, true);
上述参数的最后一个参数是”remove_flag”,如果为true该函数将会从argv中移除flag并修改argc为相应的值,如此下来,argv只保留命令行参数,而不再有命令行标记

如果remove_flag为false,那么该函数将会重新排列argv。

代码示例

#include <gtest/gtest.h>
#include "libs/common/service_coprocessor.h"
#include "third-party/brpc/src/bvar/gflag.h"

....

DEFINE_uint64(block_id, 2, "block id to set member");
DEFINE_uint64(block_type, 1, "block id to set member");
DEFINE_int64(block_count, 1, "block id to set member");

DEFINE_string(manage_service_server_ip, "10.208.12.5", "host ip of server machine");
DECLARE_int32(manage_service_port);

....
int main(int argc, char **argv) {
    LOGGER.set_level(LL_DEBUG);
    LOGGER.set_flush_level(LL_DEBUG);
    LOGGER.SetLogPrefix("./log", "test_manage_client");
    ::testing::InitGoogleTest(&argc, argv);
    gflags::ParseCommandLineFlags(&argc, &argv, true);
        //google::SetCommandLineOption("flagfile", "./test_manage_client.conf");
    int ret = RUN_ALL_TESTS();
    return ret;
}

用法示例

client-test --gtest_filter=TestDSManageClient.CreateBlock --manage_service_server_ip=$blocktype0.177.16.65 --manage_service_port=9016 --block_id=$blocktype000 --block_type=$blocktype --block_count=250