Google command line tools gflags use

Source: Internet
Author: User

Google Open Source gflags is a set of command-line parameter resolution tool, more powerful than getopt, more convenient to use, GFlags also supports reading parameters from environment variables, configuration files (available gflags instead of configuration files). This article briefly introduces the use of GFlags, the main content is translated from http://gflags.googlecode.com/svn/trunk/doc/gflags.html.

Defining parameters
Use flags to include header files #include <gflags/gflags.h>
GFlags main supported parameter types include Bool,int32, int64, UInt64, double, string, etc., define the parameters through the Define_type macro implementation, as shown below, respectively defined a bool and a string type of parameters, The three parameters of the macro are the command-line parameter name, the parameter default value, and the Help information for the parameter, respectively.

Define_bool (Big_menu, True, "Include ' advanced ' options in the menu listing");
Define_string (Languages, "English,french,german",
"Comma-separated list of languages to offer in the ' Lang ' Menu");
Gflag does not support lists, and the user is flexibly implemented with string parameters, such as the languages parameter above, which can be of type string but can be considered a comma-separated list of parameters.

Access Parameters
When the parameters are defined, the corresponding parameters can be accessed through flags_name, such as the big_menu of the above definition, languages two parameters can be accessed through Flags_big_menu, flags_languages.

if (Flags_languages.find ("中文版")! = String::npos)
Handleenglish ();

The above access method, only when the parameter definition and access in the same file (or through the header file contains), Flags_name to access parameters, if you want to access the parameters defined in other files, you need to use Declare_type. For example, in foo.cc define_string (color, "red", "the color you want to use"); This is if you need to apply color to this parameter in foo_test.cc, you need to add declare_string (color, "red", "the color you want to use");

Parameter check
After defining the parameters, you can register a check function (validator) with the parameters, and when you specify parameters from the command line or through Setcommandlineoption (), the check function is called, two parameters are command-line parameter names, and the parameter values are set.
static bool Validateport (const char* FlagName, Int32 value) {
if (value > 0 && 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);
It is recommended that you register the check function immediately after defining the parameters. Registerflagvalidator () returns True when checking for a function registration success, or FALSE if the parameter has already registered a check function, or if the function type does not match.

Initialize parameters
In main () of the referencing program via Google::P arsecommandlineflags (&ARGC, &ARGV, true); This completes the initial gflags parameter, where the third parameter is Remove_flag, and if True,gflags removes the parse argument, the gflags retains the parameters, but the parameter order may be adjusted. For example "/bin/foo" "Arg1" "-Q" "Arg2" will be adjusted to "/bin/foo", "-Q", "Arg1", "arg2", so that better understanding.

Specifying parameters on the command line
For example, to specify the value of the languages parameter on the command line, there are 4 ways, Int32, Int64, and so on, similar to string.

    • App_containing_foo--languages= "Chinese,japanese,korean"
    • App_containing_foo-languages= "Chinese,japanese,korean"
    • App_containing_foo--languages "Chinese,japanese,korean"
    • App_containing_foo-languages "Chinese,japanese,korean"

for bool types, parameters can be specified in several 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 to print Help for all parameters defined
      • --version print version information is specified by google::setversionstring ()
      • --nodefok but does not exit when there are no defined parameters in the command line (error-exit)
      • --FROMENV reading a parameter value from an environment variable--fromenv=foo,bar indicates that the value of the Foo,bar two parameter is to be read from the environment variable. Through export flags_foo=xxx; The Export FLAGS_BAR=YYY program can read to Foo,bar values of xxx,yyy respectively.
      • --tryfromenv, similar to--fromenv, does not exit when the parameter is not defined in the environment variable (fatal-exit)
      • --flagfile read the parameter value from the file,--flagfile=my.conf indicates the value of the parameter to read from the my.conf file. Specifying parameter values in the configuration file is similar to the command line, and further--flagfile can be included in the Flagfile to include other files.

Google command line tools gflags use

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.