Use the Google command line tool gflags

Source: Internet
Author: User

Google's open-source gflags is a set of Command Line Parameter Parsing tools, which are more powerful and easier to use than getopt, gflags also supports reading parameters from environment variables and configuration files (gflags can be used instead of configuration files ). This article briefly introduces the use of gflags, content mainly translated from http://gflags.googlecode.com/svn/trunk/doc/gflags.html.

 

Define parameters
Use flags to include the header file # include <gflags/gflags. h>
Gflags mainly supports the following parameter types: bool, int32, int64, uint64, double, string, and so on. The defined parameters are implemented using the define_type macro, as shown below, A bool and a string type parameter are defined respectively. The three parameters of the macro are the command line parameter names, default values, and help information of the parameters.

Define_bool (big_menu, true, "include 'advanced 'options in the menu listing ");
Define_string (ages, "English, French, German ",
"Comma-separated list of versions to offer in the 'lang 'menu ");
Gflag does not support lists. You can use string parameters flexibly. For example, the preceding ages parameter can be of the string type, but can be considered as a comma-separated parameter list.

 

Access Parameters
After a parameter is defined, the corresponding parameter can be accessed through flags_name. For example, the big_menu and ages parameters defined above can be accessed through flags_big_menu and flags_ages.

If (flags_languages.find ("English ")! = String: NPOs)
Handleenglish ();

The preceding access method allows flags_name to access parameters only when the Parameter definition and access are in the same file (or included in the header file). If you want to access parameters defined in other files, declare_type is required. For example, in Foo. define_string (color, "Red", "the color you want to use") in CC. If you need to use the color parameter in foo_test.cc, add declare_string (color, "Red", "the color you want to use ");

 

Parameter check
After defining a parameter, you can register a check function (validator) for the parameter. When you specify a parameter from the command line or use setcommandlineoption () to specify a parameter, the check function will be called, the two parameters are the command line parameter names and the Set parameter values.
Static bool validateport (const char * flagname, int32 value ){
If (value> 0 & amp; value <32768) // value is OK
Return true;
Printf ("invalid value for -- % s: % d \ n", flagname, (INT) value );
Return false;
}
Define_int32 (port, 0, "What port to listen on ");
Static const bool port_dummy = registerflagvalidator (& flags_port, & validateport );
We recommend that you register the check function immediately after defining the parameters. Registerflagvalidator () returns true when checking that the function is successfully registered. If the parameter has already been registered to check the function or the function type does not match, false is returned.

 

Initialization parameters
In the main () of the referenced program, use Google: parsecommandlineflags (& argc, & argv, true); to initialize the gflags parameter. The third parameter is remove_flag, if the value is true, gflags removes parse parameters. Otherwise, gflags retains these parameters, but may adjust the order of parameters. For example, "/bin/foo" "arg1" "-Q" "arg2" is changed to "/bin/foo", "-Q", "arg1 ", "arg2" for better understanding.

 

Specify parameters in the command line
For example, to specify the ages parameter value in the command line, you can use the following four methods: int32, int64, and other types similar to string.

  • App_containing_foo -- ages = "Chinese, Japan, Korean"
  • App_containing_foo-ages = "Chinese, Japan, Korean"
  • App_containing_foo -- ages "Chinese, Japan, Korean"
  • App_containing_foo-Ages "Chinese, Japan, Korean"

For the bool type, you can specify parameters in the following ways:

  • App_containing_foo -- big_menu
  • App_containing_foo -- nobig_menu
  • App_containing_foo -- big_menu = true
  • App_containing_foo -- big_menu = false

Special Parameters

  • -- Help print help information for all defined parameters
  • -- Version: The version information is specified by Google: setversionstring ().
  • -- Nodefok: If a parameter is not defined in the command line, it does not exit (error-exit)
  • -- Fromenv reads the parameter value from the environment variable -- fromenv = Foo. Bar indicates that the values of Foo and bar are to be read from the environment variable. Use the export flags_foo = xxx; export flags_bar = YYY program to read Foo. The Bar values are XXX and YYY respectively.
  • -- Tryfromenv is similar to -- fromenv. When the parameter is not defined in the environment variable, it is not exited (fatal-exit)
  • -- Flagfile reads the parameter value from the file. -- flagfile = My. conf indicates the value of the parameter to be read from the my. conf file. The parameter value specified in the configuration file is similar to the command line method. In addition, you can use -- flagfile to include other files in flagfile.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.