glib 的命令行选项解析顺序是否敏感?在下面的代码中,我在数组--foo
之前定义了选项。解析将两者都设置为真,但仅设置为真。我如何让它忽略顺序,因为无序选项是 *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