-
Notifications
You must be signed in to change notification settings - Fork 4
/
cmdline.h
51 lines (46 loc) · 1.24 KB
/
cmdline.h
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
#pragma once
#define CMD_TOKENS_MAX 256
#define CMD_SWITCHES_MAX 256
#define CMD_PARAMS_MAX 256
typedef struct cmd_param {
char *key;
char *value;
} cmd_param;
void parse_command_line(int argc, char **argv,
char * tokens[], char * switches[],
cmd_param params[]);
#ifdef CMDLINE_IMPLEMENTATION
#include <string.h>
void parse_command_line(int argc, char **argv,
char * tokens[], char * switches[],
cmd_param params[])
{
int ntoken = 0;
int nparam = 0;
int nswitch = 0;
for (int i = 0; i != argc; ++i)
{
char *arg = argv[i];
if (arg[0]=='-' || arg[0] =='/') {
char * s = strchr(++arg, '=');
if (s > arg) {
if (nparam + 1 < CMD_PARAMS_MAX) {
cmd_param *param = params + nparam++;
*s++ = 0;
param->key = arg;
param->value = s;
}
}
else if (nswitch + 1 < CMD_SWITCHES_MAX) {
switches[nswitch++] = arg;
}
}
else if (ntoken + 1 < CMD_TOKENS_MAX) {
tokens[ntoken++] = arg;
}
}
params[nparam].key = 0;
tokens[ntoken] = 0;
switches[nswitch] = 0;
}
#endif