Pine Blog

GLIB用户指南-命令行解析

1.概述

传统的命令行解析,如果提供的参数很多,程序很不灵活,并且很容易出问题:
glib

while(getopt())
{
swtich() { 
             case:
             case:
             case:
         }
}

比如实现下面这个参数,实现起来还是非常麻烦的。

[root@localhost glib_test]# ./glib_test -?
Usage:
  glib_test [OPTION...]

Help Options:
  -?, --help              Show help options
  --help-all              Show all help options
  --help-test group       display help output

Application Options:
  -n, --nodaemon          Don't run as daemon in background
  -d, --debug             Enable debug information output
  -s, --size=M            Input your size

2.使用方法

glib提供了一套解析命令行的函数,程序实现如下:

#include <stdio.h>
#include <glib.h>
//#include <stdlib.h>
//#include <unistd.h>

static gboolean option_detach = FALSE;
static gboolean option_debug = FALSE;
static gint size = 8;
static GOptionEntry options[] = {
        { "nodaemon", 'n', 0,
                G_OPTION_ARG_NONE, &option_detach,
                "Don't run as daemon in background" },
        { "debug", 'd', 0,
                G_OPTION_ARG_NONE, &option_debug,
                "Enable debug information output" },
        { "size", 's', 0,
                G_OPTION_ARG_INT, &size,
                "Input your size","M" },
        { NULL },
};
int main(int argc, char *argv[])
{
        GOptionContext *context;
        GError *err = NULL;
        GOptionGroup *g_group;
        context = g_option_context_new(NULL);
        g_group = g_option_group_new("test group","test group display",
                "display help output", NULL, NULL);
        g_option_context_add_main_entries(context, options, NULL);
        g_option_context_add_group(context,g_group);
        if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
                if (err != NULL) {
                        g_printerr("%s\n", err->message);
                        g_error_free(err);
                } else
                        g_printerr("An unknown error occurred\n");
                exit(1);
       }
        g_option_context_free(context);
        if (option_debug == FALSE) {
                g_printf("option debug disable\n");
        } else {
                g_printf("option debug enable\n");
        }

        if (option_detach == FALSE) {
                g_printf("option detach disable\n");
        } else {
                g_printf("option detach enable\n");
        }
        printf("size=%d\n",size);
        return 0;
}

文章转载自:https://blog.csdn.net/l197803/article/details/138244885?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-2-138244885-blog-52932358.235%5Ev43%5Epc_blog_bottom_relevance_base2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-2-138244885-blog-52932358.235%5Ev43%5Epc_blog_bottom_relevance_base2&utm_relevant_index=5,如有问题请联系删除。

未经允许不得转载:Pine Blog » GLIB用户指南-命令行解析

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Pine Blog
Anywhere, Anytime
E-mail:59054872@qq.com
苏ICP备15059480号-1