-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli-parser.c
141 lines (118 loc) · 3.66 KB
/
cli-parser.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cli-parser.h"
struct ParseOptionsResult parse_options(int argc, char **argv,
const struct CliOptions cliOptions) {
struct ParseOptionsResult result = {
.optionsCount = 0, .argcLeft = argc, .shouldContinue = true};
const struct OptionDef *currentOptionDef = NULL;
while (result.argcLeft > 0) {
char *arg = argv[0];
result.argcLeft--;
argv++;
if (currentOptionDef && currentOptionDef->has_arg) {
if (result.shouldContinue) {
enum OptionCallbackResult callbackResult =
currentOptionDef->callback(arg);
if (callbackResult == OPTION_CALLBACK_RESULT_ERROR) {
exit(2);
} else if (callbackResult == OPTION_CALLBACK_RESULT_OK_STOP) {
result.shouldContinue = false;
}
}
currentOptionDef = NULL;
continue;
}
const bool isLong = strncmp(arg, "--", 2) == 0;
const bool hasDash = arg[0] == '-';
for (int i = 0; i < cliOptions.count; i++) {
const struct OptionDef *optionDef = &cliOptions.optionDefs[i];
if (isLong) {
if (optionDef->name && strcmp(arg + 2, optionDef->name) == 0) {
currentOptionDef = optionDef;
break;
}
} else if (hasDash && strlen(arg) == 2) {
if (arg[1] == optionDef->short_option) {
currentOptionDef = optionDef;
break;
}
}
}
if (currentOptionDef) {
if (!currentOptionDef->has_arg) {
if (result.shouldContinue) {
enum OptionCallbackResult callbackResult =
currentOptionDef->callback(NULL);
if (callbackResult == OPTION_CALLBACK_RESULT_ERROR) {
exit(2);
} else if (callbackResult == OPTION_CALLBACK_RESULT_OK_STOP) {
result.shouldContinue = false;
}
}
currentOptionDef = NULL;
}
continue;
}
if (arg[0] == '-' && strlen(arg) != 1) {
fprintf(stderr, "Unrecognized option \"%s\"\n", arg);
exit(2);
} else {
argv--;
result.argcLeft++;
break;
}
}
if (currentOptionDef) {
fprintf(stderr, "Missing value for option \"%s\"\n",
currentOptionDef->name);
exit(2);
}
result.remainingArgv = argv;
return result;
}
void print_help(char *command, const struct CliOptions cliOptions) {
printf("Usage: %s [options] file\n", command);
printf("Options:\n");
int longestOption = 0;
for (int i = 0; i < cliOptions.count; i++) {
const struct OptionDef *option = &cliOptions.optionDefs[i];
// 3 is for `-${short_name} ` and 2 is for ` ` printed
// before the option
int optionLength = 3 + 2;
if (option->long_option) {
// 2 is for `--`, 3 is for `-${short_name} ` and 2 is for ` ` printed
optionLength += (int)strlen(option->long_option) + 2;
}
// ` ` and `value`
if (option->has_arg)
optionLength += 1 + 5;
if (optionLength > longestOption)
longestOption = optionLength;
}
int padLength = longestOption + 2;
for (size_t i = 0; i < cliOptions.count; i++) {
const struct OptionDef option = cliOptions.optionDefs[i];
printf(" ");
int toPad = padLength;
if (option.short_option) {
printf("-%c", option.short_option);
toPad -= 2;
}
if (option.long_option) {
if (option.short_option) {
printf(", ");
toPad -= 2;
}
printf("--%s", option.long_option);
toPad -= (int)strlen(option.long_option) + 2;
}
if (option.has_arg) {
printf(" value");
toPad -= 6;
}
printf("%*s", toPad, " ");
printf("%s\n", option.description);
}
}