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.
- # Include <getopt. h>
- # Include <stdlib. h>
- # Include <stdio. h>
- # Include <GTK/GTK. h>
- Static void display_usage (const char * program, file * FP, int exit_code)
- {
- Fprintf (FP, "Usage: % s -- level [0-9] -- thme [theme name] [Options]/n", Program );
- Fprintf (FP, "-l -- level [init-level]/n ");
- Fprintf (FP, "-t -- Theme [Theme-name]/n ");
- Fprintf (FP, "-s -- sound [true/false]/n ");
- Fprintf (FP, "-p -- progressbar [true/false]/n ");
- Fprintf (FP, "-h -- help/N ");
- Exit (exit_code );
- }
- Int main (INT argc, char ** argv)
- {
- Gint opt = 0;
- Int run_level = 0;
- Char * theme = NULL;
- Gboolean sound = false;
- Gboolean progressbar = false;
- Const struct option options [] =
- {
- {"Level", 1, null, 'L '},
- {"Theme", 1, null,'t '},
- {"Sound", 1, null,'s '},
- {"Progressbar", 1, null, 'P '},
- {"Help", 0, null, 'H '},
- {Null, 0, null, 0}
- };
- Optind = 0;
- While (OPT = getopt_long (argc, argv, "", options, null ))! =-1)
- {
- Switch (OPT)
- {
- Case 'l ':
- {
- Run_level = atoi (optarg );
- If (run_level <0 | run_level> 9)
- {
- Display_usage (argv [0], stdout, 0 );
- }
- Break;
- }
- Case 'T ':
- {
- Theme = optarg;
- Break;
- }
- Case's ':
- {
- If (! Strcmp (optarg, "true "))
- {
- Sound = true;
- }
- Break;
- }
- Case 'p ':
- {
- If (! Strcmp (optarg, "true "))
- {
- Progressbar = true;
- }
- Break;
- }
- Case 'H ':
- {
- Display_usage (argv [0], stdout, 0 );
- Break;
- }
- Default:
- {
- Display_usage (argv [0], stderr, 1 );
- Break;
- }
- }
- }
- Printf ("level = % d, theme = % s, sound = % d, progressbar = % d./N", run_level, theme, sound, progressbar );
- Return 0;
- }
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.
- # Include <GTK/GTK. h>
- # Include <stdio. h>
- # Include <string. h>
- Int main (INT argc, char ** argv)
- {
- Char character line [256] = {0 };
- Char * file_path = strdup ("/home/wutangzhi/project/getopt_long_project /");
- Sprintf (using line, "% spreview_test -- level 5 -- Theme default -- sound true -- progressbar true", file_path );
- G_printf ("file_path = % s/n", using line );
- If (! G_spawn_command_line_sync (using line, null ))
- {
- G_printf ("[% s]: preview_test couldn't be execute! /N ", _ FUNC __);
- Return-1;
- }
- Return 0;
- }
Debugging result:
- File_path =/home/wutangzhi/project/getopt_long_project/preview_test -- level 5 -- Theme default -- sound true -- progressbar true
- 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 ~