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

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

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

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

一个小例子:

 

  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 ).

运行结果输出如下:

  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  

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值