现在的位置: 首页 > 综合 > 正文

命令行参数解析实例

2012年12月07日 ⁄ 综合 ⁄ 共 3115字 ⁄ 字号 评论关闭

在上篇文章中,已经介绍了关于命令行参数解析,对于getopt和getopt_long的使用是相当熟悉了,今天将是通过一个glib的一个接口:g_spawn_command_line_sync()通过管道产生一个同步运行的子进程,然后通过getopt_long去解析,执行相应的程序。

1、写一个getopt_long 的main.c

  1. #include <getopt.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <gtk/gtk.h>
  5. static void display_usage(const char* program, FILE* fp, int exit_code)
  6. {
  7.     fprintf(fp, "usage: %s --level [0-9] --thme [theme name] [options]/n", program);
  8.     fprintf(fp, "-l --level [init-level]/n");
  9.     fprintf(fp, "-t --theme [theme-name]/n");
  10.     fprintf(fp, "-s --sound [true/false]/n");
  11.     fprintf(fp, "-p --progressbar[true/false]/n");
  12.     fprintf(fp, "-h --help /n");

  13.     exit(exit_code);
  14. }
  15. int main(int argc, char** argv)
  16. {
  17.     gint opt = 0;
  18.     int run_level = 0;
  19.     char* theme = NULL;
  20.     gboolean sound = FALSE;
  21.     gboolean progressbar = FALSE;

  22.     const struct option options[]=
  23.    {
  24.         {"level", 1, NULL, 'l'},
  25.         {"theme", 1, NULL, 't'},
  26.         {"sound", 1, NULL, 's'},
  27.         {"progressbar", 1, NULL, 'p'},
  28.         {"help", 0, NULL, 'h'},
  29.         {NULL, 0, NULL, 0}
  30.     };

  31.     optind = 0;

  32.     while((opt = getopt_long(argc, argv, "", options, NULL)) != -1)
  33.     {
  34.         switch(opt)
  35.         {
  36.             case 'l':
  37.             {
  38.                 run_level = atoi (optarg);
  39.                 if( run_level < 0 || run_level > 9)
  40.                 {
  41.                     display_usage(argv[0], stdout, 0);
  42.                 }
  43.             break;
  44.             }
  45.             case 't':
  46.             {
  47.                 theme = optarg;
  48.                 break;
  49.             }
  50.             case 's':
  51.             {
  52.                 if(!strcmp(optarg, "true"))
  53.                 {
  54.                     sound = TRUE;
  55.                 }
  56.                 break;
  57.             }
  58.             case 'p':
  59.             {
  60.                 if(!strcmp(optarg, "true"))
  61.                {
  62.                     progressbar = TRUE;
  63.                 }
  64.                 break;
  65.             }
  66.             case 'h':
  67.             {
  68.                 display_usage(argv[0], stdout, 0);
  69.                 break;
  70.             }
  71.          default:
  72.             {
  73.                 display_usage(argv[0], stderr, 1);
  74.                 break;
  75.             }
  76.         }
  77.     }
  78.     printf("level = %d, theme = %s, sound = %d, progressbar = %d./n", run_level, theme, sound, progressbar);
  79.     
  80. return 0;
  81. }

3、通过编译产生一个执行文件:preview_test.下面的函数将会通过调用这个执行文件,创建另一个进程,执行它的程序。

4、打开另外一个终端,写一个调试函数main.c

  1. #include <gtk/gtk.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(int argc, char** argv)
  5. {
  6.     char cmdline[256] = {0};
  7.     char* file_path = strdup("/home/wutangzhi/project/getopt_long_project/");

  8.     sprintf(cmdline, "%spreview_test --level 5 --theme default --sound true --progressbar true", file_path);
  9.     g_printf("file_path = %s/n", cmdline);

  10.     if(!g_spawn_command_line_sync(cmdline, NULL, NULL, NULL, NULL))
  11.     {
  12.         g_printf("[ %s ]: preview_test couldn't be execute!/n", __func__);

  13.         return -1;
  14.     }

  15.     return 0;
  16. }

调试结果:

  1. file_path = /home/wutangzhi/project/getopt_long_project/preview_test --level 5 --theme default --sound true --progressbar true
  2. level = 5, theme = default, sound = 1, progressbar = 1.

从这个实例应该更加明白了getopt_long的使用,其中也领会了g_spawn_command_line_s
ync()的简单用法。glib把它封装得很好。适当的时候使用它们,会收到相当好的效果的。

~END~

抱歉!评论已关闭.