Skip to content

Commit

Permalink
Merge 920f22c into d8905de
Browse files Browse the repository at this point in the history
  • Loading branch information
bitc committed Jan 21, 2018
2 parents d8905de + 920f22c commit 223a254
Showing 1 changed file with 55 additions and 25 deletions.
80 changes: 55 additions & 25 deletions args.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ namespace args
#ifdef ARGS_NOEXCEPT
/// Only for ARGS_NOEXCEPT
mutable Error error = Error::None;
mutable std::string errorMsg;
#endif

public:
Expand Down Expand Up @@ -893,6 +894,7 @@ namespace args
matched = false;
#ifdef ARGS_NOEXCEPT
error = Error::None;
errorMsg.clear();
#endif
}

Expand All @@ -902,6 +904,12 @@ namespace args
{
return error;
}

/// Only for ARGS_NOEXCEPT
std::string GetErrorMsg() const
{
return errorMsg;
}
#endif
};

Expand Down Expand Up @@ -1081,11 +1089,12 @@ namespace args
{
if ((GetOptions() & Options::Single) != Options::None && matched)
{
std::ostringstream problem;
problem << "Flag '" << flag.str() << "' was passed multiple times, but is only allowed to be passed once";
#ifdef ARGS_NOEXCEPT
error = Error::Extra;
errorMsg = problem.str();
#else
std::ostringstream problem;
problem << "Flag '" << flag.str() << "' was passed multiple times, but is only allowed to be passed once";
throw ExtraError(problem.str());
#endif
}
Expand All @@ -1109,13 +1118,12 @@ namespace args
{
if (!Matched() && IsRequired())
{
std::ostringstream problem;
problem << "Flag '" << matcher.GetLongOrAny().str(shortPrefix, longPrefix) << "' is required";
#ifdef ARGS_NOEXCEPT
(void)shortPrefix;
(void)longPrefix;
error = Error::Required;
errorMsg = problem.str();
#else
std::ostringstream problem;
problem << "Flag '" << matcher.GetLongOrAny().str(shortPrefix, longPrefix) << "' is required";
throw RequiredError(problem.str());
#endif
}
Expand Down Expand Up @@ -1260,6 +1268,7 @@ namespace args
ready = true;
#ifdef ARGS_NOEXCEPT
error = Error::None;
errorMsg.clear();
#endif
}

Expand All @@ -1283,11 +1292,12 @@ namespace args
{
if (IsRequired() && !Matched())
{
std::ostringstream problem;
problem << "Option '" << Name() << "' is required";
#ifdef ARGS_NOEXCEPT
error = Error::Required;
errorMsg = problem.str();
#else
std::ostringstream problem;
problem << "Option '" << Name() << "' is required";
throw RequiredError(problem.str());
#endif
}
Expand Down Expand Up @@ -1554,6 +1564,7 @@ namespace args
}
#ifdef ARGS_NOEXCEPT
error = Error::None;
errorMsg.clear();
#endif
}

