Skip to content

Commit 201e2c8

Browse files
authored
Merge pull request #3 from VSirunyan/master
solved long required arguments issue (#2)
2 parents b46e228 + a4766a9 commit 201e2c8

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

argparse.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ class ArgumentParser {
3737
public:
3838
class ArgumentNotFound : public std::runtime_error {
3939
public:
40-
ArgumentNotFound(ArgumentParser::Argument &arg) noexcept
40+
ArgumentNotFound(ArgumentParser::Argument &arg,
41+
std::unordered_map<std::string, std::string> pairs) noexcept
4142
: std::runtime_error(
42-
("Required argument not found: " + arg._name).c_str()) {}
43+
("Required argument not found: " + arg._name + ((pairs.find(arg._name) == pairs.end())
44+
? "" : " or " + pairs.find(arg._name)->second)).c_str()) {}
4345
};
4446

4547
ArgumentParser(const std::string &desc) : _desc(desc), _help(false) {}
@@ -117,8 +119,9 @@ class ArgumentParser {
117119
if (!_help) {
118120
for (auto &a : _arguments) {
119121
if (a._required) {
120-
if (_variables.find(a._name) == _variables.end()) {
121-
throw ArgumentNotFound(a);
122+
if (_variables.find(a._name) == _variables.end()
123+
&& _variables.find(_pairs.find(a._name)->second) == _variables.end()) {
124+
throw ArgumentNotFound(a, _pairs);
122125
}
123126
}
124127
}

0 commit comments

Comments
 (0)