Tekkaman

导航

 

How To Use Google Flags

   1、Commandline flags are flags that users specify on the command line when they run an executable. In the command

     fgrep -l -f /var/tmp/foo johannes brahms

  -l and -f /var/tmp/foo are the two commandline flags. (johannes and brahms, which don't start with a dash, are commandline arguments.)

  Typically, an application lists what flags the user is allowed to pass in, and what arguments they take -- in this example, -l takes no argument, and -f takes a string (in particular, a filename) as an argument.

 2、Gflags, the commandline flags library used within Google, differs from other libraries, such as getopt(), in that flag definitions can be scattered around the source code, and not just listed in one place such as main(). In practice, this means that a single source-code file will define and use flags that are meaningful to that file. Any application that links in that file will get the flags, and the gflags library will automatically handle that flag appropriately. There's significant gain in flexibility, and ease of code reuse, due to this technique. However, there is a danger that two files will define the same flag, and then give an error when they're linked together.

 3、Note that there are no 'complex' types like lists, but is defined of type "string", not "list_of_string" or similar. This is by design. We'd rather use only simple types for the flags, and allow for complex, arbitrary parsing routines to parse them, than to try to put the logic inside the flags library proper.

 4、--help flag will show the defined flag and the help string of the flag.

 5、google::ParseCommandLineFlags(&argc, &argv, true); 

  The last argument is called "remove_flags". If true, then ParseCommandLineFlags removes the flags and their arguments from argv, and modifies argc appropriately. In this case, after the function call, argv will hold only commandline arguments, and not commandline flags.

 6、Note that flags do not have single-letter synonyms, like they do in the getopt library, nor do we allow "combining" flags behind a single dash, as in ls -la.

 7、Sometimes a flag is defined in a library, and you want to change its default value in one application but not others. It's simple to do this: just assign a new value to the flag in main(), before calling ParseCommandLineFlags():

    另外C++程序的启动顺序:static initialization time(编译期赋值初始化的全局数据) -> program start time -> global construction time(函数初始化的全局数据) -> main。

 注意事項:

 1. 不允许定义FLAGS_non##name的变量。

 2. 多个文件如果定义的flag名相同,则会冲突,产生编译error。

 3. 不支持缩写,也不支持组合如 -la。

 4. 没有复杂数据构造类型,没有列表.

 5. 使用ParseCommandLineFlags()解析flags,放在在main函数第一行.

 6. 赋值flag,你可以使用-,也可以使用--.赋值可以使用=或空格,如:

  

    更多请参考:http://gflags.googlecode.com/svn/trunk/doc/gflags.html

 
posted on 2013-09-03 10:42  Tekkaman  阅读(1665)  评论(0编辑  收藏  举报