-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnyParse.hh
51 lines (43 loc) · 1.24 KB
/
AnyParse.hh
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
#include <any>
#include <map>
#include <vector>
#include <string>
enum ConvertType {
ParseInt,
ParseDouble,
ParseString
};
class AnyParse {
public:
AnyParse(){};
AnyParse(int argc, char** argv);
template <typename T>
void SetValue(const std::string &key, T value){ Arguments[key] = value; }
template <typename T>
void AddArgument(const std::string &key, T value, const std::string &shortname,
int nargs, const std::string help, ConvertType cv)
{
this->shortName[shortname] = key;
this->help[key] = help;
this->nargs[key] = nargs;
this->SetValue(key, value);
this->Conversion[key] = cv;
}
template <typename T>
T GetValue(const std::string &key, T defaultValue) const
{
auto iterator = this->Arguments.find(key);
if( iterator == this->Arguments.end() )
return defaultValue;
return std::any_cast<T>(iterator->second);
}
void Parse();
// Properties of the argument
std::vector<std::string> CommandLine;
std::vector<std::string> Positionals;
std::map< std::string, ConvertType > Conversion;
std::map< std::string, std::any > Arguments;
std::map< std::string, std::string > shortName;
std::map< std::string, std::string > help;
std::map< std::string, int > nargs;
};