Commit 3e651e3b7e67dca9d5725ad2673705b41afd59ab

Authored by Henry Fredrick Schreiner
Committed by Henry Schreiner
1 parent f6c9ce61

Moved more string processing to Error

include/CLI/App.hpp
... ... @@ -623,7 +623,7 @@ class App {
623 623 for(const App_p &subcomptr : subcommands_)
624 624 if(subcomptr.get() == subcom)
625 625 return subcom;
626   - throw CLI::OptionNotFound(subcom->get_name());
  626 + throw OptionNotFound(subcom->get_name());
627 627 }
628 628  
629 629 /// Check to see if a subcommand is part of this command (text version)
... ... @@ -631,7 +631,7 @@ class App {
631 631 for(const App_p &subcomptr : subcommands_)
632 632 if(subcomptr->check_name(subcom))
633 633 return subcomptr.get();
634   - throw CLI::OptionNotFound(subcom);
  634 + throw OptionNotFound(subcom);
635 635 }
636 636  
637 637 /// Changes the group membership
... ... @@ -1030,7 +1030,7 @@ class App {
1030 1030 return opt->get_expected() == -1 && opt->get_positional();
1031 1031 });
1032 1032 if(count > 1)
1033   - throw InvalidError(name_ + ": Too many positional arguments with unlimited expected args");
  1033 + throw InvalidError(name_);
1034 1034 for(const App_p &app : subcommands_)
1035 1035 app->_validate();
1036 1036 }
... ... @@ -1173,9 +1173,7 @@ class App {
1173 1173 if(num_left_over > 0) {
1174 1174 args = remaining(false);
1175 1175 std::reverse(std::begin(args), std::end(args));
1176   - throw ExtrasError((args.size() > 1 ? "The following argument was not expected: "
1177   - : "The following arguments were not expected: ") +
1178   - detail::rjoin(args, " "));
  1176 + throw ExtrasError(args);
1179 1177 }
1180 1178 }
1181 1179 }
... ...
include/CLI/CLI.hpp
... ... @@ -7,10 +7,17 @@
7 7 // Order is important for combiner script
8 8  
9 9 #include "CLI/Error.hpp"
  10 +
10 11 #include "CLI/TypeTools.hpp"
  12 +
11 13 #include "CLI/StringTools.hpp"
  14 +
12 15 #include "CLI/Split.hpp"
  16 +
13 17 #include "CLI/Ini.hpp"
  18 +
14 19 #include "CLI/Validators.hpp"
  20 +
15 21 #include "CLI/Option.hpp"
  22 +
16 23 #include "CLI/App.hpp"
... ...
include/CLI/Error.hpp
... ... @@ -8,6 +8,9 @@
8 8 #include <string>
9 9 #include <utility>
10 10  
  11 +// CLI library includes
  12 +#include "CLI/StringTools.hpp"
  13 +
11 14 namespace CLI {
12 15  
13 16 // Use one of these on all error classes
... ... @@ -179,19 +182,25 @@ class ExcludesError : public ParseError {
179 182 /// Thrown when too many positionals or options are found
180 183 class ExtrasError : public ParseError {
181 184 CLI11_ERROR_DEF(ParseError, ExtrasError)
182   - CLI11_ERROR_SIMPLE(ExtrasError)
  185 + ExtrasError(std::vector<std::string> args)
  186 + : ExtrasError((args.size() > 1 ? "The following argument was not expected: "
  187 + : "The following arguments were not expected: ") +
  188 + detail::rjoin(args, " "),
  189 + ExitCodes::ExtrasError) {}
183 190 };
184 191  
185 192 /// Thrown when extra values are found in an INI file
186 193 class ExtrasINIError : public ParseError {
187 194 CLI11_ERROR_DEF(ParseError, ExtrasINIError)
188   - CLI11_ERROR_SIMPLE(ExtrasINIError)
  195 + ExtrasINIError(std::string item) : ExtrasINIError("INI was not able to parse " + item, ExitCodes::ExtrasINIError) {}
189 196 };
190 197  
191 198 /// Thrown when validation fails before parsing
192 199 class InvalidError : public ParseError {
193 200 CLI11_ERROR_DEF(ParseError, InvalidError)
194   - CLI11_ERROR_SIMPLE(InvalidError)
  201 + InvalidError(std::string name)
  202 + : InvalidError(name + ": Too many positional arguments with unlimited expected args", ExitCodes::InvalidError) {
  203 + }
195 204 };
196 205  
197 206 /// 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 {
205 214 /// Thrown when counting a non-existent option
206 215 class OptionNotFound : public Error {
207 216 CLI11_ERROR_DEF(Error, OptionNotFound)
208   - CLI11_ERROR_SIMPLE(OptionNotFound)
  217 + OptionNotFound(std::string name) : OptionNotFound(name + " not found", ExitCodes::OptionNotFound) {}
209 218 };
210 219  
211 220 /// @}
... ...
include/CLI/StringTools.hpp
... ... @@ -8,6 +8,7 @@
8 8 #include <locale>
9 9 #include <sstream>
10 10 #include <string>
  11 +#include <vector>
11 12 #include <type_traits>
12 13  
13 14 namespace CLI {
... ...
tests/HelpTest.cpp
... ... @@ -395,7 +395,7 @@ TEST(Exit, ExitCodes) {
395 395 auto i = static_cast<int>(CLI::ExitCodes::ExtrasError);
396 396 EXPECT_EQ(0, app.exit(CLI::Success()));
397 397 EXPECT_EQ(0, app.exit(CLI::CallForHelp()));
398   - EXPECT_EQ(i, app.exit(CLI::ExtrasError("Thing")));
  398 + EXPECT_EQ(i, app.exit(CLI::ExtrasError({"Thing"})));
399 399 EXPECT_EQ(42, app.exit(CLI::RuntimeError(42)));
400 400 EXPECT_EQ(1, app.exit(CLI::RuntimeError())); // Not sure if a default here is a good thing
401 401 }
... ... @@ -432,7 +432,7 @@ TEST_F(CapturedHelp, CallForHelp) {
432 432 }
433 433  
434 434 TEST_F(CapturedHelp, NormalError) {
435   - EXPECT_EQ(run(CLI::ExtrasError("Thing")), static_cast<int>(CLI::ExitCodes::ExtrasError));
  435 + EXPECT_EQ(run(CLI::ExtrasError({"Thing"})), static_cast<int>(CLI::ExitCodes::ExtrasError));
436 436 EXPECT_EQ(out.str(), "");
437 437 EXPECT_THAT(err.str(), HasSubstr("for more information"));
438 438 EXPECT_THAT(err.str(), Not(HasSubstr("ExtrasError")));
... ... @@ -443,7 +443,7 @@ TEST_F(CapturedHelp, NormalError) {
443 443 TEST_F(CapturedHelp, RepacedError) {
444 444 app.set_failure_message(CLI::FailureMessage::help);
445 445  
446   - EXPECT_EQ(run(CLI::ExtrasError("Thing")), static_cast<int>(CLI::ExitCodes::ExtrasError));
  446 + EXPECT_EQ(run(CLI::ExtrasError({"Thing"})), static_cast<int>(CLI::ExitCodes::ExtrasError));
447 447 EXPECT_EQ(out.str(), "");
448 448 EXPECT_THAT(err.str(), Not(HasSubstr("for more information")));
449 449 EXPECT_THAT(err.str(), HasSubstr("ERROR: ExtrasError"));
... ...