6 static int opt_verbose = 0;
7 static char *opt_greeting = "Hello";
8 static char *opt_name = "World";
11 static char *short_options = "hvg:n:";
13 static struct option long_options[] = {
14 {"help", 0, NULL, 'h'},
15 {"verbose", 0, NULL, 'v'},
16 {"greeting", 1, NULL, 'g'},
17 {"name", 1, NULL, 'n'},
20 /* Show usage information */
21 static void usage(int status)
23 printf("opts -[OPTIONS]\n");
26 printf(" -h,--help Show usage information\n");
27 printf(" -v,--verbose Print debug output messages\n");
28 printf(" -n,--greeting=STR Use the given greeting\n");
29 printf(" -n,--name=NAME Greet the given person\n");
33 /* Parse command line arguments */
34 static void parse(int argc, char **argv)
37 int c = getopt_long(argc, argv,
38 short_options, long_options, 0);
42 case 'h': usage(0); break;
43 case 'v': opt_verbose = 1; break;
44 case 'g': opt_greeting = optarg; break;
45 case 'n': opt_name = optarg; break;
46 default: usage(1); break;
54 printf(" verbose = %d\n", opt_verbose);
55 printf(" greeting = %s\n", opt_greeting);
56 printf(" name = %s\n", opt_name);
61 /* Main entry point */
62 int main(int argc, char **argv)
65 printf("%s, %s\n", opt_greeting, opt_name);