]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.cpp
Restructure internals of GetOpt
[libs/core.git] / source / core / getopt.cpp
index 2bbfebc5c7a867e200f1b5463d0c9db1894f3899..e909712a87e7527f35bdad1ca2c148c5f3e6dd0d 100644 (file)
@@ -17,11 +17,14 @@ GetOpt::~GetOpt()
                delete *i;
 }
 
                delete *i;
 }
 
-GetOpt::OptBase &GetOpt::add_option(OptBase *opt)
+GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a)
 {
 {
+       if(l.empty())
+               throw invalid_argument("GetOpt::add_option");
+
        for(OptionList::iterator i=opts.begin(); i!=opts.end(); )
        {
        for(OptionList::iterator i=opts.begin(); i!=opts.end(); )
        {
-               if((opt->get_short()!=0 && (*i)->get_short()==opt->get_short()) || (*i)->get_long()==opt->get_long())
+               if((s!=0 && (*i)->get_short()==s) || (*i)->get_long()==l)
                {
                        delete *i;
                        opts.erase(i++);
                {
                        delete *i;
                        opts.erase(i++);
@@ -30,11 +33,11 @@ GetOpt::OptBase &GetOpt::add_option(OptBase *opt)
                        ++i;
        }
 
                        ++i;
        }
 
-       opts.push_back(opt);
+       opts.push_back(new OptionImpl(s, l, t, a));
        return *opts.back();
 }
 
        return *opts.back();
 }
 
-GetOpt::OptBase &GetOpt::get_option(char s)
+GetOpt::OptionImpl &GetOpt::get_option(char s)
 {
        for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
                if((*i)->get_short()==s)
 {
        for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
                if((*i)->get_short()==s)
@@ -42,7 +45,7 @@ GetOpt::OptBase &GetOpt::get_option(char s)
        throw usage_error(string("Unknown option -")+s);
 }
 
        throw usage_error(string("Unknown option -")+s);
 }
 
-GetOpt::OptBase &GetOpt::get_option(const string &l)
+GetOpt::OptionImpl &GetOpt::get_option(const string &l)
 {
        for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
                if((*i)->get_long()==l)
 {
        for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
                if((*i)->get_long()==l)
@@ -94,7 +97,7 @@ unsigned GetOpt::process_long(const char *const *argp)
        unsigned equals = 0;
        for(; arg[equals] && arg[equals]!='='; ++equals) ;
        
        unsigned equals = 0;
        for(; arg[equals] && arg[equals]!='='; ++equals) ;
        
-       OptBase &opt = get_option(string(arg, equals));
+       OptionImpl &opt = get_option(string(arg, equals));
        
        if(arg[equals])
                // Process the part after the = as option argument
        
        if(arg[equals])
                // Process the part after the = as option argument
@@ -122,7 +125,7 @@ unsigned GetOpt::process_short(const char *const *argp)
        // Loop through all characters in the argument
        for(; *arg; ++arg)
        {
        // Loop through all characters in the argument
        for(; *arg; ++arg)
        {
-               OptBase &opt = get_option(*arg);
+               OptionImpl &opt = get_option(*arg);
 
                if(arg[1] && opt.get_arg_type()!=NO_ARG)
                {
 
                if(arg[1] && opt.get_arg_type()!=NO_ARG)
                {
@@ -222,38 +225,41 @@ string GetOpt::generate_help() const
 }
 
 
 }
 
 
-GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a):
+GetOpt::OptionImpl::OptionImpl(char s, const std::string &l, const Store &t, ArgType a):
        shrt(s),
        lng(l),
        arg_type(a),
        seen_count(0),
        ext_seen_count(0),
        shrt(s),
        lng(l),
        arg_type(a),
        seen_count(0),
        ext_seen_count(0),
-       metavar("ARG")
+       metavar("ARG"),
+       store(t.clone())
+{ }
+
+GetOpt::OptionImpl::~OptionImpl()
 {
 {
-       if(lng.empty())
-               throw invalid_argument("empty long option name");
+       delete store;
 }
 
 }
 
-GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h)
+GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h)
 {
        help = h;
        return *this;
 }
 
 {
        help = h;
        return *this;
 }
 
-GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m)
+GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h, const string &m)
 {
        help = h;
        metavar = m;
        return *this;
 }
 
 {
        help = h;
        metavar = m;
        return *this;
 }
 
-GetOpt::OptBase &GetOpt::OptBase::bind_seen_count(unsigned &c)
+GetOpt::OptionImpl &GetOpt::OptionImpl::bind_seen_count(unsigned &c)
 {
        ext_seen_count = &c;
        return *this;
 }
 
 {
        ext_seen_count = &c;
        return *this;
 }
 
-void GetOpt::OptBase::process()
+void GetOpt::OptionImpl::process()
 {
        if(arg_type==REQUIRED_ARG)
                throw usage_error("--"+lng+" requires an argument");
 {
        if(arg_type==REQUIRED_ARG)
                throw usage_error("--"+lng+" requires an argument");
@@ -264,7 +270,7 @@ void GetOpt::OptBase::process()
 
        try
        {
 
        try
        {
-               store();
+               store->store();
        }
        catch(const exception &e)
        {
        }
        catch(const exception &e)
        {
@@ -272,7 +278,7 @@ void GetOpt::OptBase::process()
        }
 }
 
        }
 }
 
-void GetOpt::OptBase::process(const string &arg)
+void GetOpt::OptionImpl::process(const string &arg)
 {
        if(arg_type==NO_ARG)
                throw usage_error("--"+lng+" takes no argument");
 {
        if(arg_type==NO_ARG)
                throw usage_error("--"+lng+" takes no argument");
@@ -283,7 +289,7 @@ void GetOpt::OptBase::process(const string &arg)
 
        try
        {
 
        try
        {
-               store(arg);
+               store->store(arg);
        }
        catch(const exception &e)
        {
        }
        catch(const exception &e)
        {