From c2eeec205dbf17f6ca38fda2671c6aaf2d1c505f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 25 May 2011 21:35:59 +0300 Subject: [PATCH] Make help message printing automatic Start making more use of std exception classes --- source/core/application.cpp | 39 +++++--------------------- source/core/application.h | 3 -- source/core/getopt.cpp | 56 ++++++++++++++++++++++++------------- source/core/getopt.h | 24 +++++++++++++--- 4 files changed, 63 insertions(+), 59 deletions(-) diff --git a/source/core/application.cpp b/source/core/application.cpp index 139f94f..a60f605 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -8,12 +8,9 @@ Distributed under the LGPL #include #include #include -#include "../debug/backtrace.h" #include "../debug/demangle.h" -#include "../time/units.h" -#include "../time/utils.h" #include "application.h" -#include "except.h" +#include "getopt.h" using namespace std; @@ -29,7 +26,8 @@ Application::Application(): /** Constructs an instance of the registered application class and runs it. If the -application throws a UsageError, the static usage() function is called. +application throws a usage_error, a help message is printed. The GetOpt class +will throw such exceptions automatically in error conditions. This function can only be called once. The global main() function provided by the library normally does it automatically at program startup. @@ -58,9 +56,10 @@ int Application::run(int argc, char **argv, void *data) { app_ = starter_->create_app(argc, argv); } - catch(const UsageError &e) + catch(const usage_error &e) { - starter_->usage(e.what(), argv[0], e.get_brief()); + cerr<(&e); - if(exc && !exc->get_backtrace().get_frames().empty()) - { - cerr<<" backtrace:\n"; - const list &frames = exc->get_backtrace().get_frames(); - for(list::const_iterator i=frames.begin(); i!=frames.end(); ++i) - cerr<<" "<<*i<<'\n'; - } #endif return 124; } } -/** -Prints a message describing the usage of the application. The default version -will blame the programmer for being lazy. - -@param reason Why the function was called -@param argv0 The value of argv[0], to be used in the message -@param brief Whether to print a brief or long usage message -*/ -void Application::usage(const char *reason, const char *, bool) -{ - if(reason) - cerr<<"UsageError: "<::iterator i=opts.begin(); i!=opts.end(); ++i) @@ -22,7 +28,7 @@ GetOpt::OptBase &GetOpt::get_option(char s) for(list::iterator i=opts.begin(); i!=opts.end(); ++i) if((*i)->get_short()==s) return **i; - throw UsageError(string("Unknown option -")+s); + throw usage_error(string("Unknown option -")+s); } GetOpt::OptBase &GetOpt::get_option(const string &l) @@ -30,32 +36,42 @@ GetOpt::OptBase &GetOpt::get_option(const string &l) for(list::iterator i=opts.begin(); i!=opts.end(); ++i) if((*i)->get_long()==l) return **i; - throw UsageError(string("Unknown option --")+l); + throw usage_error(string("Unknown option --")+l); } void GetOpt::operator()(unsigned argc, const char *const *argv) { - unsigned i = 1; - for(; i #include +#include #include #include -#include "except.h" namespace Msp { +class usage_error: public std::runtime_error +{ +private: + std::string help_; + +public: + usage_error(const std::string &w, const std::string &h = std::string()): std::runtime_error(w), help_(h) { } + ~usage_error() throw() { } + + const char *help() const throw() { return help_.c_str(); } +}; + + class GetOpt { public: @@ -69,7 +83,7 @@ private: std::istringstream ss(a); ss>>tmp; if(ss.fail()) - throw UsageError("Invalid argument for --"+lng); + throw usage_error("Invalid argument for --"+lng); data = tmp; } @@ -82,7 +96,7 @@ private: { public: ListOption(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) - { if(arg_type!=REQUIRED_ARG) throw Exception("ListOption with arg_type!=REQUIRED makes no sense"); } + { if(arg_type!=REQUIRED_ARG) throw std::invalid_argument("ListOption arg_type!=REQUIRED"); } virtual void store() { } @@ -92,7 +106,7 @@ private: std::istringstream ss(a); ss>>tmp; if(ss.fail()) - throw UsageError("Invalid argument for --"+lng); + throw usage_error("Invalid argument for --"+lng); data.push_back(tmp); } @@ -100,10 +114,12 @@ private: T &data; }; + bool help; std::list opts; std::vector args; public: + GetOpt(); ~GetOpt(); const std::vector &get_args() const { return args; } -- 2.45.2