]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.h
Fix GetOpt to get strings correctly
[libs/core.git] / source / core / getopt.h
index 7e5e25f5a1a9ee9994995cc9c77ca4355db8e8b7..94e8b1e4af35e598a6e1f99600f837aef916cfbb 100644 (file)
@@ -27,14 +27,16 @@ public:
        class OptBase
        {
        public:
-               OptBase           &set_help(const std::string &h) { help=h; return *this; }
+               OptBase           &set_help(const std::string &);
+               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 void      process()=0;
-               virtual void      process(const std::string &)=0;
+               void              process();
+               void              process(const std::string &);
                virtual ~OptBase() { }
        protected:
                char        shrt;
@@ -42,8 +44,11 @@ public:
                ArgType     arg_type;
                unsigned    seen_count;
                std::string help;
+               std::string metavar;
 
-               OptBase(char s, const std::string &l, ArgType a): shrt(s), lng(l), arg_type(a), seen_count(0) { }
+               OptBase(char, const std::string &, ArgType);
+               virtual void store()=0;
+               virtual void store(const std::string &)=0;
        };
 
 private:
@@ -53,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;
@@ -73,12 +69,9 @@ private:
                                throw UsageError("Invalid argument for --"+lng);
 
                        data=tmp;
-                       ++seen_count;
                }
        private:
                T &data;
-
-               void process_() { }
        };
 
        template<typename T>
@@ -88,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);
@@ -102,7 +92,6 @@ private:
                                throw UsageError("Invalid argument for --"+lng);
 
                        data.push_back(tmp);
-                       ++seen_count;
                }
        private:
                T &data;
@@ -139,8 +128,14 @@ private:
        unsigned process_short(const char *const *);
 };
 
-template<> inline void GetOpt::Option<bool>::process_()     { data=true; }
-template<> inline void GetOpt::Option<unsigned>::process_() { ++data; }
+template<> inline void GetOpt::Option<bool>::store()     { data=true; }
+template<> inline void GetOpt::Option<unsigned>::store() { ++data; }
+
+template<> inline void GetOpt::Option<std::string>::store(const std::string &a)
+{ data=a; }
+
+template<> inline void GetOpt::ListOption<std::list<std::string> >::store(const std::string &a)
+{ data.push_back(a); }
 
 } // namespace Msp