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,7 +623,7 @@ class App {
623 for(const App_p &subcomptr : subcommands_) 623 for(const App_p &subcomptr : subcommands_)
624 if(subcomptr.get() == subcom) 624 if(subcomptr.get() == subcom)
625 return subcom; 625 return subcom;
626 - throw CLI::OptionNotFound(subcom->get_name()); 626 + throw OptionNotFound(subcom->get_name());
627 } 627 }
628 628
629 /// Check to see if a subcommand is part of this command (text version) 629 /// Check to see if a subcommand is part of this command (text version)
@@ -631,7 +631,7 @@ class App { @@ -631,7 +631,7 @@ class App {
631 for(const App_p &subcomptr : subcommands_) 631 for(const App_p &subcomptr : subcommands_)
632 if(subcomptr->check_name(subcom)) 632 if(subcomptr->check_name(subcom))
633 return subcomptr.get(); 633 return subcomptr.get();
634 - throw CLI::OptionNotFound(subcom); 634 + throw OptionNotFound(subcom);
635 } 635 }
636 636
637 /// Changes the group membership 637 /// Changes the group membership
@@ -1030,7 +1030,7 @@ class App { @@ -1030,7 +1030,7 @@ class App {
1030 return opt->get_expected() == -1 && opt->get_positional(); 1030 return opt->get_expected() == -1 && opt->get_positional();
1031 }); 1031 });
1032 if(count > 1) 1032 if(count > 1)
1033 - throw InvalidError(name_ + ": Too many positional arguments with unlimited expected args"); 1033 + throw InvalidError(name_);
1034 for(const App_p &app : subcommands_) 1034 for(const App_p &app : subcommands_)
1035 app->_validate(); 1035 app->_validate();
1036 } 1036 }
@@ -1173,9 +1173,7 @@ class App { @@ -1173,9 +1173,7 @@ class App {
1173 if(num_left_over > 0) { 1173 if(num_left_over > 0) {
1174 args = remaining(false); 1174 args = remaining(false);
1175 std::reverse(std::begin(args), std::end(args)); 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,10 +7,17 @@
7 // Order is important for combiner script 7 // Order is important for combiner script
8 8
9 #include "CLI/Error.hpp" 9 #include "CLI/Error.hpp"
  10 +
10 #include "CLI/TypeTools.hpp" 11 #include "CLI/TypeTools.hpp"
  12 +
11 #include "CLI/StringTools.hpp" 13 #include "CLI/StringTools.hpp"
  14 +
12 #include "CLI/Split.hpp" 15 #include "CLI/Split.hpp"
  16 +
13 #include "CLI/Ini.hpp" 17 #include "CLI/Ini.hpp"
  18 +
14 #include "CLI/Validators.hpp" 19 #include "CLI/Validators.hpp"
  20 +
15 #include "CLI/Option.hpp" 21 #include "CLI/Option.hpp"
  22 +
16 #include "CLI/App.hpp" 23 #include "CLI/App.hpp"
include/CLI/Error.hpp
@@ -8,6 +8,9 @@ @@ -8,6 +8,9 @@
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
  11 +// CLI library includes
  12 +#include "CLI/StringTools.hpp"
  13 +
11 namespace CLI { 14 namespace CLI {
12 15
13 // Use one of these on all error classes 16 // Use one of these on all error classes
@@ -179,19 +182,25 @@ class ExcludesError : public ParseError { @@ -179,19 +182,25 @@ class ExcludesError : public ParseError {
179 /// Thrown when too many positionals or options are found 182 /// Thrown when too many positionals or options are found
180 class ExtrasError : public ParseError { 183 class ExtrasError : public ParseError {
181 CLI11_ERROR_DEF(ParseError, ExtrasError) 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 /// Thrown when extra values are found in an INI file 192 /// Thrown when extra values are found in an INI file
186 class ExtrasINIError : public ParseError { 193 class ExtrasINIError : public ParseError {
187 CLI11_ERROR_DEF(ParseError, ExtrasINIError) 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 /// Thrown when validation fails before parsing 198 /// Thrown when validation fails before parsing
192 class InvalidError : public ParseError { 199 class InvalidError : public ParseError {
193 CLI11_ERROR_DEF(ParseError, InvalidError) 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 /// This is just a safety check to verify selection and parsing match - you should not ever see it 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,7 +214,7 @@ class HorribleError : public ParseError {
205 /// Thrown when counting a non-existent option 214 /// Thrown when counting a non-existent option
206 class OptionNotFound : public Error { 215 class OptionNotFound : public Error {
207 CLI11_ERROR_DEF(Error, OptionNotFound) 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,6 +8,7 @@
8 #include <locale> 8 #include <locale>
9 #include <sstream> 9 #include <sstream>
10 #include <string> 10 #include <string>
  11 +#include <vector>
11 #include <type_traits> 12 #include <type_traits>
12 13
13 namespace CLI { 14 namespace CLI {
tests/HelpTest.cpp
@@ -395,7 +395,7 @@ TEST(Exit, ExitCodes) { @@ -395,7 +395,7 @@ TEST(Exit, ExitCodes) {
395 auto i = static_cast<int>(CLI::ExitCodes::ExtrasError); 395 auto i = static_cast<int>(CLI::ExitCodes::ExtrasError);
396 EXPECT_EQ(0, app.exit(CLI::Success())); 396 EXPECT_EQ(0, app.exit(CLI::Success()));
397 EXPECT_EQ(0, app.exit(CLI::CallForHelp())); 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 EXPECT_EQ(42, app.exit(CLI::RuntimeError(42))); 399 EXPECT_EQ(42, app.exit(CLI::RuntimeError(42)));
400 EXPECT_EQ(1, app.exit(CLI::RuntimeError())); // Not sure if a default here is a good thing 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,7 +432,7 @@ TEST_F(CapturedHelp, CallForHelp) {
432 } 432 }
433 433
434 TEST_F(CapturedHelp, NormalError) { 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 EXPECT_EQ(out.str(), ""); 436 EXPECT_EQ(out.str(), "");
437 EXPECT_THAT(err.str(), HasSubstr("for more information")); 437 EXPECT_THAT(err.str(), HasSubstr("for more information"));
438 EXPECT_THAT(err.str(), Not(HasSubstr("ExtrasError"))); 438 EXPECT_THAT(err.str(), Not(HasSubstr("ExtrasError")));
@@ -443,7 +443,7 @@ TEST_F(CapturedHelp, NormalError) { @@ -443,7 +443,7 @@ TEST_F(CapturedHelp, NormalError) {
443 TEST_F(CapturedHelp, RepacedError) { 443 TEST_F(CapturedHelp, RepacedError) {
444 app.set_failure_message(CLI::FailureMessage::help); 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 EXPECT_EQ(out.str(), ""); 447 EXPECT_EQ(out.str(), "");
448 EXPECT_THAT(err.str(), Not(HasSubstr("for more information"))); 448 EXPECT_THAT(err.str(), Not(HasSubstr("for more information")));
449 EXPECT_THAT(err.str(), HasSubstr("ERROR: ExtrasError")); 449 EXPECT_THAT(err.str(), HasSubstr("ERROR: ExtrasError"));