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