-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetopt_test.c
55 lines (41 loc) · 842 Bytes
/
getopt_test.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
char* l_opt_test = NULL;
char* const short_options = "abcd:";
struct option long_opts[] =
{
{"atest", 0, NULL, 'a' },
{"btest", 0, NULL, 'b' },
{"ctest", 0, NULL, 'c' },
{ 0, 0, 0, 0 },
};
int parse_opt ( int argc, char* argv[] )
{
int c = 0;
while ( ( c = getopt_long ( argc, argv, short_options, long_opts, NULL ) ) != -1 )
{
switch ( c )
{
case 'a':
printf ( "This is atest\n" );
break;
case 'b':
printf ( "This is btest\n" );
break;
case 'c':
printf ( "This is ctest\n" );
break;
default:
printf ( "arg error!\n" );
return -1;
}
}
return 0;
}
int main ( int argc, char* argv[] )
{
parse_opt ( argc, argv );
return 0;
}