-
Notifications
You must be signed in to change notification settings - Fork 2
/
args.cpp
253 lines (227 loc) · 7 KB
/
args.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (C) 2019 by Yuri Victorovich. All rights reserved.
#include "args.h"
#include "util.h"
#include "err.h"
#include <rang.hpp>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <fstream>
#define ERR(msg...) \
ERR2("parse args", msg)
//
// internals
//
static bool strEq(const char *s1, const char *s2) {
return strcmp(s1, s2) == 0;
}
static void usage() {
std::cout << "usage: crate [-h|--help] COMMAND [...command arguments...]" << std::endl;
std::cout << "" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -h, --help show this help screen" << std::endl;
std::cout << "" << std::endl;
std::cout << "Commands:" << std::endl;
std::cout << " create creates a container (run 'crate create -h' for details)" << std::endl;
std::cout << " run runs the containerzed application (run 'crate run -h' for details)" << std::endl;
std::cout << "" << std::endl;
}
static void usageCreate() {
std::cout << "usage: crate create [-s <spec-file>|--spec <spec-file>] [-o <output-create-file>|--output <output-create-file>]" << std::endl;
std::cout << "" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -s, --spec <spec-file> crate specification (required)" << std::endl;
std::cout << " -o, --output <output-create-file> output crate file" << std::endl;
std::cout << " -h, --help show this help screen" << std::endl;
std::cout << "" << std::endl;
}
static void usageRun() {
std::cout << "usage: crate run [-h|--help] <create-file>" << std::endl;
std::cout << "" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -h, --help show this help screen" << std::endl;
std::cout << "" << std::endl;
}
static void err(const char *msg) {
fprintf(stderr, "failed to parse arguments: %s\n", msg);
std::cout << "" << std::endl;
usage();
exit(1);
}
static void err(const char *fmt, const char *arg) {
fprintf(stderr, "failed to parse arguments: ");
fprintf(stderr, fmt, arg);
fprintf(stderr, "\n");
std::cout << "" << std::endl;
usage();
exit(1);
}
static const char isShort(const char* arg) {
if (arg[0] == '-' && (isalpha(arg[1]) || isdigit(arg[1])) && arg[2] == 0)
return arg[1];
return 0;
}
static const char* isLong(const char* arg) {
if (arg[0] == '-' && arg[1] == '-') {
for (int i = 2; arg[i]; i++)
if (!islower(arg[i]) || !isdigit(arg[i]))
return nullptr;
return arg + 2;
}
return nullptr;
}
static Command isCommand(const char* arg) {
if (strEq(arg, "create"))
return CmdCreate;
if (strEq(arg, "run"))
return CmdRun;
return CmdNone;
}
static const char* getArgParam(int aidx, int argc, char** argv) {
if (aidx >= argc)
err("argument parameter expected but no more arguments were supplied");
if (argv[aidx][0] == '-')
err("argument parameter can't begin from the hyphen");
return argv[aidx];
}
//
// interface
//
void Args::validate() {
switch (cmd) {
case CmdCreate:
if (createSpec.empty())
ERR("the 'create' command requires the crate spec file as an argument (-s, --spec)")
break;
case CmdRun:
if (runCrateFile.empty())
ERR("the 'run' command requires the crate file as an argument (-f, --file)")
if (!std::ifstream(runCrateFile).good())
ERR("the file passed to the 'run' command can't be opened: " << runCrateFile)
break;
default:
err("no command was given");
}
}
Args parseArguments(int argc, char** argv, unsigned &processed) {
Args args;
// first, see if the command form is a shortened one: 'crate {name}.yml' or 'crate {name}.crate ...'
if (argc >= 2 && argv[1][0] != '-') {
if (argc == 2 && Util::Fs::hasExtension(argv[1], ".yml")) {
args.cmd = CmdCreate;
args.createSpec = argv[1];
processed = 2;
return args;
} else if (Util::Fs::hasExtension(argv[1], ".crate") && Util::Fs::isXzArchive(argv[1])) {
args.cmd = CmdRun;
args.runCrateFile = argv[1];
processed = 2;
return args;
}
}
enum Loc {LocBeforeCmd, LocAfterCmd};
Loc loc = LocBeforeCmd;
int a;
bool stop = false;
for (a = 1; !stop && a < argc; a++) {
switch (loc) {
case LocBeforeCmd:
if (auto argShort = isShort(argv[a])) {
switch (argShort) {
case 'h':
usage();
exit(0);
case 'p':
args.logProgress = true;
break;
default:
err("unsupported short option '%s'", argv[a]);
}
} else if (auto argLong = isLong(argv[a])) {
if (strEq(argLong, "help")) {
usage();
exit(0);
} else if (strEq(argLong, "log-progress")) {
args.logProgress = true;
break;
} else {
err("unsupported long option '%s'", argv[a]);
}
} else if (auto cmd = isCommand(argv[a])) {
args.cmd = cmd;
loc = LocAfterCmd;
break;
} else {
err("unknown argument '%s'", argv[a]);
}
case LocAfterCmd:
switch (args.cmd) {
case CmdNone:
// impossible
break;
case CmdCreate:
if (auto argShort = isShort(argv[a])) {
switch (argShort) {
case 'h':
usageCreate();
exit(0);
case 's':
args.createSpec = getArgParam(++a, argc, argv);
break;
case 'o':
args.createOutput = getArgParam(++a, argc, argv);
break;
default:
err("unsupported short option '%s'", argv[a]);
}
} else if (auto argLong = isLong(argv[a])) {
if (strEq(argLong, "help")) {
usageCreate();
exit(0);
} else if (strEq(argLong, "spec")) {
args.createSpec = getArgParam(++a, argc, argv);
break;
} else if (strEq(argLong, "output")) {
args.createOutput = getArgParam(++a, argc, argv);
break;
} else {
err("unsupported long option '%s'", argv[a]);
}
} else {
err("unknown argument '%s'", argv[a]);
}
break;
case CmdRun:
if (strEq(argv[a], "--")) {
stop = true;
break;
} else if (auto argShort = isShort(argv[a])) {
switch (argShort) {
case 'h':
usageRun();
exit(0);
case 'f':
args.runCrateFile = getArgParam(++a, argc, argv);
break;
default:
err("unsupported short option '%s'", argv[a]);
}
} else if (auto argLong = isLong(argv[a])) {
if (strEq(argLong, "help")) {
usage();
exit(0);
} else if (strEq(argLong, "file")) {
args.runCrateFile = getArgParam(++a, argc, argv);
} else {
err("unsupported long option '%s'", argv[a]);
}
} else {
err("unknown argument '%s'", argv[a]);
}
}
}
}
processed = a;
return args;
}