-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetopt_args.c
41 lines (38 loc) · 1018 Bytes
/
getopt_args.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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
int main(int argc, char *argv[])
{
int ret;
char *filename = NULL;
int number_a = 0;
int number_b = 0;
int add = 0;
int usage = 0;
while ((ret = getopt(argc, argv, "f:a:b:Ah")) != -1) {
switch (ret) {
case 'a':
number_a = atoi(optarg);
break;
case 'b':
number_b = atoi(optarg);
break;
case 'f':
filename = optarg;
break;
case 'A':
add = 1;
break;
case 'h':
default:
fprintf(stderr, "<%s> -a <number_a> -b <number_b> -A [add] -f <filename> -h [help]\n", argv[0]);
return -1;
}
}
if (add) {
fprintf(stderr, "add [%d + %d = %d]\n", number_a, number_b, number_a + number_b);
}
if (filename) {
fprintf(stderr, "filename is given in command line [%s]\n", filename);
}
}