]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.h
Replace earlier options sharing the same short or long name
[libs/core.git] / source / core / getopt.h
index d45e888692ea9f96d2947180f19de75f6652eabb..a3b7ae405ec567c4d9b0fb6a8966d89bed4fb689 100644 (file)
@@ -1,18 +1,11 @@
-/* $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 <list>
-#include <sstream>
 #include <stdexcept>
 #include <string>
 #include <vector>
+#include <msp/strings/lexicalcast.h>
 
 namespace Msp {
 
@@ -72,6 +65,9 @@ private:
        template<typename T>
        class Option: public OptBase
        {
+       private:
+               T &data;
+
        public:
                Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { }
 
@@ -79,21 +75,23 @@ private:
 
                virtual void store(const std::string &a)
                {
-                       T tmp;
-                       std::istringstream ss(a);
-                       ss>>tmp;
-                       if(ss.fail())
-                               throw usage_error("Invalid argument for --"+lng);
-
-                       data = tmp;
+                       try
+                       {
+                               data = lexical_cast<T>(a);
+                       }
+                       catch(const lexical_error &e)
+                       {
+                               throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+                       }
                }
-       private:
-               T &data;
        };
 
        template<typename T>
        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 std::invalid_argument("ListOption arg_type!=REQUIRED"); }
@@ -102,16 +100,15 @@ private:
 
                virtual void store(const std::string &a)
                {
-                       typename T::value_type tmp;
-                       std::istringstream ss(a);
-                       ss>>tmp;
-                       if(ss.fail())
-                               throw usage_error("Invalid argument for --"+lng);
-
-                       data.push_back(tmp);
+                       try
+                       {
+                               data.push_back(lexical_cast<typename T::value_type>(a));
+                       }
+                       catch(const lexical_error &e)
+                       {
+                               throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+                       }
                }
-       private:
-               T &data;
        };
 
        bool help;
@@ -126,17 +123,19 @@ public:
 
        template<typename T>
        OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG)
-       { opts.push_back(new Option<T>(s, l, d, a)); return *opts.back(); }
+       { return add_option(new Option<T>(s, l, d, a)); }
        
        template<typename T>
        OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a = REQUIRED_ARG)
-       { opts.push_back(new ListOption<std::list<T> >(s, l, d, a)); return *opts.back(); }
+       { return add_option(new ListOption<std::list<T> >(s, l, d, a)); }
        
        template<typename T>
        OptBase &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 &);
 
@@ -161,8 +160,11 @@ public:
        std::string generate_help() const;
 };
 
-template<> inline void GetOpt::Option<bool>::store()     { data = true; }
-template<> inline void GetOpt::Option<unsigned>::store() { ++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; }