X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.cpp;h=ad726448589cc0ebc38255cd7790cdbbaf0e0136;hp=cfc9d7a02fa99e547cc19afefecf6c32250691b3;hb=9c6c946ed658abd50be9d597cb2c722592d660ae;hpb=d16185720fa344263367dbd50c61bfc8183d99a4 diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index cfc9d7a..ad72644 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -17,6 +17,23 @@ GetOpt::~GetOpt() delete *i; } +GetOpt::OptBase &GetOpt::add_option(OptBase *opt) +{ + for(list::iterator i=opts.begin(); i!=opts.end(); ) + { + if((opt->get_short()!=0 && (*i)->get_short()==opt->get_short()) || (*i)->get_long()==opt->get_long()) + { + delete *i; + opts.erase(i++); + } + else + ++i; + } + + opts.push_back(opt); + return *opts.back(); +} + GetOpt::OptBase &GetOpt::get_option(char s) { for(list::iterator i=opts.begin(); i!=opts.end(); ++i) @@ -210,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()) @@ -229,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