diff --git a/application/argumentparser.cpp b/application/argumentparser.cpp index f176c9f..5de11a4 100644 --- a/application/argumentparser.cpp +++ b/application/argumentparser.cpp @@ -684,6 +684,24 @@ void ArgumentParser::parseArgs(int argc, const char *const *argv) } } +/*! + * \brief Parses the specified command line arguments. + * \remarks The same as parseArgs(), except that this method will not throw an exception in the error + * case. Instead, it will print an error message and terminate the application with exit + * code 1. + * \sa parseArgs(), readArgs() + */ +void ArgumentParser::parseArgsOrExit(int argc, const char *const *argv) +{ + try { + parseArgs(argc, argv); + } catch (const Failure &failure) { + CMD_UTILS_START_CONSOLE; + cerr << failure; + exit(1); + } +} + /*! * \brief Parses the specified command line arguments. * \remarks diff --git a/application/argumentparser.h b/application/argumentparser.h index 39b262d..f367e71 100644 --- a/application/argumentparser.h +++ b/application/argumentparser.h @@ -243,6 +243,7 @@ public: void addMainArgument(Argument *argument); void printHelp(std::ostream &os) const; void parseArgs(int argc, const char *const *argv); + void parseArgsOrExit(int argc, const char *const *argv); void readArgs(int argc, const char *const *argv); void resetArgs(); unsigned int actualArgumentCount() const; diff --git a/application/failure.cpp b/application/failure.cpp index cee1799..5f9b7b5 100644 --- a/application/failure.cpp +++ b/application/failure.cpp @@ -1,5 +1,9 @@ #include "./failure.h" +#include "../io/ansiescapecodes.h" + +#include + namespace ApplicationUtilities { /*! @@ -41,4 +45,16 @@ const char *Failure::what() const USE_NOTHROW { return m_what.c_str(); } + +/*! + * \brief Prints an error message "Unable to parse arguments: ..." for the specified \a failure. + */ +std::ostream &operator<<(std::ostream &o, const Failure &failure) +{ + using namespace std; + using namespace EscapeCodes; + return o << Phrases::Error << "Unable to parse arguments: " << TextAttribute::Reset << failure.what() << "\nSee --help for available commands." + << endl; +} + } // namespace ApplicationUtilities diff --git a/application/failure.h b/application/failure.h index 3098698..6001fde 100644 --- a/application/failure.h +++ b/application/failure.h @@ -4,6 +4,7 @@ #include "../global.h" #include +#include #include namespace ApplicationUtilities { @@ -19,6 +20,9 @@ public: private: std::string m_what; }; + +std::ostream &operator<<(std::ostream &o, const Failure &failure); + } // namespace ApplicationUtilities #endif // APPLICATION_UTILITIES_FAILURE_H