1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Moved more string processing to Error

This commit is contained in:
Henry Fredrick Schreiner 2017-11-26 09:21:01 -05:00 committed by Henry Schreiner
parent f6c9ce6109
commit 3e651e3b7e
5 changed files with 28 additions and 13 deletions

View File

@ -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);
}
}
}

View File

@ -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"

View File

@ -8,6 +8,9 @@
#include <string>
#include <utility>
// 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<std::string> 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) {}
};
/// @}

View File

@ -8,6 +8,7 @@
#include <locale>
#include <sstream>
#include <string>
#include <vector>
#include <type_traits>
namespace CLI {

View File

@ -395,7 +395,7 @@ TEST(Exit, ExitCodes) {
auto i = static_cast<int>(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<int>(CLI::ExitCodes::ExtrasError));
EXPECT_EQ(run(CLI::ExtrasError({"Thing"})), static_cast<int>(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<int>(CLI::ExitCodes::ExtrasError));
EXPECT_EQ(run(CLI::ExtrasError({"Thing"})), static_cast<int>(CLI::ExitCodes::ExtrasError));
EXPECT_EQ(out.str(), "");
EXPECT_THAT(err.str(), Not(HasSubstr("for more information")));
EXPECT_THAT(err.str(), HasSubstr("ERROR: ExtrasError"));