-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetopt_long_demo.c
73 lines (67 loc) · 2.09 KB
/
getopt_long_demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int opt4;
while (1)
{
int this_optind = optind; // Index for all options, 1-based
int long_option_index = -1; // Index for long options only, 0-based?
int opt;
static struct option long_options[] = {
// flag controls how an option's argument is returned
// NULL return val
// int * fill with val or leave unchanged
//
// name, has_arg, flag, val
{"opt1", no_argument, 0, 0},
{"opt2", optional_argument, 0, 0},
{"opt3", required_argument, 0, 0},
{"opt4", optional_argument, 0, 4},
};
long_options[3].flag = &opt4;
// If not a long option, the index is unchanged?
opt = getopt_long(argc, argv, "ab:c", long_options, &long_option_index);
if (opt == -1)
break;
switch (opt)
{
case 0:
{
struct option *long_option = &long_options[long_option_index];
printf("Long option: %s", long_option->name);
if (optarg)
{
printf(" with arg %s", optarg);
}
printf("\n");
break;
}
case 'a':
printf("Option 'a'\n");
break;
case 'b':
printf("Option 'b': %s\n", optarg);
break;
case 'c':
printf("Option 'c'\n");
break;
case '?': // Unrecognized option
case ':': // Missing argument
default:
fprintf(stderr, "Usage: %s [-ac] [-b val] [ARG]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if (optind < argc)
{
printf("Positional arguments: ");
do
{
printf("%s ", argv[optind++]);
} while (optind < argc);
printf("\n");
}
exit(EXIT_SUCCESS);
}