X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.cpp;h=6f7b1f807c9d719d3829959db91370860a62fabe;hp=766b858e8d4aa4b2212b94f51756cad0a07a964b;hb=44da9fc9afb6b7e49c1558c5572213a1e6f401e8;hpb=154d5f43f7a6633a4937499039fc1b1b713e0c0c diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index 766b858..6f7b1f8 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -1,4 +1,5 @@ #include +#include "algorithm.h" #include "getopt.h" using namespace std; @@ -13,15 +14,22 @@ GetOpt::GetOpt(): GetOpt::~GetOpt() { - for(list::iterator i=opts.begin(); i!=opts.end(); ++i) - delete *i; + for(OptionImpl *i: opts) + delete i; + for(ArgumentImpl *i: args) + delete i; } -GetOpt::OptBase &GetOpt::add_option(OptBase *opt) +GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a) { - for(list::iterator i=opts.begin(); i!=opts.end(); ) + if(l.empty()) + throw invalid_argument("GetOpt::add_option"); + if(t.is_list() && a!=REQUIRED_ARG) + throw invalid_argument("GetOpt::add_option"); + + for(auto 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++); @@ -30,23 +38,47 @@ GetOpt::OptBase &GetOpt::add_option(OptBase *opt) ++i; } - opts.push_back(opt); + opts.push_back(new OptionImpl(s, l, t, a)); return *opts.back(); } -GetOpt::OptBase &GetOpt::get_option(char s) +GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType y) +{ + if(y==NO_ARG) + throw invalid_argument("GetOpt::add_argument"); + + bool have_list = false; + bool have_optional = false; + for(const ArgumentImpl *a: args) + { + if(a->is_list_store()) + have_list = true; + else if(a->get_type()==OPTIONAL_ARG) + have_optional = true; + } + + if(have_optional && (t.is_list() || y!=OPTIONAL_ARG)) + throw invalid_argument("GetOpt::add_argument"); + if(have_list && (t.is_list() || y==OPTIONAL_ARG)) + throw invalid_argument("GetOpt::add_argument"); + + args.push_back(new ArgumentImpl(n, t, y)); + return *args.back(); +} + +GetOpt::OptionImpl &GetOpt::get_option(char s) { - for(list::iterator i=opts.begin(); i!=opts.end(); ++i) - if((*i)->get_short()==s) - return **i; + auto i = find_if(opts, [s](const OptionImpl *o){ return o->get_short()==s; }); + if(i!=opts.end()) + 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::iterator i=opts.begin(); i!=opts.end(); ++i) - if((*i)->get_long()==l) - return **i; + auto i = find_if(opts, [&l](const OptionImpl *o){ return o->get_long()==l; }); + if(i!=opts.end()) + return **i; throw usage_error(string("Unknown option --")+l); } @@ -54,6 +86,8 @@ void GetOpt::operator()(unsigned argc, const char *const *argv) { try { + /* Arguments must first be collected into an array to handle the case + where a variable-length argument list is followed by fixed arguments. */ unsigned i = 1; for(; iis_list_store()) + { + unsigned end = args_raw.size(); + for(auto k=j; ++k!=args.end(); ) + --end; + if(i==end && (*j)->get_type()==REQUIRED_ARG) + throw usage_error((*j)->get_name()+" is required"); + for(; iprocess(args_raw[i]); + } + else + { + if(iprocess(args_raw[i++]); + else if((*j)->get_type()==REQUIRED_ARG) + throw usage_error((*j)->get_name()+" is required"); + } + } + + // XXX Enable this when get_args() is completely removed + /*if(i::const_iterator i=opts.begin(); i!=opts.end(); ++i) + if(compact) + result += " [options]"; + else { - result += " ["; - if((*i)->get_short()) + for(const OptionImpl *o: opts) { - result += format("-%c", (*i)->get_short()); - if(!(*i)->get_long().empty()) - result += '|'; - else if((*i)->get_arg_type()==OPTIONAL_ARG) - result += format("[%s]", (*i)->get_metavar()); - else if((*i)->get_arg_type()==REQUIRED_ARG) - result += format(" %s", (*i)->get_metavar()); - } - if(!(*i)->get_long().empty()) - { - result += format("--%s", (*i)->get_long()); + result += " ["; + if(o->get_short()) + { + result += format("-%c", o->get_short()); + if(!o->get_long().empty()) + result += '|'; + else if(o->get_arg_type()==OPTIONAL_ARG) + result += format("[%s]", o->get_metavar()); + else if(o->get_arg_type()==REQUIRED_ARG) + result += format(" %s", o->get_metavar()); + } + if(!o->get_long().empty()) + { + result += format("--%s", o->get_long()); - if((*i)->get_arg_type()==OPTIONAL_ARG) - result += format("[=%s]", (*i)->get_metavar()); - else if((*i)->get_arg_type()==REQUIRED_ARG) - result += format("=%s", (*i)->get_metavar()); + if(o->get_arg_type()==OPTIONAL_ARG) + result += format("[=%s]", o->get_metavar()); + else if(o->get_arg_type()==REQUIRED_ARG) + result += format("=%s", o->get_metavar()); + } + result += ']'; } - result += ']'; + } + + for(const ArgumentImpl *a: args) + { + result += ' '; + if(a->get_type()==OPTIONAL_ARG) + result += '['; + result += format("<%s>", a->get_name()); + if(a->is_list_store()) + result += " ..."; + if(a->get_type()==OPTIONAL_ARG) + result += ']'; } return result; @@ -179,89 +257,163 @@ string GetOpt::generate_usage(const string &argv0) const string GetOpt::generate_help() const { - bool any_short = false; - for(list::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i) - any_short = (*i)->get_short(); + bool any_short = any_of(opts.begin(), opts.end(), [](const OptionImpl *o){ return o->get_short(); }); string::size_type maxw = 0; - list switches; - for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i) + vector switches; + for(const OptionImpl *o: opts) { string swtch; - if((*i)->get_short()) + if(o->get_short()) { - swtch += format("-%c", (*i)->get_short()); - if(!(*i)->get_long().empty()) + swtch += format("-%c", o->get_short()); + if(!o->get_long().empty()) swtch += ", "; - else if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch += format("[%s]", (*i)->get_metavar()); - else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch += format(" %s", (*i)->get_metavar()); + else if(o->get_arg_type()==OPTIONAL_ARG) + swtch += format("[%s]", o->get_metavar()); + else if(o->get_arg_type()==REQUIRED_ARG) + swtch += format(" %s", o->get_metavar()); } else if(any_short) swtch += " "; - if(!(*i)->get_long().empty()) + if(!o->get_long().empty()) { - swtch += format("--%s", (*i)->get_long()); + swtch += format("--%s", o->get_long()); - if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch += format("[=%s]", (*i)->get_metavar()); - else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch += format("=%s", (*i)->get_metavar()); + if(o->get_arg_type()==OPTIONAL_ARG) + swtch += format("[=%s]", o->get_metavar()); + else if(o->get_arg_type()==REQUIRED_ARG) + swtch += format("=%s", o->get_metavar()); } switches.push_back(swtch); maxw = max(maxw, swtch.size()); } + vector pargs; + for(const ArgumentImpl *a: args) + { + string parg = format("<%s>", a->get_name()); + pargs.push_back(parg); + maxw = max(maxw, parg.size()); + } + string result; - list::const_iterator j = switches.begin(); - for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j) + result += "Options:\n"; + auto j = switches.begin(); + for(auto i=opts.begin(); i!=opts.end(); ++i, ++j) result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help()); - + if(!pargs.empty()) + { + result += "\nArguments:\n"; + j = pargs.begin(); + for(auto i=args.begin(); i!=args.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 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()+")"); + } +} + + +GetOpt::ArgumentImpl::ArgumentImpl(const string &n, const Store &t, ArgType a): + name(n), + type(a), + store(t.clone()) +{ } + +GetOpt::ArgumentImpl::~ArgumentImpl() +{ + delete store; +} + +GetOpt::ArgumentImpl &GetOpt::ArgumentImpl::set_help(const string &h) +{ + help = h; + return *this; +} + +void GetOpt::ArgumentImpl::process(const string &arg) +{ + try + { + store->store(arg); + } + catch(const exception &e) + { + throw usage_error("Invalid "+name+" ("+e.what()+")"); + } } } // namespace Msp