-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
130 lines (118 loc) · 3.52 KB
/
test.c
File metadata and controls
130 lines (118 loc) · 3.52 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (C) 2003-2013 Jules Colding <jcolding@gmail.com>
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You can use, modify and redistribute it in any way you want.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "argopt.h"
int
main(int argc, char **argv)
{
/* current argv index */
int index = 0;
/* options with arguments */
int count;
int offset;
/* option flags */
int opt_rw = 0;
int opt_rb = 0;
/* get options */
while (1) {
int c;
char *parameter;
struct option_t options[] = {
{"rb", "-rb will help you rb-fy your source", NO_PARAM, NULL, 'r'},
{"rw", "-rw is great to foobalooze the bazee", NO_PARAM, &opt_rw, 1},
{"abc123", "-abc123 alphabetizes a lower set", NO_PARAM, NULL, 0},
{"abc456", "-abc456 alphabetizes a higher set", NO_PARAM, NULL, 0},
{"abc", NULL, NO_PARAM, NULL, 0},
{"count", "-count <PARAMETER> enumerates the parameter", NEED_PARAM, NULL, 'c'},
{"offset", "-offset [PARAMETER] set the offset for the optional paramater", OPTIONAL_PARAM, NULL, 'o'},
{0, 0, 0, 0}
};
c = argopt(argc,
argv,
options,
&index,
¶meter);
switch (c) {
case ARGOPT_OPTION_FOUND :
fprintf(stdout, "%d - Option found:\t\t%s (*flag is not NULL)\n", __LINE__, argv[index]);
break;
case ARGOPT_AMBIGIOUS_OPTION :
argopt_completions(stdout,
"Ambigious option found. Possible completions:",
++argv[index],
options);
break;
case ARGOPT_UNKNOWN_OPTION :
fprintf(stdout, "%d - Unknown option found:\t%s\n", __LINE__, argv[index]);
argopt_help(stdout,
"Unknown option found",
argv[0],
options);
break;
case ARGOPT_NOT_OPTION :
fprintf(stdout, "%d - Bad or malformed option found:\t%s\n", __LINE__, argv[index]);
argopt_help(stdout,
"Bad or malformed option found",
argv[0],
options);
break;
case ARGOPT_MISSING_PARAM :
fprintf(stdout, "%d - Option missing parameter:\t%s\n", __LINE__, argv[index]);
argopt_help(stdout,
"Option missing parameter",
argv[0],
options);
break;
case ARGOPT_DONE :
fprintf(stdout, "\nargv[] parsed.\n");
goto opt_done;
case 0 :
fprintf(stdout, "%d - Option found:\t\t%s\n", __LINE__, argv[index]);
break;
case 'c' :
count = strtol(parameter, NULL, 10);
fprintf(stdout, "%d - Count option found:\t%s\n", __LINE__, argv[index-1]);
fprintf(stdout, "%d - Count parameter is:\t\"%s\"\n", __LINE__, parameter);
break;
case 'o' :
if (parameter) {
fprintf(stdout, "%d - Offset option found:\t%s\n", __LINE__, argv[index-1]);
fprintf(stdout, "%d - Offset parameter is:\t\"%s\"\n", __LINE__, parameter);
offset = strtol(parameter, NULL, 16);
} else {
fprintf(stdout, "%d - Offset option found:\t%s\n", __LINE__, argv[index]);
}
break;
case 'r' :
fprintf(stdout, "%d - Option found:\t\t%s\n", __LINE__, argv[index]);
opt_rb = 1;
break;
default:
printf ("?? get_option() returned character code 0%o ??\n", c);
}
if (parameter)
free(parameter);
if (opt_rw) {
fprintf(stdout, "-rw option found:\t%s\n", argv[index]);
opt_rw = 0; /* purely cosmetic for this test progam */
}
}
opt_done:
if ((index) && (index < argc)) {
printf ("non-option ARGV-elements: ");
while (index < argc)
printf("%s ", argv[index++]);
printf ("\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}