-
Notifications
You must be signed in to change notification settings - Fork 0
/
argcv.c
112 lines (100 loc) · 4.02 KB
/
argcv.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
#include "argcv.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static argcv_arg* options = NULL;
static int num_options = 0;
static int argc;
static char** argv;
void argcv_init(int _argc, char** _argv) {
argc = _argc;
argv = _argv;
options = NULL;
num_options = 0;
}
void argcv_print_usage() {
printf("%s Usage:\n", argv[0]);
for (int i = 0; i < num_options; i++) {
printf(" %s, %s: %s\n", options[i].shortopt, options[i].longopt, options[i].description);
}
}
void argcv_add_option(const char* shortopt, const char* longopt, argcv_arg_type type, void* variable, const char* description) {
if (strlen(shortopt) !=2) {
fprintf(stderr, "shortopt should be like -x");
exit(1);
}
num_options++;
options = (argcv_arg*)realloc(options, num_options * sizeof(argcv_arg));
options[num_options - 1].shortopt = shortopt;
options[num_options - 1].longopt = longopt;
options[num_options - 1].type = type;
options[num_options - 1].value = variable;
options[num_options - 1].description = description;
}
void argcv_parse() {
for (int i = 1; i < argc; i++) {
const char* arg = argv[i];
if (arg[0] == '-') {
if (arg[1] == '-') {
// Long option
const char* longopt = arg ;
for (int j = 0; j < num_options; j++) {
if (options[j].longopt && strcmp(options[j].longopt, longopt) == 0) {
// Matched long option
i++;
if (i < argc) {
if (options[j].type == ARGCV_INT) {
*((int*)options[j].value) = atoi(argv[i]);
} else if (options[j].type == ARGCV_FLOAT) {
*((float*)options[j].value) = atof(argv[i]);
} else if (options[j].type == ARGCV_STRING) {
*((const char**)options[j].value) = argv[i];
}
} else {
if (options[j].type == ARGCV_BOOL) {
*((bool*)options[j].value) = true;
} else {
fprintf(stderr, "Error: Missing value for option --%s\n", longopt);
argcv_print_usage();
exit(1);
}
}
break;
}
}
} else {
// Short option
char shortopt = arg[1];
for (int j = 0; j < num_options; j++) {
if (options[j].shortopt && options[j].shortopt[1] == shortopt) {
// Matched short option
i++;
if (i < argc) {
if (options[j].type == ARGCV_INT) {
*((int*)options[j].value) = atoi(argv[i]);
} else if (options[j].type == ARGCV_FLOAT) {
*((float*)options[j].value) = atof(argv[i]);
} else if (options[j].type == ARGCV_STRING) {
*((const char**)options[j].value) = argv[i];
}
} else {
if (options[j].type == ARGCV_BOOL) {
*((bool*)options[j].value) = true;
} else {
fprintf(stderr, "Error: Missing value for option -%c\n", shortopt);
argcv_print_usage();
exit(1);
}
}
break;
}
}
}
}
}
}
void argcv_destroy() {
free(options);
options = NULL;
num_options = 0;
}