【问题标题】:How to set a default value to flagfile in gflags?如何在 gflags 中为 flagfile 设置默认值?
【发布时间】:2015-06-19 02:51:33
【问题描述】:

我这样设置标志文件:

DECLARE_string(flagfile);
int main(int argc, char** argv) {
  FLAGS_flagfile = "./conf/default.conf"
  ParseCommandLineFlags(&argc, &argv, true);
  ....
}

然后通过命令行更改标志文件

./main --flagfile=./conf/another.conf

但 flagfile 仍然是 ./conf/default.conf

如何设置flagfile的默认值并接受命令行修改?

【问题讨论】:

    标签: c++ gflags


    【解决方案1】:

    在调用ParseCommandLineFlags函数之前,您可以自己检查参数。

    例如:

    std::regex flag_regex("--flagfile=(.+.conf)")
    std::smatch reg_match;
    if(std::regex_match(std::string(argv[1]), reg_match, flag_regex){
       FLAGS_flagfile = reg_match[0];
    }
    

    这样您将使用其他配置文件。您可以根据需要更改正则表达式以进行不同的匹配。

    【讨论】:

      【解决方案2】:

      使用

      google::SetCommandLineOption("flagfile", "gflags_sample.flags");
      

      【讨论】:

        猜你喜欢
        • 2011-08-31
        • 2020-12-14
        • 2012-05-02
        • 2017-12-26
        • 1970-01-01
        • 2021-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多