glib命令行解析库简单使用--GOptionEntry 命令行参数

转自:http://blog.csdn.net/ciahi/archive/2010/12/15/6076786.aspx

官网文档为:

http://library.gnome.org/devel/glib/stable/glib-Commandline-option-parser.html


简单来说,就是定义GOptionEntry结构,这个结构里面包含了命令项名字、类型以及简单介绍

然后创建GOptionContext,把定义的GOptionEntry结构放到GOptionContext中,调用g_option_context_parse就可以将命令选项都解出来


默认情况下,-h和--help可以查看程序的帮助,这个帮助信息是使用的GOptionEntry中定义的信息,还有一些辅助函数用来添加一些其它信息,或对这些信息的格式进行设置。


下面是一个简单的示例:


view plain copy to clipboard print ?
  1. static  gint repeats = 2;  
  2. static  gint max_size = 8;  
  3. static  gboolean verbose = FALSE;  
  4. static  gboolean beep = FALSE;  
  5. static  gboolean rand = FALSE;  
  6. static  gchar *string;  
  7. static  GOptionEntry entries[] =  
  8. {  
  9.   { "repeats" 'r' , 0, G_OPTION_ARG_INT, &repeats,  "Average over N repetitions" "N"  },  
  10.   { "max-size" 'm' , 0, G_OPTION_ARG_INT, &max_size,  "Test up to 2^M items" "M"  },  
  11.   { "verbose" 'v' , 0, G_OPTION_ARG_NONE, &verbose,  "Be verbose" , NULL },  
  12.   { "beep" 'b' , 0, G_OPTION_ARG_NONE, &beep,  "Beep when done" , NULL },  
  13.   { "rand" , 0, 0, G_OPTION_ARG_NONE, &rand,  "Randomize the data" , NULL },  
  14.   { "str_test" 's' , 0, G_OPTION_ARG_STRING, &string,  "test the stirng" , NULL},  
  15.   { NULL }  
  16. };  
  17. int   
  18. main (int  argc,  char  *argv[])  
  19. {  
  20.   GError *error = NULL;  
  21.   GOptionContext *context;  
  22.   context = g_option_context_new ("- test tree model performance" );  
  23.   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);  
  24.   // g_option_context_add_group (context, gtk_get_option_group (TRUE));   
  25.   if  (!g_option_context_parse (context, &argc, &argv, &error))  
  26.     {  
  27.       g_print ("option parsing failed: %s/n" , error->message);  
  28.       exit (1);  
  29.     }  
  30.   /* ... */   
  31. }  

static gint repeats = 2; static gint max_size = 8; static gboolean verbose = FALSE; static gboolean beep = FALSE; static gboolean rand = FALSE; static gchar *string; static GOptionEntry entries[] = { { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" }, { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" }, { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL }, { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL }, { "rand", 0, 0, G_OPTION_ARG_NONE, &rand, "Randomize the data", NULL }, { "str_test", 's', 0, G_OPTION_ARG_STRING, &string, "test the stirng", NULL}, { NULL } }; int main (int argc, char *argv[]) { GError *error = NULL; GOptionContext *context; context = g_option_context_new ("- test tree model performance"); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); // g_option_context_add_group (context, gtk_get_option_group (TRUE)); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_print ("option parsing failed: %s/n", error->message); exit (1); } /* ... */ }

 

GOptionEntry的结构定义为:

view plain copy to clipboard print ?
  1. typedef   struct  {  
  2.   const  gchar *long_name;   // 完整命令 如:--name   
  3.   gchar        short_name;    // 简写命令 如:-n   
  4.   gint         flags;           // GOptionFlags枚举的值   
  5.   GOptionArg   arg;    // GOptionArg枚举的值   
  6.   gpointer     arg_data; // 解析出来的数据,所要存储的位置   
  7.     
  8.   const  gchar *description;   // 参数描述,--help可以查看到   
  9.   const  gchar *arg_description;   
  10. } GOptionEntry;  

typedef struct { const gchar *long_name; // 完整命令 如:--name gchar short_name; // 简写命令 如:-n gint flags; // GOptionFlags枚举的值 GOptionArg arg; // GOptionArg枚举的值 gpointer arg_data; // 解析出来的数据,所要存储的位置 const gchar *description; // 参数描述,--help可以查看到 const gchar *arg_description; } GOptionEntry;

 

编译之后,可以这样执行程序

./a.out -r 10 -b -s test

-b后面不能跟参数,因为这个参数类型为:G_OPTION_ARG_NONE。存储它的变量是一个bool型的值,当有这个参数的值,这个bool值是TRUE,否则是FALSE。

这样当执行完g_option_context_parse函数之后,就会发现repeats、beep、string里面都有值了。这儿要注意的是,string这个参数,在使用完了,需要自己来释放,否则的话就会有内存泄露。

下面这个网址里面是一些不错的示例代码,比较详细的说明了这块的用法

https://dev.mobileread.com/svn/iliados/upstream/glib-2.6.6/tests/option-test.c

原来在glib的源代码里面就有详细用法

glib-2.26.0/glib/tests/option-context.c

https://dev.mobileread.com/svn/iliados/upstream/glib-2.6.6/tests/  这个下面的一些glib的用法,是2.6.6版本的glib源码附带的

你可能感兴趣的:(String,null,存储,performance,gtk,Parsing)