From: Mikko Rasa Date: Fri, 11 Sep 2009 17:34:36 +0000 (+0000) Subject: Fix GetOpt to get strings correctly X-Git-Tag: 1.1~2 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ae48dac6a368dbf68b1ec976254a5f896c5b737b;p=libs%2Fcore.git Fix GetOpt to get strings correctly Handle missing/extraneous arguments in OptBase --- diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index 8fd13a0..be1d151 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -247,4 +247,22 @@ GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m) return *this; } +void GetOpt::OptBase::process() +{ + if(arg_type==REQUIRED_ARG) + throw UsageError("--"+lng+" requires an argument"); + ++seen_count; + + store(); +} + +void GetOpt::OptBase::process(const string &arg) +{ + if(arg_type==NO_ARG) + throw UsageError("--"+lng+" takes no argument"); + ++seen_count; + + store(arg); +} + } // namespace Msp diff --git a/source/core/getopt.h b/source/core/getopt.h index f6c4859..94e8b1e 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -35,8 +35,8 @@ public: 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 void process()=0; - virtual void process(const std::string &)=0; + void process(); + void process(const std::string &); virtual ~OptBase() { } protected: char shrt; @@ -47,6 +47,8 @@ public: std::string metavar; OptBase(char, const std::string &, ArgType); + virtual void store()=0; + virtual void store(const std::string &)=0; }; private: @@ -56,19 +58,10 @@ private: public: Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { } - virtual void process() - { - if(arg_type==REQUIRED_ARG) - throw UsageError("--"+lng+" requires an argument"); - process_(); - ++seen_count; - } + virtual void store() { } - virtual void process(const std::string &a) + virtual void store(const std::string &a) { - if(arg_type==NO_ARG) - throw UsageError("--"+lng+" takes no argument"); - T tmp; std::istringstream ss(a); ss>>tmp; @@ -76,12 +69,9 @@ private: throw UsageError("Invalid argument for --"+lng); data=tmp; - ++seen_count; } private: T &data; - - void process_() { } }; template @@ -91,12 +81,9 @@ private: 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"); } - virtual void process() - { - throw UsageError("--"+lng+" requires an argument"); - } + virtual void store() { } - virtual void process(const std::string &a) + virtual void store(const std::string &a) { typename T::value_type tmp; std::istringstream ss(a); @@ -105,7 +92,6 @@ private: throw UsageError("Invalid argument for --"+lng); data.push_back(tmp); - ++seen_count; } private: T &data; @@ -142,8 +128,14 @@ private: unsigned process_short(const char *const *); }; -template<> inline void GetOpt::Option::process_() { data=true; } -template<> inline void GetOpt::Option::process_() { ++data; } +template<> inline void GetOpt::Option::store() { data=true; } +template<> inline void GetOpt::Option::store() { ++data; } + +template<> inline void GetOpt::Option::store(const std::string &a) +{ data=a; } + +template<> inline void GetOpt::ListOption >::store(const std::string &a) +{ data.push_back(a); } } // namespace Msp