-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptparser.c
56 lines (48 loc) · 1.56 KB
/
optparser.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
#include "optparser.h"
#include "stdbool.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "utils.h"
const char *unary_args[] = {"--version", "-v", "--help", "-h"};
const char *supported_commands[] = {"--version", "-v", "--help", "-h"};
const char *supported_options[] = {"--host", "--port"};
bool is_unary_arg(char *arg) {
for (size_t i = 0; i < sizeof(unary_args) / sizeof(unary_args[0]); i++) {
if (strcmp(unary_args[i], arg) == 0) {
return true;
}
}
return false;
}
ArgKV *parse_options(int argc, char *argv[], size_t *length) {
if (argc == 1) {
return NULL;
}
*length = argc - 1;
ArgKV *arg_kvs = malloc(sizeof(ArgKV) * *length);
for (int i = 1; i < argc; i++) {
char *kv = argv[i];
char *key = strtok(kv, "=");
if (key == NULL) {
fprintf(stderr, "Invalid arguments: provide arguments in `--key=value` format\n");
exit(1);
}
key = remove_leading_dashes(key);
size_t key_len = strlen(key);
arg_kvs[i - 1].key = (char *)malloc(sizeof(char *) * key_len + 1);
strncpy(arg_kvs[i - 1].key, key, key_len);
arg_kvs[i - 1].key[key_len] = '\0';
char *value = strtok(NULL, "=");
if (value == NULL) {
fprintf(stderr, "Invalid arguments. Value is empty: provide arguments in "
"`--key=value` format\n");
exit(1);
}
size_t value_len = strlen(value);
arg_kvs[i - 1].value = (char *)malloc(sizeof(char *) * value_len + 1);
strncpy(arg_kvs[i - 1].value, value, value_len);
arg_kvs[i - 1].value[value_len] = '\0';
}
return arg_kvs;
}