用glib标准化程序的命令行解析 (option parser ) 使用GOptionEntry

来源:互联网 发布:js array的方法和属性 编辑:程序博客网 时间:2024/04/29 16:05

转自:http://blog.csdn.net/yunhappy/archive/2010/03/08/5356945.aspx

 

以前写程序时 遇到需要接收参数时,总是自己printf,自己去parse。总是感觉很机械,繁琐,不过主要也都是服务端程序,一般不需要提供给其他人用。

不过,哈哈 最近发现了一个好东东,GOptionEntry:http://library.gnome.org/devel/glib/stable/glib-Commandline-option-parser.html

一个小例子:

 

view plaincopy to clipboardprint?
  1. static gint repeats = 2;  
  2. static gint max_size = 8;  
  3. static gboolean verbose = FALSE;  
  4. static gboolean beep = FALSE;  
  5. static gboolean op_rand = FALSE;  
  6. static gchar arg_data[32] = {"arg data"};  
  7.   
  8. static GOptionEntry entries[] =  
  9. {  
  10.     {"long name"'s'/*short-name*/, 0/*flags*/, G_OPTION_ARG_STRING/*arg*/,  
  11.         arg_data,  
  12.         "description""arg_description"},  
  13.   
  14.     {"repeats"'r', 0, G_OPTION_ARG_INT,  
  15.         &repeats, "Average over N repetitions""N"},  
  16.   
  17.     {"max-size"'m', 0, G_OPTION_ARG_INT,  
  18.         &max_size, "Test up to 2^M items""M"},  
  19.   
  20.     {"verbose"'v', 0, G_OPTION_ARG_NONE,  
  21.         &verbose, "Be verbose", NULL},  
  22.   
  23.     {"beep"'b', 0, G_OPTION_ARG_NONE,  
  24.         &beep, "Beep when done", NULL},  
  25.   
  26.     {"rand", 0, 0, G_OPTION_ARG_NONE,  
  27.         &op_rand, "Randomize the data", NULL},  
  28.   
  29.     {NULL}  
  30. };  
  31.   
  32. int  
  33. main (int argc, char *argv[])  
  34. {  
  35.     GError *error = NULL;  
  36.     GOptionContext *context = NULL;  
  37.   
  38.     context = g_option_context_new("- test tree model performance");  
  39.     g_option_context_add_main_entries(context, entries, NULL);  
  40.     g_option_context_set_summary(context, "my-add-summary");  
  41. //  g_option_context_add_group(context, gtk_get_option_group(TRUE));  
  42.     if (!g_option_context_parse(context, &argc, &argv, &error)) {  
  43.         g_print("option parsing failed: %s/n", error->message);  
  44.         exit (1);  
  45.     }  
  46.   
  47.     g_option_context_free(context);  
  48.   
  49.     g_print("Now value is: repeats=%d, max_size=%d, /n",  
  50.             repeats, max_size);  
  51.   
  52.     return 0;  
  53. }  

丛上面的例子可以看出,程序的Commandline parse主要工作就变成了

GOptionEntry 的定义:

  1. typedef struct {  
  2.   const gchar *long_name;    // 参数的完整名  
  3.   gchar        short_name;   // 简写名  
  4.   gint         flags;        // 参数选项标准 不关心直接赋0  
  5.   
  6.   GOptionArg   arg;          // 参数类型,int,string...  
  7.   gpointer     arg_data;     // 默认参数值  
  8.     
  9.   const gchar *description;  // 参数意义说明  
  10.   const gchar *arg_description;  // 参数占位符说明  
  11. } GOptionEntry;  

同时还需要注意 不要引起内存泄露:

Please note that parsed arguments need to be freed separately (see GOptionEntry).

运行结果输出如下:

view plaincopy to clipboardprint?
  1. [yunm@yunmiao src]$ ./test --help  
  2. Usage:  
  3.   test [OPTION...] - test tree model performance  
  4.   
  5. my-add-summary  
  6.   
  7. Help Options:  
  8.   -h, --help                          Show help options  
  9.   
  10. Application Options:  
  11.   -s, --long name=arg_description     description  
  12.   -r, --repeats=N                     Average over N repetitions  
  13.   -m, --max-size=M                    Test up to 2^M items  
  14.   -v, --verbose                       Be verbose  
  15.   -b, --beep                          Beep when done  
  16.   --rand                              Randomize the data  
  17.   
  18. [yunm@yunmiao src]$ ./test -r 10 -m 2  
  19. Now value is: repeats=10, max_size=2,   
  20. [yunm@yunmiao src]$ ./test   
  21. Now value is: repeats=2, max_size=8,   
  22. [yunm@yunmiao src]$ ./test -r 111111111111111111111  
  23. option parsing failed: Integer value '111111111111111111111' for -r out of range  

这样一来,又大大加进了我们程序的标准化进程。 ^_^

原创粉丝点击