]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.cpp
Use a typedef for the option descriptor list
[libs/core.git] / source / core / getopt.cpp
index 766b858e8d4aa4b2212b94f51756cad0a07a964b..2bbfebc5c7a867e200f1b5463d0c9db1894f3899 100644 (file)
@@ -13,13 +13,13 @@ GetOpt::GetOpt():
 
 GetOpt::~GetOpt()
 {
-       for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
+       for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
                delete *i;
 }
 
 GetOpt::OptBase &GetOpt::add_option(OptBase *opt)
 {
-       for(list<OptBase *>::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())
                {
@@ -36,7 +36,7 @@ GetOpt::OptBase &GetOpt::add_option(OptBase *opt)
 
 GetOpt::OptBase &GetOpt::get_option(char s)
 {
-       for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
+       for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
                if((*i)->get_short()==s)
                        return **i;
        throw usage_error(string("Unknown option -")+s);
@@ -44,7 +44,7 @@ GetOpt::OptBase &GetOpt::get_option(char s)
 
 GetOpt::OptBase &GetOpt::get_option(const string &l)
 {
-       for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
+       for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
                if((*i)->get_long()==l)
                        return **i;
        throw usage_error(string("Unknown option --")+l);
@@ -149,7 +149,7 @@ unsigned GetOpt::process_short(const char *const *argp)
 string GetOpt::generate_usage(const string &argv0) const
 {
        string result = argv0;
-       for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+       for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
        {
                result += " [";
                if((*i)->get_short())
@@ -180,12 +180,12 @@ string GetOpt::generate_usage(const string &argv0) const
 string GetOpt::generate_help() const
 {
        bool any_short = false;
-       for(list<OptBase *>::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
+       for(OptionList::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
                any_short = (*i)->get_short();
 
        string::size_type maxw = 0;
        list<string> switches;
-       for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+       for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
        {
                string swtch;
                if((*i)->get_short())
@@ -215,7 +215,7 @@ string GetOpt::generate_help() const
 
        string result;
        list<string>::const_iterator j = switches.begin();
-       for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
+       for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
                result += format("  %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
        
        return result;
@@ -227,6 +227,7 @@ GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a):
        lng(l),
        arg_type(a),
        seen_count(0),
+       ext_seen_count(0),
        metavar("ARG")
 {
        if(lng.empty())
@@ -246,22 +247,48 @@ GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m)
        return *this;
 }
 
+GetOpt::OptBase &GetOpt::OptBase::bind_seen_count(unsigned &c)
+{
+       ext_seen_count = &c;
+       return *this;
+}
+
 void GetOpt::OptBase::process()
 {
        if(arg_type==REQUIRED_ARG)
                throw usage_error("--"+lng+" requires an argument");
+
        ++seen_count;
+       if(ext_seen_count)
+               *ext_seen_count = seen_count;
 
-       store();
+       try
+       {
+               store();
+       }
+       catch(const exception &e)
+       {
+               throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+       }
 }
 
 void GetOpt::OptBase::process(const string &arg)
 {
        if(arg_type==NO_ARG)
                throw usage_error("--"+lng+" takes no argument");
+
        ++seen_count;
+       if(ext_seen_count)
+               *ext_seen_count = seen_count;
 
-       store(arg);
+       try
+       {
+               store(arg);
+       }
+       catch(const exception &e)
+       {
+               throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+       }
 }
 
 } // namespace Msp