c++gflags

How to set a default value to flagfile in gflags?


I set flagfile like this:

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

then change flagfile by command line

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

but flagfile is still ./conf/default.conf

How to set flagfile's default value and also accept changes by command line?


Solution

  • You can simply check the parameters yourself before calling the ParseCommandLineFlags function.

    For example something like:

    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];
    }
    

    That way you will use the other configuration file. You can change the regex to match differently depending on what you need.