Commit a6621ff8a98b52869690f1e952377b6097410a28
1 parent
b6f82d63
Changing ErrorCodes -> ExitCodes
Showing
3 changed files
with
25 additions
and
25 deletions
include/CLI/App.hpp
| ... | ... | @@ -482,7 +482,7 @@ public: |
| 482 | 482 | |
| 483 | 483 | /// Print a nice error message and return the exit code |
| 484 | 484 | int exit(const Error& e) const { |
| 485 | - if(e.exit_code != ErrorCodes::Success) { | |
| 485 | + if(e.exit_code != ExitCodes::Success) { | |
| 486 | 486 | std::cerr << "ERROR: "; |
| 487 | 487 | std::cerr << e.what() << std::endl; |
| 488 | 488 | if(e.print_help) | ... | ... |
include/CLI/Error.hpp
| ... | ... | @@ -9,7 +9,7 @@ |
| 9 | 9 | |
| 10 | 10 | namespace CLI { |
| 11 | 11 | |
| 12 | -enum class ErrorCodes { | |
| 12 | +enum class ExitCodes { | |
| 13 | 13 | Success = 0, |
| 14 | 14 | IncorrectConstruction=100, |
| 15 | 15 | BadNameString, |
| ... | ... | @@ -38,43 +38,43 @@ enum class ErrorCodes { |
| 38 | 38 | |
| 39 | 39 | /// All errors derive from this one |
| 40 | 40 | struct Error : public std::runtime_error { |
| 41 | - ErrorCodes exit_code; | |
| 41 | + ExitCodes exit_code; | |
| 42 | 42 | bool print_help; |
| 43 | 43 | int get_exit_code() const {return static_cast<int>(exit_code);} |
| 44 | - Error(std::string parent, std::string name, ErrorCodes exit_code=ErrorCodes::BaseClass, bool print_help=true) | |
| 44 | + Error(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true) | |
| 45 | 45 | : runtime_error(parent + ": " + name), exit_code(exit_code), print_help(print_help) {} |
| 46 | 46 | }; |
| 47 | 47 | |
| 48 | 48 | /// Construction errors (not in parsing) |
| 49 | 49 | struct ConstructionError : public Error { |
| 50 | 50 | // Using Error::Error constructors seem to not work on GCC 4.7 |
| 51 | - ConstructionError(std::string parent, std::string name, ErrorCodes exit_code=ErrorCodes::BaseClass, bool print_help=true) | |
| 51 | + ConstructionError(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true) | |
| 52 | 52 | : Error(parent, name, exit_code, print_help) {} |
| 53 | 53 | }; |
| 54 | 54 | |
| 55 | 55 | /// Thrown when an option is set to conflicting values (non-vector and multi args, for example) |
| 56 | 56 | struct IncorrectConstruction : public ConstructionError { |
| 57 | 57 | IncorrectConstruction(std::string name) |
| 58 | - : ConstructionError("IncorrectConstruction", name, ErrorCodes::IncorrectConstruction) {} | |
| 58 | + : ConstructionError("IncorrectConstruction", name, ExitCodes::IncorrectConstruction) {} | |
| 59 | 59 | }; |
| 60 | 60 | |
| 61 | 61 | /// Thrown on construction of a bad name |
| 62 | 62 | struct BadNameString : public ConstructionError { |
| 63 | 63 | BadNameString(std::string name) |
| 64 | - : ConstructionError("BadNameString", name, ErrorCodes::BadNameString) {} | |
| 64 | + : ConstructionError("BadNameString", name, ExitCodes::BadNameString) {} | |
| 65 | 65 | }; |
| 66 | 66 | |
| 67 | 67 | /// Thrown when an option already exists |
| 68 | 68 | struct OptionAlreadyAdded : public ConstructionError { |
| 69 | 69 | OptionAlreadyAdded(std::string name) |
| 70 | - : ConstructionError("OptionAlreadyAdded", name, ErrorCodes::OptionAlreadyAdded) {} | |
| 70 | + : ConstructionError("OptionAlreadyAdded", name, ExitCodes::OptionAlreadyAdded) {} | |
| 71 | 71 | }; |
| 72 | 72 | |
| 73 | 73 | // Parsing errors |
| 74 | 74 | |
| 75 | 75 | /// Anything that can error in Parse |
| 76 | 76 | struct ParseError : public Error { |
| 77 | - ParseError(std::string parent, std::string name, ErrorCodes exit_code=ErrorCodes::BaseClass, bool print_help=true) | |
| 77 | + ParseError(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true) | |
| 78 | 78 | : Error(parent, name, exit_code, print_help) {} |
| 79 | 79 | }; |
| 80 | 80 | |
| ... | ... | @@ -83,74 +83,74 @@ struct ParseError : public Error { |
| 83 | 83 | /// This is a successful completion on parsing, supposed to exit |
| 84 | 84 | struct Success : public ParseError { |
| 85 | 85 | Success() |
| 86 | - : ParseError("Success", "Successfully completed, should be caught and quit", ErrorCodes::Success, false) {} | |
| 86 | + : ParseError("Success", "Successfully completed, should be caught and quit", ExitCodes::Success, false) {} | |
| 87 | 87 | }; |
| 88 | 88 | |
| 89 | 89 | /// -h or --help on command line |
| 90 | 90 | struct CallForHelp : public ParseError { |
| 91 | 91 | CallForHelp() |
| 92 | - : ParseError("CallForHelp", "This should be caught in your main function, see examples", ErrorCodes::Success) {} | |
| 92 | + : ParseError("CallForHelp", "This should be caught in your main function, see examples", ExitCodes::Success) {} | |
| 93 | 93 | }; |
| 94 | 94 | |
| 95 | 95 | |
| 96 | 96 | /// Thrown when parsing an INI file and it is missing |
| 97 | 97 | struct FileError : public ParseError { |
| 98 | 98 | FileError (std::string name) |
| 99 | - : ParseError("FileError", name, ErrorCodes::File) {} | |
| 99 | + : ParseError("FileError", name, ExitCodes::File) {} | |
| 100 | 100 | }; |
| 101 | 101 | |
| 102 | 102 | /// Thrown when conversion call back fails, such as when an int fails to coerse to a string |
| 103 | 103 | struct ConversionError : public ParseError { |
| 104 | 104 | ConversionError(std::string name) |
| 105 | - : ParseError("ConversionError", name, ErrorCodes::Conversion) {} | |
| 105 | + : ParseError("ConversionError", name, ExitCodes::Conversion) {} | |
| 106 | 106 | }; |
| 107 | 107 | |
| 108 | 108 | /// Thrown when validation of results fails |
| 109 | 109 | struct ValidationError : public ParseError { |
| 110 | 110 | ValidationError(std::string name) |
| 111 | - : ParseError("ValidationError", name, ErrorCodes::Validation) {} | |
| 111 | + : ParseError("ValidationError", name, ExitCodes::Validation) {} | |
| 112 | 112 | }; |
| 113 | 113 | |
| 114 | 114 | /// Thrown when a required option is missing |
| 115 | 115 | struct RequiredError : public ParseError { |
| 116 | 116 | RequiredError(std::string name) |
| 117 | - : ParseError("RequiredError", name, ErrorCodes::Required) {} | |
| 117 | + : ParseError("RequiredError", name, ExitCodes::Required) {} | |
| 118 | 118 | }; |
| 119 | 119 | |
| 120 | 120 | /// Thrown when a requires option is missing |
| 121 | 121 | struct RequiresError : public ParseError { |
| 122 | 122 | RequiresError(std::string name, std::string subname) |
| 123 | - : ParseError("RequiresError", name + " requires " + subname, ErrorCodes::Requires) {} | |
| 123 | + : ParseError("RequiresError", name + " requires " + subname, ExitCodes::Requires) {} | |
| 124 | 124 | }; |
| 125 | 125 | |
| 126 | 126 | /// Thrown when a exludes option is present |
| 127 | 127 | struct ExcludesError : public ParseError { |
| 128 | 128 | ExcludesError(std::string name, std::string subname) |
| 129 | - : ParseError("ExcludesError", name + " excludes " + subname, ErrorCodes::Excludes) {} | |
| 129 | + : ParseError("ExcludesError", name + " excludes " + subname, ExitCodes::Excludes) {} | |
| 130 | 130 | }; |
| 131 | 131 | |
| 132 | 132 | /// Thrown when too many positionals or options are found |
| 133 | 133 | struct ExtrasError : public ParseError { |
| 134 | 134 | ExtrasError(std::string name) |
| 135 | - : ParseError("ExtrasError", name, ErrorCodes::Extras) {} | |
| 135 | + : ParseError("ExtrasError", name, ExitCodes::Extras) {} | |
| 136 | 136 | }; |
| 137 | 137 | |
| 138 | 138 | /// Thrown when extra values are found in an INI file |
| 139 | 139 | struct ExtrasINIError : public ParseError { |
| 140 | 140 | ExtrasINIError(std::string name) |
| 141 | - : ParseError("ExtrasINIError", name, ErrorCodes::ExtrasINI) {} | |
| 141 | + : ParseError("ExtrasINIError", name, ExitCodes::ExtrasINI) {} | |
| 142 | 142 | }; |
| 143 | 143 | |
| 144 | 144 | /// Thrown when validation fails before parsing |
| 145 | 145 | struct InvalidError : public ParseError { |
| 146 | 146 | InvalidError(std::string name) |
| 147 | - : ParseError("InvalidError", name, ErrorCodes::Invalid) {} | |
| 147 | + : ParseError("InvalidError", name, ExitCodes::Invalid) {} | |
| 148 | 148 | }; |
| 149 | 149 | |
| 150 | 150 | /// This is just a safety check to verify selection and parsing match |
| 151 | 151 | struct HorribleError : public ParseError { |
| 152 | 152 | HorribleError(std::string name) |
| 153 | - : ParseError("HorribleError", "(You should never see this error) " + name, ErrorCodes::Horrible) {} | |
| 153 | + : ParseError("HorribleError", "(You should never see this error) " + name, ExitCodes::Horrible) {} | |
| 154 | 154 | }; |
| 155 | 155 | |
| 156 | 156 | // After parsing |
| ... | ... | @@ -158,7 +158,7 @@ struct HorribleError : public ParseError { |
| 158 | 158 | /// Thrown when counting a non-existent option |
| 159 | 159 | struct OptionNotFound : public Error { |
| 160 | 160 | OptionNotFound(std::string name) |
| 161 | - : Error("OptionNotFound", name, ErrorCodes::OptionNotFound) {} | |
| 161 | + : Error("OptionNotFound", name, ExitCodes::OptionNotFound) {} | |
| 162 | 162 | }; |
| 163 | 163 | |
| 164 | 164 | /// @} | ... | ... |
tests/HelpTest.cpp
| ... | ... | @@ -265,7 +265,7 @@ TEST(Exit, ErrorWithHelp) { |
| 265 | 265 | try { |
| 266 | 266 | app.parse(input); |
| 267 | 267 | } catch (const CLI::CallForHelp &e) { |
| 268 | - EXPECT_EQ(CLI::ErrorCodes::Success, e.exit_code); | |
| 268 | + EXPECT_EQ(CLI::ExitCodes::Success, e.exit_code); | |
| 269 | 269 | } |
| 270 | 270 | } |
| 271 | 271 | |
| ... | ... | @@ -276,14 +276,14 @@ TEST(Exit, ErrorWithoutHelp) { |
| 276 | 276 | try { |
| 277 | 277 | app.parse(input); |
| 278 | 278 | } catch (const CLI::ParseError &e) { |
| 279 | - EXPECT_EQ(CLI::ErrorCodes::Extras, e.exit_code); | |
| 279 | + EXPECT_EQ(CLI::ExitCodes::Extras, e.exit_code); | |
| 280 | 280 | } |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | 283 | TEST(Exit, ExitCodes) { |
| 284 | 284 | CLI::App app; |
| 285 | 285 | |
| 286 | - int i = static_cast<int>(CLI::ErrorCodes::Extras); | |
| 286 | + int i = static_cast<int>(CLI::ExitCodes::Extras); | |
| 287 | 287 | EXPECT_EQ(0, app.exit(CLI::Success())); |
| 288 | 288 | EXPECT_EQ(0, app.exit(CLI::CallForHelp())); |
| 289 | 289 | EXPECT_EQ(i, app.exit(CLI::ExtrasError("Thing"))); | ... | ... |