-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetopt_example.c
More file actions
39 lines (36 loc) · 817 Bytes
/
getopt_example.c
File metadata and controls
39 lines (36 loc) · 817 Bytes
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
#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[]) {
int opt = 0;
char *in_fname = NULL;
char *out_fname = NULL;
while ((opt = getopt(argc, argv, "i:o:")) != -1) {
switch(opt) {
case 'i':
in_fname = optarg;
printf("\nInput option value=%s", in_fname);
break;
case 'o':
out_fname = optarg;
printf("\nOutput option value=%s", out_fname);
break;
case '?':
/* Case when user enters the command as
* $ ./cmd_exe -i
*/
if (optopt == 'i') {
printf("\nMissing mandatory input option");
/* Case when user enters the command as
* # ./cmd_exe -o
*/
} else if (optopt == 'o') {
printf("\nMissing mandatory output option");
} else {
printf("\nInvalid option received");
}
break;
}
}
printf("\n");
return 0;
}