-
Notifications
You must be signed in to change notification settings - Fork 0
/
argparse.h
61 lines (53 loc) · 1.65 KB
/
argparse.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
52
53
54
55
56
57
58
59
60
61
#pragma once
#ifndef FASTCAT_ARGPARSE_H
#define FASTCAT_ARGPARSE_H
#include <stddef.h>
typedef enum ArgParseOptionType
{
ARGPARSE_OPT_END,
ARGPARSE_OPT_HELP,
ARGPARSE_OPT_BOOLEAN,
ARGPARSE_OPT_INTEGER,
ARGPARSE_OPT_SIZE,
ARGPARSE_OPT_DOUBLE,
ARGPARSE_OPT_STRING,
ARGPARSE__OPT_TYPECOUNT
} ArgParseOptionType;
static const char ArgumentDescriptionStrings[7][15] =
{
"",
"",
"",
"<int>",
"<positive int>",
"<float>",
"<str>"
};
typedef struct ArgumentOption
{
ArgParseOptionType type;
const char short_name;
const char* long_name;
void* value;
const char* desc;
} ArgumentOption;
typedef struct ArgParseInfo
{
const ArgumentOption* options;
const char* usage;
const char* description;
const char* epilog;
} ArgParseInfo;
// Macros for creating ArgumentOptions conveniently
#define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, NULL }
#define OPT_HELP(MSG) { ARGPARSE_OPT_HELP, 'h', "help", NULL, MSG }
#define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ }
#define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ }
#define OPT_SIZE(...) { ARGPARSE_OPT_SIZE, __VA_ARGS__ }
#define OPT_DOUBLE(...) { ARGPARSE_OPT_DOUBLE, __VA_ARGS__ }
#define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ }
void print_state(const ArgParseInfo* argParseInfo);
void print_usage(const ArgParseInfo* argParseInfo);
// I don't like the use of `int` here over `size_t` but I use it for consistency with `argc` being an int
int do_argparse(const ArgParseInfo* argParseInfo, int argc, const char** argv);
#endif //FASTCAT_ARGPARSE_H