X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.cpp;h=6f7b1f807c9d719d3829959db91370860a62fabe;hp=f88d4a92ac5a2c972eb0fba77cede64766d50126;hb=44da9fc9afb6b7e49c1558c5572213a1e6f401e8;hpb=9be92503cda27dffd8c3219ec4cfadaee37b6369 diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index f88d4a9..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,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); } @@ -105,17 +106,17 @@ void GetOpt::operator()(unsigned argc, const char *const *argv) else args_raw.push_back(argv[i++]); } - + for(; iis_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"); @@ -137,7 +138,8 @@ void GetOpt::operator()(unsigned argc, const char *const *argv) } catch(const usage_error &e) { - throw usage_error(e.what(), "Usage: "+generate_usage(argv[0])); + if(!help) + throw usage_error(e.what(), "Usage: "+generate_usage(argv[0])); } if(help) @@ -152,9 +154,9 @@ unsigned GetOpt::process_long(const char *const *argp) // See if the argument contains an = unsigned equals = 0; for(; arg[equals] && arg[equals]!='='; ++equals) ; - + OptionImpl &opt = get_option(string(arg, equals)); - + if(arg[equals]) // Process the part after the = as option argument opt.process(arg+equals+1); @@ -169,7 +171,7 @@ unsigned GetOpt::process_long(const char *const *argp) } else opt.process(); - + return 1; } @@ -193,7 +195,7 @@ unsigned GetOpt::process_short(const char *const *argp) { if(!argp[1]) throw usage_error("-"+string(1, *arg)+" requires an argument"); - + // Use the next argument as option argument opt.process(argp[1]); return 2; @@ -212,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 += ']'; } @@ -255,66 +257,64 @@ 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) + 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()); } - list pargs; - for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i) + vector pargs; + 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()); } - + return result; } -GetOpt::OptionImpl::OptionImpl(char s, const std::string &l, const Store &t, ArgType a): +GetOpt::OptionImpl::OptionImpl(char s, const string &l, const Store &t, ArgType a): shrt(s), lng(l), arg_type(a),