-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnyParse.cc
93 lines (91 loc) · 2.45 KB
/
AnyParse.cc
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <AnyParse.hh>
#include <iostream>
#include <algorithm>
AnyParse::AnyParse(int argc, char** argv)
{
this->CommandLine = std::vector<std::string>(argv+1,argv+argc);
}
void AnyParse::Parse(){
std::string key = "NULL";
int remainingArgs = 0;
std::any selection;
std::vector<std::string> subArguments;
for( auto& arg : this->CommandLine )
{
if( arg.rfind("--", 0) == 0 )
{
arg.erase(0, 2);
key = arg;
//selection = Arguments[key];
remainingArgs = nargs[key];
subArguments.clear();
// If something takes no additional arguments then it is a switch
if( remainingArgs == 0 )
this->SetValue(key, true);
}
else if ( arg.rfind("-", 0) == 0 )
{
arg.erase(0, 1);
key = this->shortName[arg];
//selection = Arguments[key];
remainingArgs = nargs[key];
subArguments.clear();
if( remainingArgs == 0 )
this->SetValue(key, true);
}
else
{
// Collect the arguments for the selected key up to nargs
if( remainingArgs > 1 )
{
subArguments.push_back( arg );
remainingArgs--;
}
else if ( remainingArgs == 1 )
{
// Process the list of arguments
subArguments.push_back( arg );
remainingArgs--;
// Now can I convert?
if( subArguments.size() == 1 )
{
ConvertType cv = this->Conversion[key];
if( cv == ParseInt )
{
this->SetValue(key, stoi(arg));
}
else if( cv == ParseDouble )
{
this->SetValue(key, stod(arg));
}
else
this->SetValue(key, arg);
}
else
{
ConvertType cv = this->Conversion[key];
if( cv == ParseInt )
{
std::vector<int> vint;
std::transform( subArguments.begin(), subArguments.end(), std::back_inserter(vint),
[&](std::string s) { return stoi(s); } );
this->SetValue(key, vint);
}
else if( cv == ParseDouble )
{
std::vector<double> vdouble;
std::transform( subArguments.begin(), subArguments.end(), std::back_inserter(vdouble),
[&](std::string s) { return stod(s); } );
this->SetValue(key, vdouble);
}
else
this->SetValue(key, subArguments);
}
}
else
{
this->Positionals.push_back(arg);
}
}
}
}