-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetopt_experiment.cc
More file actions
53 lines (50 loc) · 976 Bytes
/
getopt_experiment.cc
File metadata and controls
53 lines (50 loc) · 976 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
I wrote this program to figure out how to use
getopt().
*/
#include<iostream>
#include<unistd.h>
#include<stdio.h>
int main(int argc, char * argv[])
{
// use this varible to save the return value
// form getopt().
int option = -1;
// set opterr to 0 ot prevent getopt() sending
// error msg to stderr.
opterr = 0;
while((option = getopt(argc, argv, ":tn:")) != -1)
{
switch(option)
{
case 'n':
printf("case n\n");
printf("N = %s\n", optarg);
break;
case 't':
printf("case t\n");
printf("optarg: %s\n", optarg);
printf("optopt: %c\n", optopt);
break;
case '?':
printf("case ?\n");
printf("optarg: %s\n", optarg);
printf("optopt: %c\n", optopt);
break;
default:
exit(1);
}
}
printf("argc: %d\n", argc);
printf("optind: %d\n", optind);
printf("argv:\n");
for(int i=0; i < argc; i++)
{
printf("%s", argv[i]);
if(i != argc-1)
printf("->");
else
printf("\n");
}
return 0;
}