Command Line Parameter Parsing instance

Source: Internet
Author: User

In the previous article, I have introduced the parsing of command line parameters. I am quite familiar with the use of getopt and getopt_long. Today I will use a glib interface: g_spawn_command_line_sync () A synchronization sub-process is generated through the pipeline, and then parsed through getopt_long to execute the corresponding program.

1. Write the main. C of getopt_long.

  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. Return 0;
  80. }

3. compile and generate an execution file: preview_test. The following function will call this execution file to create another process and execute its program.

4. Open another terminal and write a debugging function 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 character line [256] = {0 };
  7. Char * file_path = strdup ("/home/wutangzhi/project/getopt_long_project /");

  8. Sprintf (using line, "% spreview_test -- level 5 -- Theme default -- sound true -- progressbar true", file_path );
  9. G_printf ("file_path = % s/n", using line );

  10. If (! G_spawn_command_line_sync (using line, null ))
  11. {
  12. G_printf ("[% s]: preview_test couldn't be execute! /N ", _ FUNC __);

  13. Return-1;
  14. }

  15. Return 0;
  16. }

Debugging result:

  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.

The use of getopt_long should be better understood from this instance, and g_spawn_command_line_s should also be understood.
The simple usage of YNC. Glib encapsulates it well. When appropriate, you can use them to achieve good results.

~ End ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.