glib命令行解析是否对命令敏感?

时间:2014-01-16 01:56:24

标签: c command-line-arguments glib fastq

glib的命令行选项是否解析顺序敏感?在下面的代码中,我在--foo数组中的--bar之前定义选项GOptionEntry。解析--foo --bar将两者都设置为true,但只将--bar --foo设置为foo为true。如何使它无视顺序,因为无序选项是* nix afaik中的常态。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <glib.h>

static bool foo = false;
static bool bar = false;

static GOptionEntry entries[] =
{
  { "foo" , 0 , 0 , G_OPTION_ARG_NONE , &foo , "foo" , NULL } ,
  { "bar" , 0 , 0 , G_OPTION_ARG_NONE , &bar , "bar" , NULL } ,
  { NULL }
};

int main(int argc, char * argv[]) {
    GError * error = NULL;
    GOptionContext * context = g_option_context_new ("- convert fastq");
    g_option_context_add_main_entries (context, entries, NULL);

    if (!g_option_context_parse (context, &argc, &argv, &error)){
        exit(1);
    }

    printf("%s\n", foo ? "foo is true" : "foo is false");
    printf("%d\n", bar ? "bar is true" : "bar is false");
    return 0;
}

结果:

> ./test2 
foo is false
bar is false
> ./test2 --foo
foo is true
bar is false
> ./test2 --foo --bar
foo is true
bar is true
> ./test2 --bar
foo is false
bar is true
> ./test2 --bar --foo
foo is true
bar is false

1 个答案:

答案 0 :(得分:3)

arg_data结构中的GOptionEntry指针应指向gboolean,而不是boolgboolean的大小与gint的大小相同,可能大于bool。在上一次测试中,settimg foo可能会覆盖bar