From 3e651e3b7e67dca9d5725ad2673705b41afd59ab Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Sun, 26 Nov 2017 09:21:01 -0500 Subject: [PATCH] Moved more string processing to Error --- include/CLI/App.hpp | 10 ++++------ include/CLI/CLI.hpp | 7 +++++++ include/CLI/Error.hpp | 17 +++++++++++++---- include/CLI/StringTools.hpp | 1 + tests/HelpTest.cpp | 6 +++--- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index e7e0a6f3..53952285 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -623,7 +623,7 @@ class App { for(const App_p &subcomptr : subcommands_) if(subcomptr.get() == subcom) return subcom; - throw CLI::OptionNotFound(subcom->get_name()); + throw OptionNotFound(subcom->get_name()); } /// Check to see if a subcommand is part of this command (text version) @@ -631,7 +631,7 @@ class App { for(const App_p &subcomptr : subcommands_) if(subcomptr->check_name(subcom)) return subcomptr.get(); - throw CLI::OptionNotFound(subcom); + throw OptionNotFound(subcom); } /// Changes the group membership @@ -1030,7 +1030,7 @@ class App { return opt->get_expected() == -1 && opt->get_positional(); }); if(count > 1) - throw InvalidError(name_ + ": Too many positional arguments with unlimited expected args"); + throw InvalidError(name_); for(const App_p &app : subcommands_) app->_validate(); } @@ -1173,9 +1173,7 @@ class App { if(num_left_over > 0) { args = remaining(false); std::reverse(std::begin(args), std::end(args)); - throw ExtrasError((args.size() > 1 ? "The following argument was not expected: " - : "The following arguments were not expected: ") + - detail::rjoin(args, " ")); + throw ExtrasError(args); } } } diff --git a/include/CLI/CLI.hpp b/include/CLI/CLI.hpp index 2ef44c14..a5613629 100644 --- a/include/CLI/CLI.hpp +++ b/include/CLI/CLI.hpp @@ -7,10 +7,17 @@ // Order is important for combiner script #include "CLI/Error.hpp" + #include "CLI/TypeTools.hpp" + #include "CLI/StringTools.hpp" + #include "CLI/Split.hpp" + #include "CLI/Ini.hpp" + #include "CLI/Validators.hpp" + #include "CLI/Option.hpp" + #include "CLI/App.hpp" diff --git a/include/CLI/Error.hpp b/include/CLI/Error.hpp index 8916381c..99eb014b 100644 --- a/include/CLI/Error.hpp +++ b/include/CLI/Error.hpp @@ -8,6 +8,9 @@ #include #include +// CLI library includes +#include "CLI/StringTools.hpp" + namespace CLI { // Use one of these on all error classes @@ -179,19 +182,25 @@ class ExcludesError : public ParseError { /// Thrown when too many positionals or options are found class ExtrasError : public ParseError { CLI11_ERROR_DEF(ParseError, ExtrasError) - CLI11_ERROR_SIMPLE(ExtrasError) + ExtrasError(std::vector args) + : ExtrasError((args.size() > 1 ? "The following argument was not expected: " + : "The following arguments were not expected: ") + + detail::rjoin(args, " "), + ExitCodes::ExtrasError) {} }; /// Thrown when extra values are found in an INI file class ExtrasINIError : public ParseError { CLI11_ERROR_DEF(ParseError, ExtrasINIError) - CLI11_ERROR_SIMPLE(ExtrasINIError) + ExtrasINIError(std::string item) : ExtrasINIError("INI was not able to parse " + item, ExitCodes::ExtrasINIError) {} }; /// Thrown when validation fails before parsing class InvalidError : public ParseError { CLI11_ERROR_DEF(ParseError, InvalidError) - CLI11_ERROR_SIMPLE(InvalidError) + InvalidError(std::string name) + : InvalidError(name + ": Too many positional arguments with unlimited expected args", ExitCodes::InvalidError) { + } }; /// This is just a safety check to verify selection and parsing match - you should not ever see it @@ -205,7 +214,7 @@ class HorribleError : public ParseError { /// Thrown when counting a non-existent option class OptionNotFound : public Error { CLI11_ERROR_DEF(Error, OptionNotFound) - CLI11_ERROR_SIMPLE(OptionNotFound) + OptionNotFound(std::string name) : OptionNotFound(name + " not found", ExitCodes::OptionNotFound) {} }; /// @} diff --git a/include/CLI/StringTools.hpp b/include/CLI/StringTools.hpp index 3fff1449..64b8603f 100644 --- a/include/CLI/StringTools.hpp +++ b/include/CLI/StringTools.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include namespace CLI { diff --git a/tests/HelpTest.cpp b/tests/HelpTest.cpp index 9c43af88..4437de5c 100644 --- a/tests/HelpTest.cpp +++ b/tests/HelpTest.cpp @@ -395,7 +395,7 @@ TEST(Exit, ExitCodes) { auto i = static_cast(CLI::ExitCodes::ExtrasError); EXPECT_EQ(0, app.exit(CLI::Success())); EXPECT_EQ(0, app.exit(CLI::CallForHelp())); - EXPECT_EQ(i, app.exit(CLI::ExtrasError("Thing"))); + EXPECT_EQ(i, app.exit(CLI::ExtrasError({"Thing"}))); EXPECT_EQ(42, app.exit(CLI::RuntimeError(42))); EXPECT_EQ(1, app.exit(CLI::RuntimeError())); // Not sure if a default here is a good thing } @@ -432,7 +432,7 @@ TEST_F(CapturedHelp, CallForHelp) { } TEST_F(CapturedHelp, NormalError) { - EXPECT_EQ(run(CLI::ExtrasError("Thing")), static_cast(CLI::ExitCodes::ExtrasError)); + EXPECT_EQ(run(CLI::ExtrasError({"Thing"})), static_cast(CLI::ExitCodes::ExtrasError)); EXPECT_EQ(out.str(), ""); EXPECT_THAT(err.str(), HasSubstr("for more information")); EXPECT_THAT(err.str(), Not(HasSubstr("ExtrasError"))); @@ -443,7 +443,7 @@ TEST_F(CapturedHelp, NormalError) { TEST_F(CapturedHelp, RepacedError) { app.set_failure_message(CLI::FailureMessage::help); - EXPECT_EQ(run(CLI::ExtrasError("Thing")), static_cast(CLI::ExitCodes::ExtrasError)); + EXPECT_EQ(run(CLI::ExtrasError({"Thing"})), static_cast(CLI::ExitCodes::ExtrasError)); EXPECT_EQ(out.str(), ""); EXPECT_THAT(err.str(), Not(HasSubstr("for more information"))); EXPECT_THAT(err.str(), HasSubstr("ERROR: ExtrasError"));