-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetopt.c3
184 lines (161 loc) · 3.38 KB
/
getopt.c3
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
module serp::getopt;
import std::collections::list;
import std::io;
struct Flag
{
char shortopt;
String longopt;
bool optarg;
String desc;
}
struct Option (Printable)
{
inline Flag flag;
String arg;
}
fn usz! Option.to_format(&opt, Formatter *f) @dynamic
{
usz n;
if (opt.shortopt) n+= f.printf("-%c", opt.shortopt)!;
if (opt.shortopt && opt.longopt) n += f.printf(", ")!;
if (opt.longopt) n += f.printf("--%s", opt.longopt)!;
if (opt.optarg) n += f.printf(" %s", opt.arg)!;
if (opt.desc) n += f.printf("\t%s", opt.desc)!;
if (n > 0) n += f.printf("\n")!;
return n;
}
fault Error
{
UNKNOWN_OPTION,
MISSING_ARGUMENT
}
<*
Parses command-line arguments..
@param [in] args "Strings of options."
@param [in] flags "Specification of options."
@param [out] optind "Index of the first non-option argument"
@require args.len > 0
@return "Slice of options. Allocated on heap by default."
@return! Error.UNKNOWN_OPTION, Error.MISSING_ARGUMENT
*>
fn Option[]! parse(String[] args, Flag[] flags, int *optind, Allocator allocator)
{
int i;
List(<Option>) opts;
opts.temp_init(flags.len);
// remove program name
args = args[1..];
i++;
// parse arguments
while LOOP: (args.len) {
String arg = args[0];
switch {
case arg == "--":
i++;
break LOOP;
case arg == "-":
break LOOP;
case arg.len == 0:
break LOOP;
case arg[0] != '-':
break LOOP;
}
arg = arg[1..];
if (arg.len > 0 && arg[0] == '-')
{
// parse long flag options
arg = arg[1..];
Flag flag;
foreach (f : flags)
{
if (f.longopt == arg)
{
flag = f;
break;
}
}
if (flag.longopt.len == 0) return Error.UNKNOWN_OPTION?;
if (flag.optarg) {
if (args.len >= 2) {
args = args[1..];
i++;
opts.push({.flag=flag, .arg=args[0]});
} else {
return Error.MISSING_ARGUMENT?;
}
} else {
opts.push({.flag=flag});
}
}
else
{
// parse single flag options
foreach (c : arg) {
Flag flag;
foreach (f : flags)
{
if (f.shortopt == c)
{
flag = f;
break;
}
}
if (flag.shortopt == 0) return Error.UNKNOWN_OPTION?;
if (flag.optarg) {
if (args.len >= 2) {
args = args[1..];
i++;
opts.push({.flag=flag, .arg=args[0]});
} else {
return Error.MISSING_ARGUMENT?;
}
} else {
opts.push({.flag=flag});
}
}
}
args = args[1..];
i++;
}
if (optind) {
*optind = i;
}
return opts.to_new_array(allocator);
}
fn Option[]! parse_new(String[] args, Flag[] flags, int *optind = null) @inline
{
return parse(args, flags, optind, allocator::heap());
}
fn Option[]! parse_temp(String[] args, Flag[] flags, int *optind = null) @inline
{
return parse(args, flags, optind, allocator::temp());
}
fn Option[]! getopts(String[] args, String optstr, int *optind = null, Allocator allocator)
{
@pool()
{
Flag[] flags = mem::temp_alloc_array(Flag, optstr.len);
int i = 0;
foreach (c : optstr)
{
if (c == ':')
{
flags[i].optarg = true;
}
else
{
flags[i].shortopt = c;
i++;
}
}
return parse(args, flags, optind, allocator);
};
}
fn Option[]! getopts_new(String[] args, String optstr, int *optind = null) @inline
{
return getopts(args, optstr, optind, allocator::heap());
}
fn Option[]! getopts_temp(String[] args, String optstr, int *optind = null) @inline
{
return getopts(args, optstr, optind, allocator::temp());
}