gflag的簡單入門demo


gflags

一. 下載與安裝

這里直接使用包管理器安裝:

sudo apt install libgflags-dev

二. gflags的簡單使用

1. 定義需要的類型

格式: DEFINE_類型名(變量名, 默認值, 描述語)

類型 定義格式
bool類型 DEFINE_bool
32位的int類型 DEFINE_int32
64位的int類型 DEFINE_int64
64位的unsigned int 類型 DEFINE_uint64
double類型 DEFINE_double
string類型 DEFINE_string

例子:

  • 定義一個名為name的string類型的變量
DEFINE_string(name, "xiaoming", "this is the name who you love! ");
  • 定義一個名為uppercase的bool類型的變量
DEFINE_bool(uppercase, true, "Whether the output characters are in Uppercase format! ");
  • 定義一個名為count的int類型的變量
DEFINE_int32(count, 10, "Number of times the output is repeated! ");

2. 在命令行中給定義好的變量進行賦值操作:

--uppercase=true   或者   --uppercase
--uppercase=false  或者   --nouppercase
--name="china"     或者   --name "China"
--count=10         或者   --count 10

3. 在代碼中使用定義好的變量

在代碼中使用之前定義好的變量時,在每一個變量名前加FLAGS_的前綴就可以了.

定義的變量名 代碼中使用的變量名
name FLAGS_name
uppercase FLAGS_uppercase
count FLAGS_count

三. 小小的demo

1. demo.cpp文件的編寫

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

using namespace std;

// 定義三個變量
DEFINE_string(name, "xiaoming", "this is the name who you love! ");
DEFINE_bool(uppercase, true, "Whether the output characters are in Uppercase format! ");
DEFINE_int32(count, 10, "Number of times the output is repeated! ");

// 綁定對變量name和count的值合法性的檢測函數
static bool ValidateName(const char* flagname, const string& value);
DEFINE_validator(name, &ValidateName);
static bool ValidateCount(const char* flagname, int value);
DEFINE_validator(count, &ValidateCount);

/** @brief 主函數 */
int main(int argc, char* argv[])
{
    // 進行解析命令行參數,true表示會修改argc和argv的值, 把相應的命令行參數去除掉。
    gflags::ParseCommandLineFlags(&argc, &argv, true);

    if (FLAGS_uppercase)
    {
        for (int i = 0; i < FLAGS_count; ++i)
            cout << "I LOVE " << FLAGS_name << endl;
    }
    else
    {
        for (int i = 0; i < FLAGS_count; ++i)
            cout << "i love  " << FLAGS_name << endl;
    }

    return 0;
}

/** @brief  定義對變量name值合法性的檢測函數 */
static bool ValidateName(const char* flagname, const string& value)
{
    if (value.empty())
    {
        cout << "不應該輸入空的參數值(" << flagname << " 的值為空) " << endl << endl;
        return false;
    }
    return true;
}

/** @brief 定義對變量count值的合法性檢測函數。 */
static bool ValidateCount(const char* flagname, int value)
{
    if (value <= 0)
    {
        cout << "不應該輸入小於或等於0的參數值(" << flagname << " 的值<= 0) " << endl << endl;
        return false;
    }
    return true;
}

2. CMakeLists.txt文件的編寫

cmake_minimum_required(VERSION 3.10.2)
project(gflag_demo VERSION 1.0.0)
add_executable(demo.out demo.cpp)
find_package(gflags REQUIRED)
target_link_libraries(demo.out gflags)

3. 編譯

mkdir build
cd build
cmake ..
make

4. 運行

./demo.out --uppercase=false --name="China" --count=10

輸出為:

i love  China
i love  China
i love  China
i love  China
i love  China
i love  China
i love  China
i love  China
i love  China
i love  China


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM