X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fgetopt.h;h=bb1bd1568f6dff403843e6bd57492bc0832fb21d;hb=6a38983c19fe78753962288e206c5817ad595448;hp=8e0f49a3fb9d12c83d986289e6cf69d5fc10cee9;hpb=d5dd704b2576f878809e87dbb8ff8591b9bdbce4;p=libs%2Fcore.git diff --git a/source/core/getopt.h b/source/core/getopt.h index 8e0f49a..bb1bd15 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -8,13 +8,27 @@ Distributed under the LGPL #ifndef MSP_CORE_GETOPT_H_ #define MSP_CORE_GETOPT_H_ -#include +#include +#include #include #include -#include "except.h" +#include 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: @@ -65,13 +79,14 @@ private: virtual void store(const std::string &a) { - T tmp; - std::istringstream ss(a); - ss>>tmp; - if(ss.fail()) - throw UsageError("Invalid argument for --"+lng); - - data = tmp; + try + { + data = lexical_cast(a); + } + catch(const lexical_error &e) + { + throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")"); + } } private: T &data; @@ -82,28 +97,31 @@ 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() { } virtual void store(const std::string &a) { - typename T::value_type tmp; - std::istringstream ss(a); - ss>>tmp; - if(ss.fail()) - throw UsageError("Invalid argument for --"+lng); - - data.push_back(tmp); + try + { + data.push_back(lexical_cast(a)); + } + catch(const lexical_error &e) + { + throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")"); + } } private: T &data; }; + bool help; std::list opts; std::vector args; public: + GetOpt(); ~GetOpt(); const std::vector &get_args() const { return args; }