1

glib のコマンド ライン オプションの解析順序は重要ですか? 以下のコードでは、配列の--foo前にオプションを定義しています。解析は両方を true に設定しますが、withのみを true に設定します。* nix afaikでは順序付けられていないオプションが標準であるため、順序を無視するにはどうすればよいですか。--barGOptionEntry--foo --bar--bar --foofoo

#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
4

1 に答える 1

4

構造体のarg_dataポインタは、ではなくを指す必要があります。Aは a と同じサイズで、おそらく a よりも大きいです。最後のテストでは、settimgはおそらく上書きします。GOptionEntrygbooleanboolgbooleangintboolfoobar

于 2014-01-16T02:42:11.080 に答える