c++: command line arguments

Google's open source C++ library gflags (also known as Google Flags) is a library for handling command-line parameters (flags), which aims to help developers easily define, parse, and manage command-line parameters of programs. The gflags library provides a convenient way to handle program configuration options, making program configuration more flexible and manageable.

Here are some key functions and descriptions of the gflags library:

  1. Flags Definition : The gflags library allows developers to use simple macros to define the program's command-line arguments. These macros can be used to declare flags of various types such as integers, floats, strings, etc.

  2. Command-Line Parsing : The gflags library can parse command-line arguments and associate them with corresponding flag values. Developers only need to call the function in the program ParseCommandLineFlags, and gflags will automatically parse the command line parameters and set the value of the flag.

  3. Handling Flag Values : Once the command-line arguments have been parsed, developers can FLAGS_flagnameaccess flag values ​​by accessing global variables. In this way, other parts of the program can use these flag values ​​for logic control and configuration.

  4. Default Values ​​and Documentation : The gflags library allows developers to set default values ​​and documentation for each flag. This information can help users understand the purpose and optional values ​​of the flag.

  5. Environment Variable Support (Environment Variable Support) : In addition to command line parameters, gflags also supports setting flag values ​​through environment variables, which provides more flexibility for program configuration.

  6. Support for Various Data Types (Support for Various Data Types) : gflags supports multiple data types, including integers, floating-point numbers, strings, Boolean values, etc., to meet the needs of different types of flags.

  7. Multi-thread safety (Thread Safety) : The gflags library provides a multi-thread safe flag processing mechanism to ensure that it is also reliable when used in a multi-threaded environment.

  8. Command-Line Help Messages (Command-Line Help Messages) : The gflags library can automatically generate command-line help messages so that users can understand the available command-line options and flags when using programs.

Overall, the gflags library makes managing configuration options for programs easier and more consistent. It is widely used in Google and many other development environments to help improve development efficiency and code maintainability.

#include <iostream>
#include <gflags/gflags.h>

DEFINE_string(name, "John", "Your name");
DEFINE_int32(age, 25, "Your age");
DEFINE_bool(is_student, false, "Are you a student?");
DEFINE_double(height, 170.5, "Your height in centimeters");

int main(int argc, char* argv[]) {
    // 解析命令行参数
    gflags::ParseCommandLineFlags(&argc, &argv, true);
    
    // 访问标志值
    std::cout << "Hello, " << FLAGS_name << "!" << std::endl;
    std::cout << "You are " << FLAGS_age << " years old." << std::endl;
    if (FLAGS_is_student) {
        std::cout << "You are a student." << std::endl;
    } else {
        std::cout << "You are not a student." << std::endl;
    }
    std::cout << "Your height is " << FLAGS_height << " cm." << std::endl;

    return 0;
}

In this example, we define four command line flags using the gflags library: name, age, is_student, height. The command-line arguments are then parsed by gflags::ParseCommandLineFlagsthe function to obtain the values ​​of these flags and used in the program for output.

When running the sample program, you can set the value of the flag through the command line parameters. For example:

./my_program --name=Alice --age=30 --is_student --height=160.0

This will set the value of the flag to the corresponding parameter value, and the program will output the corresponding information. Remember to make sure the gflags library is properly configured and installed before running the program.

Guess you like

Origin blog.csdn.net/qq_36541069/article/details/132143810