Skip to content

Commit

Permalink
Adding more detail to error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Nov 19, 2017
1 parent 5f1df63 commit 94b3355
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
44 changes: 26 additions & 18 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,10 @@ class App {
T &variable, ///< The variable to set
std::string description = "") {

CLI::callback_t fun = [&variable](CLI::results_t res) {
std::string simple_name = CLI::detail::split(name, ',').at(0);
CLI::callback_t fun = [&variable, simple_name](CLI::results_t res) {
if(res.size() != 1)
return false;
throw ConversionError("Only one " + simple_name + " allowed");
return detail::lexical_cast(res[0], variable);
};

Expand All @@ -259,9 +260,10 @@ class App {
std::string description,
bool defaulted) {

CLI::callback_t fun = [&variable](CLI::results_t res) {
std::string simple_name = CLI::detail::split(name, ',').at(0);
CLI::callback_t fun = [&variable, simple_name](CLI::results_t res) {
if(res.size() != 1)
return false;
throw ConversionError("Only one " + simple_name + " allowed");
return detail::lexical_cast(res[0], variable);
};

Expand Down Expand Up @@ -405,13 +407,14 @@ class App {
std::set<T> options, ///< The set of posibilities
std::string description = "") {

CLI::callback_t fun = [&member, options](CLI::results_t res) {
std::string simple_name = CLI::detail::split(name, ',').at(0);
CLI::callback_t fun = [&member, options, simple_name](CLI::results_t res) {
if(res.size() != 1) {
return false;
throw ConversionError("Only one " + simple_name + " allowed");
}
bool retval = detail::lexical_cast(res[0], member);
if(!retval)
return false;
throw ConversionError("The value " + res[0] + "is not an allowed value for " + simple_name);
return std::find(std::begin(options), std::end(options), member) != std::end(options);
};

Expand All @@ -430,13 +433,14 @@ class App {
std::string description,
bool defaulted) {

CLI::callback_t fun = [&member, options](CLI::results_t res) {
std::string simple_name = CLI::detail::split(name, ',').at(0);
CLI::callback_t fun = [&member, options, simple_name](CLI::results_t res) {
if(res.size() != 1) {
return false;
throw ConversionError("Only one " + simple_name + " allowed");
}
bool retval = detail::lexical_cast(res[0], member);
if(!retval)
return false;
throw ConversionError("The value " + res[0] + "is not an allowed value for " + simple_name);
return std::find(std::begin(options), std::end(options), member) != std::end(options);
};

Expand All @@ -458,16 +462,17 @@ class App {
std::set<std::string> options, ///< The set of posibilities
std::string description = "") {

CLI::callback_t fun = [&member, options](CLI::results_t res) {
std::string simple_name = CLI::detail::split(name, ',').at(0);
CLI::callback_t fun = [&member, options, simple_name](CLI::results_t res) {
if(res.size() != 1) {
return false;
throw ConversionError("Only one " + simple_name + " allowed");
}
member = detail::to_lower(res[0]);
auto iter = std::find_if(std::begin(options), std::end(options), [&member](std::string val) {
return detail::to_lower(val) == member;
});
if(iter == std::end(options))
return false;
throw ConversionError("The value " + member + "is not an allowed value for " + simple_name);
else {
member = *iter;
return true;
Expand All @@ -489,16 +494,17 @@ class App {
std::string description,
bool defaulted) {

CLI::callback_t fun = [&member, options](CLI::results_t res) {
std::string simple_name = CLI::detail::split(name, ',').at(0);
CLI::callback_t fun = [&member, options, simple_name](CLI::results_t res) {
if(res.size() != 1) {
return false;
throw ConversionError("Only one " + simple_name + " allowed");
}
member = detail::to_lower(res[0]);
auto iter = std::find_if(std::begin(options), std::end(options), [&member](std::string val) {
return detail::to_lower(val) == member;
});
if(iter == std::end(options))
return false;
throw ConversionError("The value " + member + "is not an allowed value for " + simple_name);
else {
member = *iter;
return true;
Expand All @@ -522,9 +528,11 @@ class App {
std::string description = "",
bool defaulted = false,
std::string label = "COMPLEX") {
CLI::callback_t fun = [&variable](results_t res) {

std::string simple_name = CLI::detail::split(name, ',').at(0);
CLI::callback_t fun = [&variable, simple_name, label](results_t res) {
if(res.size() != 2)
return false;
throw ConversionError(simple_name + " is " + label + " which must have two values");
double x, y;
bool worked = detail::lexical_cast(res[0], x) && detail::lexical_cast(res[1], y);
if(worked)
Expand Down
4 changes: 2 additions & 2 deletions include/CLI/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum class ExitCodes {
Invalid,
Horrible,
OptionNotFound,
BaseClass = 255
BaseClass = 127
};

// Error definitions
Expand Down Expand Up @@ -105,7 +105,7 @@ struct FileError : public ParseError {
FileError(std::string name) : ParseError("FileError", name, ExitCodes::File) {}
};

/// Thrown when conversion call back fails, such as when an int fails to coerse to a string
/// Thrown when conversion call back fails, such as when an int fails to coerce to a string
struct ConversionError : public ParseError {
ConversionError(std::string name) : ParseError("ConversionError", name, ExitCodes::Conversion) {}
};
Expand Down
2 changes: 1 addition & 1 deletion include/CLI/StringTools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace detail {
/// Split a string by a delim
inline std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
// Check to see if emtpy string, give consistent result
// Check to see if empty string, give consistent result
if(s.empty())
elems.emplace_back("");
else {
Expand Down

0 comments on commit 94b3355

Please sign in to comment.