mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-30 04:33:53 +00:00
Moved more string processing to Error
This commit is contained in:
parent
f6c9ce6109
commit
3e651e3b7e
@ -623,7 +623,7 @@ class App {
|
|||||||
for(const App_p &subcomptr : subcommands_)
|
for(const App_p &subcomptr : subcommands_)
|
||||||
if(subcomptr.get() == subcom)
|
if(subcomptr.get() == subcom)
|
||||||
return 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)
|
/// 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_)
|
for(const App_p &subcomptr : subcommands_)
|
||||||
if(subcomptr->check_name(subcom))
|
if(subcomptr->check_name(subcom))
|
||||||
return subcomptr.get();
|
return subcomptr.get();
|
||||||
throw CLI::OptionNotFound(subcom);
|
throw OptionNotFound(subcom);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Changes the group membership
|
/// Changes the group membership
|
||||||
@ -1030,7 +1030,7 @@ class App {
|
|||||||
return opt->get_expected() == -1 && opt->get_positional();
|
return opt->get_expected() == -1 && opt->get_positional();
|
||||||
});
|
});
|
||||||
if(count > 1)
|
if(count > 1)
|
||||||
throw InvalidError(name_ + ": Too many positional arguments with unlimited expected args");
|
throw InvalidError(name_);
|
||||||
for(const App_p &app : subcommands_)
|
for(const App_p &app : subcommands_)
|
||||||
app->_validate();
|
app->_validate();
|
||||||
}
|
}
|
||||||
@ -1173,9 +1173,7 @@ class App {
|
|||||||
if(num_left_over > 0) {
|
if(num_left_over > 0) {
|
||||||
args = remaining(false);
|
args = remaining(false);
|
||||||
std::reverse(std::begin(args), std::end(args));
|
std::reverse(std::begin(args), std::end(args));
|
||||||
throw ExtrasError((args.size() > 1 ? "The following argument was not expected: "
|
throw ExtrasError(args);
|
||||||
: "The following arguments were not expected: ") +
|
|
||||||
detail::rjoin(args, " "));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,17 @@
|
|||||||
// Order is important for combiner script
|
// Order is important for combiner script
|
||||||
|
|
||||||
#include "CLI/Error.hpp"
|
#include "CLI/Error.hpp"
|
||||||
|
|
||||||
#include "CLI/TypeTools.hpp"
|
#include "CLI/TypeTools.hpp"
|
||||||
|
|
||||||
#include "CLI/StringTools.hpp"
|
#include "CLI/StringTools.hpp"
|
||||||
|
|
||||||
#include "CLI/Split.hpp"
|
#include "CLI/Split.hpp"
|
||||||
|
|
||||||
#include "CLI/Ini.hpp"
|
#include "CLI/Ini.hpp"
|
||||||
|
|
||||||
#include "CLI/Validators.hpp"
|
#include "CLI/Validators.hpp"
|
||||||
|
|
||||||
#include "CLI/Option.hpp"
|
#include "CLI/Option.hpp"
|
||||||
|
|
||||||
#include "CLI/App.hpp"
|
#include "CLI/App.hpp"
|
||||||
|
@ -8,6 +8,9 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
// CLI library includes
|
||||||
|
#include "CLI/StringTools.hpp"
|
||||||
|
|
||||||
namespace CLI {
|
namespace CLI {
|
||||||
|
|
||||||
// Use one of these on all error classes
|
// 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
|
/// Thrown when too many positionals or options are found
|
||||||
class ExtrasError : public ParseError {
|
class ExtrasError : public ParseError {
|
||||||
CLI11_ERROR_DEF(ParseError, ExtrasError)
|
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
|
/// Thrown when extra values are found in an INI file
|
||||||
class ExtrasINIError : public ParseError {
|
class ExtrasINIError : public ParseError {
|
||||||
CLI11_ERROR_DEF(ParseError, ExtrasINIError)
|
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
|
/// Thrown when validation fails before parsing
|
||||||
class InvalidError : public ParseError {
|
class InvalidError : public ParseError {
|
||||||
CLI11_ERROR_DEF(ParseError, InvalidError)
|
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
|
/// 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
|
/// Thrown when counting a non-existent option
|
||||||
class OptionNotFound : public Error {
|
class OptionNotFound : public Error {
|
||||||
CLI11_ERROR_DEF(Error, OptionNotFound)
|
CLI11_ERROR_DEF(Error, OptionNotFound)
|
||||||
CLI11_ERROR_SIMPLE(OptionNotFound)
|
OptionNotFound(std::string name) : OptionNotFound(name + " not found", ExitCodes::OptionNotFound) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include <locale>
|
#include <locale>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
namespace CLI {
|
namespace CLI {
|
||||||
|
@ -395,7 +395,7 @@ TEST(Exit, ExitCodes) {
|
|||||||
auto i = static_cast<int>(CLI::ExitCodes::ExtrasError);
|
auto i = static_cast<int>(CLI::ExitCodes::ExtrasError);
|
||||||
EXPECT_EQ(0, app.exit(CLI::Success()));
|
EXPECT_EQ(0, app.exit(CLI::Success()));
|
||||||
EXPECT_EQ(0, app.exit(CLI::CallForHelp()));
|
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(42, app.exit(CLI::RuntimeError(42)));
|
||||||
EXPECT_EQ(1, app.exit(CLI::RuntimeError())); // Not sure if a default here is a good thing
|
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) {
|
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_EQ(out.str(), "");
|
||||||
EXPECT_THAT(err.str(), HasSubstr("for more information"));
|
EXPECT_THAT(err.str(), HasSubstr("for more information"));
|
||||||
EXPECT_THAT(err.str(), Not(HasSubstr("ExtrasError")));
|
EXPECT_THAT(err.str(), Not(HasSubstr("ExtrasError")));
|
||||||
@ -443,7 +443,7 @@ TEST_F(CapturedHelp, NormalError) {
|
|||||||
TEST_F(CapturedHelp, RepacedError) {
|
TEST_F(CapturedHelp, RepacedError) {
|
||||||
app.set_failure_message(CLI::FailureMessage::help);
|
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_EQ(out.str(), "");
|
||||||
EXPECT_THAT(err.str(), Not(HasSubstr("for more information")));
|
EXPECT_THAT(err.str(), Not(HasSubstr("for more information")));
|
||||||
EXPECT_THAT(err.str(), HasSubstr("ERROR: ExtrasError"));
|
EXPECT_THAT(err.str(), HasSubstr("ERROR: ExtrasError"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user