Use of gflags

Source: Internet
Author: User

Today,Mayuyu to introduce to you a very useful tool called gflags. gflags is a set of Google open-source command-line parameter resolution tool, more powerful than the getopt () function, more convenient to use,gflags It also supports reading parameters from environment variables and configuration files. There are currently C + + and Python versions. This article is to introduce the use of C + + version gflags , the main points are as follows two parts

Contents

1. Installation of gflags

2. Introduction to the use of gflags

1. Installation of gflags

We already have a general understanding of gflags , and then we start installing gflags in a Linux environment. First download the following installation package

Links:https://github.com/schuhschuh/gflags/releases

Online Help documentation:http://gflags.googlecode.com/svn/trunk/doc/gflags.html

The Mayuyu version is gflags-1.0rc2.tar.gzand then decompressed to perform the following 3 steps

(1)./configure

(2) Make

(3) Make install

Introduction to two more important documents

Doc : GFlags's Help file, which describes some of the ways to use gflags.

src : gflags code source file.

in order for the program to load the libgflags.so file at run time, you also need to create a soft link in the /usr/lib directory, as follows

The gflags is now completely installed, so you can use it safely.

2. Introduction to the use of gflags

(1) Defining parameters

use GFlags to include header file # include <gflags/gflags.h>. The parameter types supported by GFlags are

Define_bool:boolean
Define_int32:32-bit integer
Define_int64:64-bit integer
define_uint64:unsigned 64-bit Integer
Define_double:double
Define_string:c++ string

defined in the following way

That is Define_type , the three parameters of the macro represent the command line parameter name, parameter default value, parameter Help information.

GFlags does not support lists, but can be implemented with string types, such as

(2) Parameter access

When the parameters are defined, they can be accessed through flags_name , such as

Note that by DEFINE the parameters defined, to ensure that the file that accesses the variable and the file that defines the parameter are the same file or the header file

Contains a relationship, otherwise the parameters that are not defined will be accessed. The parameters defined in other files are implemented using DECLARE .

(3) Parameter check

After the parameter is defined, you can register a check function for the parameter when you specify the parameter from the command line or by Setcommandlineoption ()

parameter, the check function is called, two parameters are command-line parameter names, and the parameter values are set.

#include <iostream> #include <string.h> #include <string> #include <stdio.h> #include < Gflags/gflags.h>using namespace std;using namespace google;static bool Validateport (const char *flagname, int32_t Val {if (Val > 0 && val < 32768) return True;return false;} Define_int32 (port, 6379, "What's the port?"); int main (int argc, char **argv) {    parsecommandlineflags (&ARGC, &ARGV, true);    BOOL Isport = Registerflagvalidator (&flags_port, &validateport);  return 0;}

It is recommended that you register the check function immediately after defining the parameters. Registerflagvalidator () returns True when checking for a successful function registration;

Returns False if the parameter has already registered the check function, or if the function type does not match.

(4) Initialization parameters

Implemented by calling Parsecommandlineflags (&ARGC, &ARGV, True) , the last parameter if the True,gflags

The parse argument is removed, and if False it is preserved. The parameter order may be adjusted.

(5) Specifying parameters on the command line

can be specified by:--Parameter name = argument value . For example, the following code

#include <iostream> #include <string.h> #include <string> #include <stdio.h> #include < Gflags/gflags.h>using namespace std;using namespace google;static bool Validateport (const char *flagname, int32_t Val {if (Val > 0 && val < 32768) return True;return false;} Define_int32 (port, 6379, "What's the port?");D Efine_string (IP, "127.0.0.1", "My IP?"); int main (int argc, char **argv) {    parsecommandlineflags (&ARGC, &ARGV, true);    BOOL Isport = Registerflagvalidator (&flags_port, &validateport);  int32_t port = flags_port;string ip = flags_ip;cout << Port << endl;cout << IP << endl;return 0;}


parameters can be specified in the following way, and there can be no order between the parameters.

(5) Some special parameters to understand

--help

Print Help for all parameters that have been defined


--version

Print version information 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. Pass

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

Reading the parameter value from a file,--flagfile=my.conf indicates the value of the parameter to read from the my.conf file. Specify the parameter in the configuration file

The values are similar to those in the command line, and additional files can be included in the Flagfile by--flagfile.

Use of gflags

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.