X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.h;h=d3369c6b46fdf57b392b5a8dc56507fc6b79a773;hp=8e0f49a3fb9d12c83d986289e6cf69d5fc10cee9;hb=bc08f0a03c75627427b19e3f821642931ba60b33;hpb=b56eb5ec1da675da0c66abc53c1e4f6c4e4cccbd diff --git a/source/core/getopt.h b/source/core/getopt.h index 8e0f49a..d3369c6 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -1,20 +1,27 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions -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: @@ -25,7 +32,20 @@ public: REQUIRED_ARG }; - class OptBase + class Option + { + protected: + Option() { } + public: + virtual ~Option() { } + + virtual Option &set_help(const std::string &) = 0; + virtual Option &set_help(const std::string &, const std::string &) = 0; + virtual unsigned get_seen_count() const = 0; + }; + +private: + class OptBase: public Option { protected: char shrt; @@ -39,14 +59,14 @@ public: public: virtual ~OptBase() { } - OptBase &set_help(const std::string &); - OptBase &set_help(const std::string &, const std::string &); + virtual OptBase &set_help(const std::string &); + virtual OptBase &set_help(const std::string &, const std::string &); char get_short() const { return shrt; } const std::string &get_long() const { return lng; } ArgType get_arg_type() const { return arg_type; } const std::string &get_help() const { return help; } const std::string &get_metavar() const { return metavar; } - unsigned get_seen_count() const { return seen_count; } + virtual unsigned get_seen_count() const { return seen_count; } void process(); void process(const std::string &); protected: @@ -54,73 +74,80 @@ public: virtual void store(const std::string &) = 0; }; -private: template - class Option: public OptBase + class SimpleOption: public OptBase { + private: + T &data; + public: - Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { } + SimpleOption(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { } virtual void store() { } 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; }; template class ListOption: public OptBase { + private: + T &data; + 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; } template - OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG) - { opts.push_back(new Option(s, l, d, a)); return *opts.back(); } + Option &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG) + { return add_option(new SimpleOption(s, l, d, a)); } template - OptBase &add_option(char s, const std::string &l, std::list &d, ArgType a = REQUIRED_ARG) - { opts.push_back(new ListOption >(s, l, d, a)); return *opts.back(); } + Option &add_option(char s, const std::string &l, std::list &d, ArgType a = REQUIRED_ARG) + { return add_option(new ListOption >(s, l, d, a)); } template - OptBase &add_option(const std::string &l, T &d, ArgType a) + Option &add_option(const std::string &l, T &d, ArgType a) { return add_option(0, l, d, a); } private: + OptBase &add_option(OptBase *); + OptBase &get_option(char); OptBase &get_option(const std::string &); @@ -145,10 +172,13 @@ public: std::string generate_help() const; }; -template<> inline void GetOpt::Option::store() { data = true; } -template<> inline void GetOpt::Option::store() { ++data; } +template<> inline void GetOpt::SimpleOption::store() +{ data = true; } + +template<> inline void GetOpt::SimpleOption::store() +{ ++data; } -template<> inline void GetOpt::Option::store(const std::string &a) +template<> inline void GetOpt::SimpleOption::store(const std::string &a) { data = a; } template<> inline void GetOpt::ListOption >::store(const std::string &a)