|
|
608c44 |
|
|
|
608c44 |
struct ArgDesc;
|
|
|
608c44 |
typedef int (*ArgFunc)(const struct ArgDesc *desc, const char *arg);
|
|
|
608c44 |
|
|
|
608c44 |
typedef struct ArgDesc {
|
|
|
608c44 |
char c;
|
|
|
608c44 |
void *data;
|
|
|
608c44 |
ArgFunc func;
|
|
|
608c44 |
const char *desc;
|
|
|
608c44 |
} ArgDesc;
|
|
|
608c44 |
|
|
|
608c44 |
|
|
|
608c44 |
int pargInt(const ArgDesc *desc, const char *arg)
|
|
|
14902d |
{ *(int*)desc->data = atoi(arg); return TRUE; }
|
|
|
608c44 |
int pargDouble(const ArgDesc *desc, const char *arg)
|
|
|
14902d |
{ *(double*)desc->data = atof(arg); return TRUE; }
|
|
|
608c44 |
int pargString(const ArgDesc *desc, const char *arg)
|
|
|
608c44 |
{ *(const char**)desc->data = arg; return TRUE; }
|
|
|
608c44 |
|
|
|
608c44 |
|
|
|
608c44 |
int parseArgs(const ArgDesc *descs, int argc, char **argv) {
|
|
|
608c44 |
for(; argc; --argc, ++argv) {
|
|
|
608c44 |
int found = FALSE;
|
|
|
608c44 |
for(const ArgDesc *desc = descs; desc->c && desc->data; ++desc) {
|
|
|
608c44 |
if ((*argv)[0] == '-' && (*argv)[1] == desc->c && !(*argv)[2]) {
|
|
|
608c44 |
if (desc->func) {
|
|
|
608c44 |
--argc; ++argv;
|
|
|
608c44 |
if (!argc || !desc->func(desc, *argv)) {
|
|
|
608c44 |
fprintf(stderr, "Wrong argument for -%c: %s\n", desc->c, *argv);
|
|
|
608c44 |
fflush(stderr);
|
|
|
608c44 |
return FALSE;
|
|
|
608c44 |
}
|
|
|
608c44 |
} else {
|
|
|
608c44 |
*((int*)desc->data) = TRUE;
|
|
|
608c44 |
}
|
|
|
608c44 |
found = TRUE;
|
|
|
608c44 |
break;
|
|
|
608c44 |
}
|
|
|
608c44 |
}
|
|
|
608c44 |
if (!found) {
|
|
|
608c44 |
fprintf(stderr, "Unknown arg: %s\n", *argv);
|
|
|
608c44 |
fflush(stderr);
|
|
|
608c44 |
return FALSE;
|
|
|
608c44 |
}
|
|
|
608c44 |
}
|
|
|
608c44 |
return TRUE;
|
|
|
608c44 |
}
|
|
|
608c44 |
|
|
|
608c44 |
|
|
|
608c44 |
void printUsage(const ArgDesc *descs) {
|
|
|
608c44 |
printf("Available command-line arguments:\n");
|
|
|
608c44 |
for(const ArgDesc *desc = descs; desc->c && desc->data; ++desc)
|
|
|
608c44 |
printf(" -%c %s - %s\n", desc->c, desc->func ? "<arg>" : " ", desc->desc);</arg>
|
|
|
608c44 |
fflush(stdout);
|
|
|
608c44 |
}
|
|
|
608c44 |
|