X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.cpp;fp=source%2Fcore%2Fgetopt.cpp;h=ec821e0d26aea1372c3314cc8a1a99a268d5443e;hp=0f8886ad746a542e3a40b36ed8e7c2ddcd0c1e19;hb=f24e7b96e76b63c9b9b8a6bce4c7a9db64276ea8;hpb=9f754b788b872f9768af8c3a4f9e001a588e011a diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index 0f8886a..ec821e0 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,10 +14,10 @@ GetOpt::GetOpt(): GetOpt::~GetOpt() { - for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i) - delete *i; - for(ArgumentList::iterator i=args.begin(); i!=args.end(); ++i) - delete *i; + for(OptionImpl *i: opts) + delete i; + for(ArgumentImpl *i: args) + delete i; } GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a) @@ -26,7 +27,7 @@ GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, if(t.is_list() && a!=REQUIRED_ARG) throw invalid_argument("GetOpt::add_option"); - for(OptionList::iterator i=opts.begin(); i!=opts.end(); ) + for(auto i=opts.begin(); i!=opts.end(); ) { if((s!=0 && (*i)->get_short()==s) || (*i)->get_long()==l) { @@ -41,43 +42,43 @@ GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, return *opts.back(); } -GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType a) +GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType y) { - if(a==NO_ARG) + if(y==NO_ARG) throw invalid_argument("GetOpt::add_argument"); bool have_list = false; bool have_optional = false; - for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i) + for(const ArgumentImpl *a: args) { - if((*i)->is_list_store()) + if(a->is_list_store()) have_list = true; - else if((*i)->get_type()==OPTIONAL_ARG) + else if(a->get_type()==OPTIONAL_ARG) have_optional = true; } - if(have_optional && (t.is_list() || a!=OPTIONAL_ARG)) + if(have_optional && (t.is_list() || y!=OPTIONAL_ARG)) throw invalid_argument("GetOpt::add_argument"); - if(have_list && (t.is_list() || a==OPTIONAL_ARG)) + if(have_list && (t.is_list() || y==OPTIONAL_ARG)) throw invalid_argument("GetOpt::add_argument"); - args.push_back(new ArgumentImpl(n, t, a)); + args.push_back(new ArgumentImpl(n, t, y)); return *args.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; + 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::OptionImpl &GetOpt::get_option(const string &l) { - for(OptionList::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); } @@ -110,12 +111,12 @@ void GetOpt::operator()(unsigned argc, const char *const *argv) args_raw.push_back(argv[i]); i = 0; - for(ArgumentList::const_iterator j=args.begin(); j!=args.end(); ++j) + for(auto j=args.begin(); j!=args.end(); ++j) { if((*j)->is_list_store()) { unsigned end = args_raw.size(); - for(ArgumentList::const_iterator k=j; ++k!=args.end(); ) + for(auto k=j; ++k!=args.end(); ) --end; if(i==end && (*j)->get_type()==REQUIRED_ARG) throw usage_error((*j)->get_name()+" is required"); @@ -213,41 +214,41 @@ string GetOpt::generate_usage(const string &argv0, bool compact) const result += " [options]"; else { - for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i) + for(const OptionImpl *o: opts) { result += " ["; - if((*i)->get_short()) + if(o->get_short()) { - result += format("-%c", (*i)->get_short()); - if(!(*i)->get_long().empty()) + result += format("-%c", o->get_short()); + if(!o->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()); + 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(!(*i)->get_long().empty()) + if(!o->get_long().empty()) { - result += format("--%s", (*i)->get_long()); + 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 += ']'; } } - for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i) + for(const ArgumentImpl *a: args) { result += ' '; - if((*i)->get_type()==OPTIONAL_ARG) + if(a->get_type()==OPTIONAL_ARG) result += '['; - result += format("<%s>", (*i)->get_name()); - if((*i)->is_list_store()) + result += format("<%s>", a->get_name()); + if(a->is_list_store()) result += " ..."; - if((*i)->get_type()==OPTIONAL_ARG) + if(a->get_type()==OPTIONAL_ARG) result += ']'; } @@ -256,58 +257,56 @@ string GetOpt::generate_usage(const string &argv0, bool compact) const string GetOpt::generate_help() const { - bool any_short = false; - for(OptionList::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(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i) + 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()); } list pargs; - for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i) + for(const ArgumentImpl *a: args) { - string parg = format("<%s>", (*i)->get_name()); + string parg = format("<%s>", a->get_name()); pargs.push_back(parg); maxw = max(maxw, parg.size()); } string result; result += "Options:\n"; - list::const_iterator j = switches.begin(); - for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j) + 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(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i, ++j) + 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()); }