-
Notifications
You must be signed in to change notification settings - Fork 2
/
args_parser.h
61 lines (50 loc) · 1.2 KB
/
args_parser.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
#include <cctype>
#include <string>
#define CONSTSTR const std::string &
enum CLIArgType {
CLIARG_NONE,
CLIARG_FLAG,
CLIARG_S64,
CLIARG_STR
};
// Meta container
class CLIArg {
public:
CLIArg() = delete;
CLIArgType getType() const { return m_type; }
static void parseArgs(int argc, char **argv);
protected:
CLIArg(CONSTSTR prefix, CLIArgType type);
virtual bool parse(const char *str) { return false; }
std::string m_prefix;
CLIArgType m_type;
};
// Various data types
class CLIArgFlag : public CLIArg {
public:
CLIArgFlag(CONSTSTR prefix) :
CLIArg(prefix, CLIARG_FLAG) {}
bool get() const { return m_value; }
protected:
bool parse(const char *str) { return m_value = true; }
bool m_value = false;
};
class CLIArgS64 : public CLIArg {
public:
CLIArgS64(CONSTSTR prefix, int64_t def = 0) :
CLIArg(prefix, CLIARG_S64), m_value(def) {}
int64_t get() const { return m_value; }
protected:
bool parse(const char *str);
int64_t m_value;
};
class CLIArgStr : public CLIArg {
public:
CLIArgStr(CONSTSTR prefix, CONSTSTR def = "") :
CLIArg(prefix, CLIARG_STR), m_value(def) {}
CONSTSTR get() const { return m_value; }
protected:
bool parse(const char *str);
std::string m_value;
};