zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux glib命令行解析GOptionEntry使用

Linux命令行 解析 使用
2023-09-14 09:09:56 时间
1.安装glib
//安装依赖库
sudo apt-get install libffi-dev -y

//安装glib
# sudo apt-get install libglib2.0-dev -y

//64位ubuntu安装32位glib2.0, 仅作参考
# sudo apt-get install libglib2.0-dev:i386

2.test.c
#include <glib.h>
#include <locale.h>
#define ERR_MSG_V(msg, ...) \
    g_print("** ERROR: <%s:%d>: " msg "\n", __func__, __LINE__, ##__VA_ARGS__)

static  gint repeats = 2;
static  gint max_size = 8;
static  gboolean verbose = FALSE;
static  gboolean beep = FALSE;
static  gboolean op_rand = FALSE;
static  gchar arg_data[32] = { "arg data" };

static  GOptionEntry entries[] =
{
    {"long name" ,  's' /*short-name*/ , 0 /*flags*/ , G_OPTION_ARG_STRING /*arg*/ ,
        arg_data, "description" ,  "arg_description" },
    {"repeats" ,  'r' , 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions" ,  "N" },
    {"max-size" ,  'm' , 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items" ,  "M" },
    {"verbose" ,  'v' , 0, G_OPTION_ARG_NONE, &verbose, "Be verbose" , NULL},
    {"beep" ,  'b' , 0, G_OPTION_ARG_NONE, &beep, "Beep when done" , NULL},
    {"rand" , 0, 0, G_OPTION_ARG_NONE, &op_rand, "Randomize the data" , NULL},
    {NULL}
};


int main (int  argc,  char  *argv[])
{
    GError *error = NULL;
    GOptionContext *context = NULL;
    GOptionGroup *group = NULL;

    // 创建一个新的选项上下文
    context = g_option_context_new("- test tree model performance" );
#if 1
    // 如果主要组不存在则创建主要组,向组添加entries并设置转换域
    g_option_context_add_main_entries(context, entries, NULL);

    //添加要在选项列表之前的--help输出中显示的字符串。 这通常是程序功能的摘要
    g_option_context_set_summary(context, "This is a glib demo" );
#else
    group = g_option_group_new ("abc", NULL, NULL, NULL, NULL);
    g_option_group_add_entries (group, entries);

    g_option_context_set_main_group (context, group);
    //  g_option_context_add_group(context, gtk_get_option_group(TRUE));
#endif

    // 解析命令行参数,识别已添加到上下文的选项
    if  (!g_option_context_parse(context, &argc, &argv, &error))
    {
        ERR_MSG_V("%s", error->message);
        exit (1);
    }

    // 释放被解析的参数
    g_option_context_free(context);

    g_print("Now value is: repeats=%d, max_size=%d, verbose=%d, beep=%d\n",
            repeats, max_size, verbose, beep);

    return  0;
}

3.编译
# gcc test.c -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
-L/usr/lib/x86_64-linux-gnu -lglib-2.0 -o test

4.run测试
# ./test -h
Usage:
  test [OPTION?] - test tree model performance

This is a glib demo

Help Options:
  -h, --help                          Show help options

Application Options:
  -s, --long name=arg_description     description
  -r, --repeats=N                     Average over N repetitions
  -m, --max-size=M                    Test up to 2^M items
  -v, --verbose                       Be verbose
  -b, --beep                          Beep when done
  --rand                              Randomize the data

# ./test -r 66 -m 6
Now value is: repeats=66, max_size=6, verbose=0, beep=0

5.GOptionEntry结构体定义:
typedef struct GOptionEntry {
  const gchar *long_name;		// 参数的完整名 
  gchar        short_name;		// 简写名   
  gint         flags;			// 参数选项标准,如果不关心可直接赋0 

  GOptionArg   arg;				// 参数类型,int,string... 
  gpointer     arg_data;		// 默 认参数值  
  
  const gchar *description;		// 参数意义说明  
  const gchar *arg_description;	// 参数占位符说明   
}GOptionEntry;

glib命令行解参考