Contents (click to collapse) >>

gflags' How-To and Tips

  1. define flags in header files or cpp files which invoke the flags

    define flags with initial values

     DEFINE_bool(debug_mode, true, "if debug mode enabled");
     DEFINE_int32(port, 8899, "TCP port for server");
     DEFINE_int32(libbp_log_level, 4, "explanation");
or, just define them

  1. in flag file, there should not be any spaces between flag and value, or the flag my be null

    correct, FLAGS_my_flag will be a b c or "abc", pay attion to the spaces

     -my_flag=a b c

    <font color=#ff0000>wrong</font>, FLAGS_my_flag will be empty (nothing)

     -my_flag = abcded
  2. define flags with file or command line
    1. with default flagfile

      the following code must be invoked in the your code fisrt

       google::SetCommandLineOption("flagfile", "default_flag_file_path")
    2. with specific flagfile

       your_prog_with_gflags -flagfile 'flagfile_path'
    3. with command line

       your_prog_with_gflags -my_flag1 value1 -my_flag2 value2
  3. parse sequence, set default flagfile first, then parse the command line

    flagfile is the reserved word of gflags, which defines which file to find the flags

     if (is_file_existed("list.conf") {
         google::SetCommandLineOption("flagfile", "list.conf");
     google::ParseCommandLineFlags(&argc, &argv, true);

    the above code will do the the following:

    1. try to assign the flags by reading default flagfile list.conf
    2. try to assign the flags by parsing the command line, values previously defined in flagfile will be override

    Note that the flagfile can be define again in command line, which will make gflags to read the specific flagfile, flag defined in flagfile can always be override by command line

  4. enjoy!

  5. references

comments powered by Disqus

braft call graph

This is the call graph of [braft](,an industrial grade raft implementation, which may help you understand b...… Continue reading

Clock And Timestamp

Published on August 16, 2019

memory ordering

Published on April 30, 2019