Commit a6621ff8a98b52869690f1e952377b6097410a28

Authored by Henry Fredrick Schreiner
1 parent b6f82d63

Changing ErrorCodes -> ExitCodes

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