X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.cpp;h=34e53172aeb194d1cebc29c7df4de8719add8c27;hp=b472c58b835a94e27fa94046c488e9777096b60f;hb=9f8d6a18f68fb54b62e9f4a5cf1a1650282b3667;hpb=967785734be5c3fc6f75da122c2d93ebbb338271 diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index b472c58..34e5317 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -1,3 +1,4 @@ +#include #include "getopt.h" using namespace std; @@ -16,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) @@ -130,35 +148,33 @@ unsigned GetOpt::process_short(const char *const *argp) string GetOpt::generate_usage(const string &argv0) const { - ostringstream line; - - line<::const_iterator i=opts.begin(); i!=opts.end(); ++i) { - line<<" ["; + result += " ["; if((*i)->get_short()) { - line<<'-'<<(*i)->get_short(); + result += format("-%c", (*i)->get_short()); if(!(*i)->get_long().empty()) - line<<'|'; + result += '|'; else if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<'['<<(*i)->get_metavar()<<']'; + result += format("[%s]", (*i)->get_metavar()); else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<' '<<(*i)->get_metavar(); + result += format(" %s", (*i)->get_metavar()); } if(!(*i)->get_long().empty()) { - line<<"--"<<(*i)->get_long(); + result += format("--%s", (*i)->get_long()); if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<"[="<<(*i)->get_metavar()<<']'; + result += format("[=%s]", (*i)->get_metavar()); else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<'='<<(*i)->get_metavar(); + result += format("=%s", (*i)->get_metavar()); } - line<<']'; + result += ']'; } - return line.str(); + return result; } string GetOpt::generate_help() const @@ -171,41 +187,36 @@ string GetOpt::generate_help() const list switches; for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i) { - ostringstream swtch; + string swtch; if((*i)->get_short()) { - swtch<<'-'<<(*i)->get_short(); + swtch += format("-%c", (*i)->get_short()); if(!(*i)->get_long().empty()) - swtch<<", "; + swtch += ", "; else if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch<<'['<<(*i)->get_metavar()<<']'; + swtch += format("[%s]", (*i)->get_metavar()); else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch<<' '<<(*i)->get_metavar(); + swtch += format(" %s", (*i)->get_metavar()); } else if(any_short) - swtch<<" "; + swtch += " "; if(!(*i)->get_long().empty()) { - swtch<<"--"<<(*i)->get_long(); + swtch += format("--%s", (*i)->get_long()); if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch<<"[="<<(*i)->get_metavar()<<']'; + swtch += format("[=%s]", (*i)->get_metavar()); else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch<<'='<<(*i)->get_metavar(); + swtch += format("=%s", (*i)->get_metavar()); } - switches.push_back(swtch.str()); - maxw = max(maxw, switches.back().size()); + switches.push_back(swtch); + maxw = max(maxw, swtch.size()); } string result; list::const_iterator j = switches.begin(); for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j) - { - result += " "+*j; - result += string(maxw+2-j->size(), ' '); - result += (*i)->get_help(); - result += '\n'; - } + result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help()); return result; } @@ -216,8 +227,12 @@ 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()) + throw invalid_argument("empty long option name"); +} GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h) { @@ -232,11 +247,19 @@ 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(); } @@ -246,6 +269,8 @@ 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); }