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::get_option(char s)
+GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a)
{
- for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
+ if(l.empty())
+ throw invalid_argument("GetOpt::add_option");
+ if(t.is_list() && a!=REQUIRED_ARG)
+ throw invalid_argument("GetOpt::add_option");
+
+ for(OptionList::iterator i=opts.begin(); i!=opts.end(); )
+ {
+ if((s!=0 && (*i)->get_short()==s) || (*i)->get_long()==l)
+ {
+ delete *i;
+ opts.erase(i++);
+ }
+ else
+ ++i;
+ }
+
+ opts.push_back(new OptionImpl(s, l, t, a));
+ return *opts.back();
+}
+
+GetOpt::OptionImpl &GetOpt::get_option(char s)
+{
+ for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
if((*i)->get_short()==s)
return **i;
throw usage_error(string("Unknown option -")+s);
}
-GetOpt::OptBase &GetOpt::get_option(const string &l)
+GetOpt::OptionImpl &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);
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
// 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)
{
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())
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())
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;
}
-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),
- metavar("ARG")
+ ext_seen_count(0),
+ 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;
}
-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;
}
-void GetOpt::OptBase::process()
+GetOpt::OptionImpl &GetOpt::OptionImpl::bind_seen_count(unsigned &c)
+{
+ ext_seen_count = &c;
+ return *this;
+}
+
+void GetOpt::OptionImpl::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->store();
+ }
+ catch(const exception &e)
+ {
+ throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+ }
}
-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");
+
++seen_count;
+ if(ext_seen_count)
+ *ext_seen_count = seen_count;
- store(arg);
+ try
+ {
+ store->store(arg);
+ }
+ catch(const exception &e)
+ {
+ throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+ }
}
} // namespace Msp