1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-08 07:43:52 +00:00

Changing ErrorCodes -> ExitCodes

This commit is contained in:
Henry Fredrick Schreiner 2017-03-17 14:45:04 -04:00
parent b6f82d63ec
commit a6621ff8a9
3 changed files with 25 additions and 25 deletions

View File

@ -482,7 +482,7 @@ public:
/// Print a nice error message and return the exit code
int exit(const Error& e) const {
if(e.exit_code != ErrorCodes::Success) {
if(e.exit_code != ExitCodes::Success) {
std::cerr << "ERROR: ";
std::cerr << e.what() << std::endl;
if(e.print_help)

View File

@ -9,7 +9,7 @@
namespace CLI {
enum class ErrorCodes {
enum class ExitCodes {
Success = 0,
IncorrectConstruction=100,
BadNameString,
@ -38,43 +38,43 @@ enum class ErrorCodes {
/// All errors derive from this one
struct Error : public std::runtime_error {
ErrorCodes exit_code;
ExitCodes exit_code;
bool print_help;
int get_exit_code() const {return static_cast<int>(exit_code);}
Error(std::string parent, std::string name, ErrorCodes exit_code=ErrorCodes::BaseClass, bool print_help=true)
Error(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true)
: runtime_error(parent + ": " + name), exit_code(exit_code), print_help(print_help) {}
};
/// Construction errors (not in parsing)
struct ConstructionError : public Error {
// Using Error::Error constructors seem to not work on GCC 4.7
ConstructionError(std::string parent, std::string name, ErrorCodes exit_code=ErrorCodes::BaseClass, bool print_help=true)
ConstructionError(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true)
: Error(parent, name, exit_code, print_help) {}
};
/// Thrown when an option is set to conflicting values (non-vector and multi args, for example)
struct IncorrectConstruction : public ConstructionError {
IncorrectConstruction(std::string name)
: ConstructionError("IncorrectConstruction", name, ErrorCodes::IncorrectConstruction) {}
: ConstructionError("IncorrectConstruction", name, ExitCodes::IncorrectConstruction) {}
};
/// Thrown on construction of a bad name
struct BadNameString : public ConstructionError {
BadNameString(std::string name)
: ConstructionError("BadNameString", name, ErrorCodes::BadNameString) {}
: ConstructionError("BadNameString", name, ExitCodes::BadNameString) {}
};
/// Thrown when an option already exists
struct OptionAlreadyAdded : public ConstructionError {
OptionAlreadyAdded(std::string name)
: ConstructionError("OptionAlreadyAdded", name, ErrorCodes::OptionAlreadyAdded) {}
: ConstructionError("OptionAlreadyAdded", name, ExitCodes::OptionAlreadyAdded) {}
};
// Parsing errors
/// Anything that can error in Parse
struct ParseError : public Error {
ParseError(std::string parent, std::string name, ErrorCodes exit_code=ErrorCodes::BaseClass, bool print_help=true)
ParseError(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true)
: Error(parent, name, exit_code, print_help) {}
};
@ -83,74 +83,74 @@ struct ParseError : public Error {
/// This is a successful completion on parsing, supposed to exit
struct Success : public ParseError {
Success()
: ParseError("Success", "Successfully completed, should be caught and quit", ErrorCodes::Success, false) {}
: ParseError("Success", "Successfully completed, should be caught and quit", ExitCodes::Success, false) {}
};
/// -h or --help on command line
struct CallForHelp : public ParseError {
CallForHelp()
: ParseError("CallForHelp", "This should be caught in your main function, see examples", ErrorCodes::Success) {}
: ParseError("CallForHelp", "This should be caught in your main function, see examples", ExitCodes::Success) {}
};
/// Thrown when parsing an INI file and it is missing
struct FileError : public ParseError {
FileError (std::string name)
: ParseError("FileError", name, ErrorCodes::File) {}
: ParseError("FileError", name, ExitCodes::File) {}
};
/// Thrown when conversion call back fails, such as when an int fails to coerse to a string
struct ConversionError : public ParseError {
ConversionError(std::string name)
: ParseError("ConversionError", name, ErrorCodes::Conversion) {}
: ParseError("ConversionError", name, ExitCodes::Conversion) {}
};
/// Thrown when validation of results fails
struct ValidationError : public ParseError {
ValidationError(std::string name)
: ParseError("ValidationError", name, ErrorCodes::Validation) {}
: ParseError("ValidationError", name, ExitCodes::Validation) {}
};
/// Thrown when a required option is missing
struct RequiredError : public ParseError {
RequiredError(std::string name)
: ParseError("RequiredError", name, ErrorCodes::Required) {}
: ParseError("RequiredError", name, ExitCodes::Required) {}
};
/// Thrown when a requires option is missing
struct RequiresError : public ParseError {
RequiresError(std::string name, std::string subname)
: ParseError("RequiresError", name + " requires " + subname, ErrorCodes::Requires) {}
: ParseError("RequiresError", name + " requires " + subname, ExitCodes::Requires) {}
};
/// Thrown when a exludes option is present
struct ExcludesError : public ParseError {
ExcludesError(std::string name, std::string subname)
: ParseError("ExcludesError", name + " excludes " + subname, ErrorCodes::Excludes) {}
: ParseError("ExcludesError", name + " excludes " + subname, ExitCodes::Excludes) {}
};
/// Thrown when too many positionals or options are found
struct ExtrasError : public ParseError {
ExtrasError(std::string name)
: ParseError("ExtrasError", name, ErrorCodes::Extras) {}
: ParseError("ExtrasError", name, ExitCodes::Extras) {}
};
/// Thrown when extra values are found in an INI file
struct ExtrasINIError : public ParseError {
ExtrasINIError(std::string name)
: ParseError("ExtrasINIError", name, ErrorCodes::ExtrasINI) {}
: ParseError("ExtrasINIError", name, ExitCodes::ExtrasINI) {}
};
/// Thrown when validation fails before parsing
struct InvalidError : public ParseError {
InvalidError(std::string name)
: ParseError("InvalidError", name, ErrorCodes::Invalid) {}
: ParseError("InvalidError", name, ExitCodes::Invalid) {}
};
/// This is just a safety check to verify selection and parsing match
struct HorribleError : public ParseError {
HorribleError(std::string name)
: ParseError("HorribleError", "(You should never see this error) " + name, ErrorCodes::Horrible) {}
: ParseError("HorribleError", "(You should never see this error) " + name, ExitCodes::Horrible) {}
};
// After parsing
@ -158,7 +158,7 @@ struct HorribleError : public ParseError {
/// Thrown when counting a non-existent option
struct OptionNotFound : public Error {
OptionNotFound(std::string name)
: Error("OptionNotFound", name, ErrorCodes::OptionNotFound) {}
: Error("OptionNotFound", name, ExitCodes::OptionNotFound) {}
};
/// @}

View File

@ -265,7 +265,7 @@ TEST(Exit, ErrorWithHelp) {
try {
app.parse(input);
} catch (const CLI::CallForHelp &e) {
EXPECT_EQ(CLI::ErrorCodes::Success, e.exit_code);
EXPECT_EQ(CLI::ExitCodes::Success, e.exit_code);
}
}
@ -276,14 +276,14 @@ TEST(Exit, ErrorWithoutHelp) {
try {
app.parse(input);
} catch (const CLI::ParseError &e) {
EXPECT_EQ(CLI::ErrorCodes::Extras, e.exit_code);
EXPECT_EQ(CLI::ExitCodes::Extras, e.exit_code);
}
}
TEST(Exit, ExitCodes) {
CLI::App app;
int i = static_cast<int>(CLI::ErrorCodes::Extras);
int i = static_cast<int>(CLI::ExitCodes::Extras);
EXPECT_EQ(0, app.exit(CLI::Success()));
EXPECT_EQ(0, app.exit(CLI::CallForHelp()));
EXPECT_EQ(i, app.exit(CLI::ExtrasError("Thing")));