-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArgumentParser.cpp
148 lines (135 loc) · 4.28 KB
/
ArgumentParser.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "ArgumentParser.h"
namespace
{
std::string unrecognizedOption(const std::string& programName,
const char* option)
{
std::stringstream ss;
ss << programName << ": error: unrecognized command line option '" << option
<< "'\n";
return ss.str();
}
std::string missingArgument(const std::string& programName,
const std::string& name)
{
std::stringstream ss;
ss << programName << ": error: missing argument after '" << name << "'\n";
return ss.str();
}
std::string multipleArguments(const std::string& programName,
const std::string& name)
{
std::stringstream ss;
ss << programName << ": error: multiple '" << name << "' arguments\n";
return ss.str();
}
std::string missingPositionalArguments(
const std::string& programName,
const std::vector<std::string>& positionalArguments,
size_t index)
{
std::stringstream ss;
ss << programName << ": error: missing positional arguments:";
for (size_t ii = index; ii < positionalArguments.size(); ++ii)
ss << " '" << positionalArguments[ii] << "',";
ss << "\n";
return ss.str();
}
void verifyPositionalArgumentsWereConsumed(
const std::string& programName,
const std::vector<std::string>& positionalArguments,
size_t index)
{
if (index < positionalArguments.size())
{
const auto message = missingPositionalArguments(programName,
positionalArguments,
index);
throw ArgumentParser::ParseError(message.c_str());
}
}
bool isOption(const char* text)
{
if (text == NULL)
return false;
return text[0] == '-';
}
}
ArgumentParser::ParseError::ParseError(const char* message) :
std::runtime_error(message)
{
}
ArgumentParser::InvalidOption::InvalidOption(const char* message) :
std::runtime_error(message)
{
}
ArgumentParser::ArgumentParser(const std::string& programName) :
mProgramName(programName)
{
}
void ArgumentParser::addOption(const std::string& name, bool takesArgument)
{
if (isOption(name.c_str()))
{
mOptions.push_back(Option(name, takesArgument));
if (!takesArgument)
mDefaultResults[name] = "";
}
else
{
if (takesArgument)
throw InvalidOption("positional argument cannot take an argument");
mPositionalArguments.push_back(name);
}
}
std::map<std::string, std::string> ArgumentParser::parse(int argc, char** argv)
{
auto result = mDefaultResults;
size_t positionalArgumentIndex = 0;
// TODO: This is a bit of a mess, but it works...
for (int i = 1; i < argc; ++i)
{
char* currentArg = argv[i];
if (isOption(currentArg))
{
for (const auto& option : mOptions)
{
if (option.name == currentArg)
{
if (!result[option.name].empty())
throw ParseError(
multipleArguments(mProgramName, option.name)
.c_str());
if (option.takesArgument)
{
i++;
if (i == argc || isOption(argv[i]))
throw ParseError(
missingArgument(mProgramName, option.name)
.c_str());
result[option.name] = argv[i];
}
else
{
result[option.name] = "true";
}
break;
}
}
if (result.find(currentArg) == result.end())
{
throw ParseError(
unrecognizedOption(mProgramName, argv[i]).c_str());
}
}
else
{
result[mPositionalArguments[positionalArgumentIndex++]] =
currentArg;
}
}
verifyPositionalArgumentsWereConsumed(mProgramName,
mPositionalArguments,
positionalArgumentIndex);
return result;
}