Expand Down Expand Up @@ -2118,11 +2129,12 @@ namespace args
{
if (child->IsGroup() && !child->Matched())
{
std::ostringstream problem;
problem << "Group validation failed somewhere!";
#ifdef ARGS_NOEXCEPT
error = Error::Validation;
errorMsg = problem.str();
#else
std::ostringstream problem;
problem << "Group validation failed somewhere!";
throw ValidationError(problem.str());
#endif
}
Expand All @@ -2137,11 +2149,12 @@ namespace args

if (selectedCommand == nullptr && commandIsRequired && (Group::HasCommand() || subparserHasCommand))
{
std::ostringstream problem;
problem << "Command is required";
#ifdef ARGS_NOEXCEPT
error = Error::Validation;
errorMsg = problem.str();
#else
std::ostringstream problem;
problem << "Command is required";
throw ValidationError(problem.str());
#endif
}
Expand Down Expand Up @@ -2360,6 +2373,7 @@ namespace args
throw ParseError(errorMessage);
#else
error = Error::Parse;
errorMsg = errorMessage;
return false;
#endif
}
Expand All @@ -2376,10 +2390,12 @@ namespace args
}
} else
{
const std::string errorMessage("Flag could not be matched: " + arg);
#ifndef ARGS_NOEXCEPT
throw ParseError("Flag could not be matched: " + arg);
throw ParseError(errorMessage);
#else
error = Error::Parse;
errorMsg = errorMessage;
return false;
#endif
}
Expand Down Expand Up @@ -2410,6 +2426,7 @@ namespace args
throw ParseError(errorMessage);
#else
error = Error::Parse;
errorMsg = errorMessage;
return false;
#endif
}
Expand All @@ -2431,10 +2448,12 @@ namespace args
}
} else
{
const std::string errorMessage("Flag could not be matched: '" + std::string(1, arg) + "'");
#ifndef ARGS_NOEXCEPT
throw ParseError("Flag could not be matched: '" + std::string(1, arg) + "'");
throw ParseError(errorMessage);
#else
error = Error::Parse;
errorMsg = errorMessage;
return false;
#endif
}
Expand Down Expand Up @@ -2607,10 +2626,12 @@ namespace args
auto itCommand = std::find_if(commands.begin(), commands.end(), [&chunk](Command *c) { return c->Name() == chunk; });
if (itCommand == commands.end())
{
const std::string errorMessage("Unknown command: " + chunk);
#ifndef ARGS_NOEXCEPT
throw ParseError("Unknown command: " + chunk);
throw ParseError(errorMessage);
#else
error = Error::Parse;
errorMsg = errorMessage;
return it;
#endif
}
Expand Down Expand Up @@ -2658,10 +2679,12 @@ namespace args
}
} else
{
const std::string errorMessage("Passed in argument, but no positional arguments were ready to receive it: " + chunk);
#ifndef ARGS_NOEXCEPT
throw ParseError("Passed in argument, but no positional arguments were ready to receive it: " + chunk);
throw ParseError(errorMessage);
#else
error = Error::Parse;
errorMsg = errorMessage;
return it;
#endif
}
Expand Down Expand Up @@ -2796,10 +2819,12 @@ namespace args
{
if (longseparator_.empty())
{
const std::string errorMessage("longseparator can not be set to empty");
#ifdef ARGS_NOEXCEPT
error = Error::Usage;
errorMsg = errorMessage;
#else
throw UsageError("longseparator can not be set to empty");
throw UsageError(errorMessage);
#endif
} else
{
Expand Down Expand Up @@ -3125,6 +3150,7 @@ namespace args
{
#ifdef ARGS_NOEXCEPT
error = Error::Help;
errorMsg = Name();
#else
throw Help(Name());
#endif
Expand Down Expand Up @@ -3638,11 +3664,12 @@ namespace args
auto it = map.find(key);
if (it == std::end(map))
{
std::ostringstream problem;
problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
#ifdef ARGS_NOEXCEPT
error = Error::Map;
errorMsg = problem.str();
#else
std::ostringstream problem;
problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
throw MapError(problem.str());
#endif
} else
Expand Down Expand Up @@ -3724,11 +3751,12 @@ namespace args
auto it = map.find(key);
if (it == std::end(map))
{
std::ostringstream problem;
problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
#ifdef ARGS_NOEXCEPT
error = Error::Map;
errorMsg = problem.str();
#else
std::ostringstream problem;
problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
throw MapError(problem.str());
#endif
} else
Expand Down Expand Up @@ -3988,11 +4016,12 @@ namespace args
auto it = map.find(key);
if (it == std::end(map))
{
std::ostringstream problem;
problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
#ifdef ARGS_NOEXCEPT
error = Error::Map;
errorMsg = problem.str();
#else
std::ostringstream problem;
problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
throw MapError(problem.str());
#endif
} else
Expand Down Expand Up @@ -4076,11 +4105,12 @@ namespace args
auto it = map.find(key);
if (it == std::end(map))
{
std::ostringstream problem;
problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
#ifdef ARGS_NOEXCEPT
error = Error::Map;
errorMsg = problem.str();
#else
std::ostringstream problem;
problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
throw MapError(problem.str());
#endif
} else
Expand Down

0 comments on commit 223a254

Please sign in to comment.