X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.cpp;h=0f8886ad746a542e3a40b36ed8e7c2ddcd0c1e19;hp=498977569e2179ee203de59f0e1624b536031f70;hb=20c897ece781e18ba54c41fd68e232ce566a938d;hpb=1f262a37bf2c0531f7474df3cdb60d608bfdbba1 diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index 4989775..0f8886a 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -1,180 +1,168 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ +#include #include "getopt.h" using namespace std; namespace Msp { +GetOpt::GetOpt(): + help(false) +{ + add_option("help", help, NO_ARG).set_help("Displays this help"); +} + GetOpt::~GetOpt() { - for(list::iterator i=opts.begin(); i!=opts.end(); ++i) + for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i) + delete *i; + for(ArgumentList::iterator i=args.begin(); i!=args.end(); ++i) delete *i; } -/** -Generates a single line that gives an overview about the known options. - -@param argv0 The program name to be used in the usage string - -@return The generated usage string -*/ -string GetOpt::generate_usage(const string &argv0) const +GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a) { - ostringstream line; - - line<::const_iterator i=opts.begin(); i!=opts.end(); ++i) + if(l.empty()) + throw invalid_argument("GetOpt::add_option"); + if(t.is_list() && a!=REQUIRED_ARG) + throw invalid_argument("GetOpt::add_option"); + + for(OptionList::iterator i=opts.begin(); i!=opts.end(); ) { - line<<" ["; - if((*i)->get_short()) + if((s!=0 && (*i)->get_short()==s) || (*i)->get_long()==l) { - line<<'-'<<(*i)->get_short(); - if(!(*i)->get_long().empty()) - line<<'|'; - else if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<'['<<(*i)->get_metavar()<<']'; - else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<' '<<(*i)->get_metavar(); - } - if(!(*i)->get_long().empty()) - { - line<<"--"<<(*i)->get_long(); - - if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<"[="<<(*i)->get_metavar()<<']'; - else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<'='<<(*i)->get_metavar(); + delete *i; + opts.erase(i++); } - line<<']'; + else + ++i; } - return line.str(); + opts.push_back(new OptionImpl(s, l, t, a)); + return *opts.back(); } -/** -Generates help for known options in tabular format, one option per line. -The returned string will have a linefeed at the end. -*/ -string GetOpt::generate_help() const +GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType a) { - bool any_short=false; - for(list::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i) - any_short=(*i)->get_short(); + if(a==NO_ARG) + throw invalid_argument("GetOpt::add_argument"); - string::size_type maxw=0; - list switches; - for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i) + bool have_list = false; + bool have_optional = false; + for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i) { - ostringstream swtch; - if((*i)->get_short()) - { - swtch<<'-'<<(*i)->get_short(); - if(!(*i)->get_long().empty()) - swtch<<", "; - else if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch<<'['<<(*i)->get_metavar()<<']'; - else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch<<' '<<(*i)->get_metavar(); - } - else if(any_short) - swtch<<" "; - if(!(*i)->get_long().empty()) - { - swtch<<"--"<<(*i)->get_long(); - - if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch<<"[="<<(*i)->get_metavar()<<']'; - else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch<<'='<<(*i)->get_metavar(); - } - switches.push_back(swtch.str()); - maxw=max(maxw, switches.back().size()); + if((*i)->is_list_store()) + have_list = true; + else if((*i)->get_type()==OPTIONAL_ARG) + have_optional = true; } - 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'; - } - - return result; -} + if(have_optional && (t.is_list() || a!=OPTIONAL_ARG)) + throw invalid_argument("GetOpt::add_argument"); + if(have_list && (t.is_list() || a==OPTIONAL_ARG)) + throw invalid_argument("GetOpt::add_argument"); -void GetOpt::operator()(unsigned argc, const char *const *argv) -{ - unsigned i=1; - for(; i::iterator i=opts.begin(); i!=opts.end(); ++i) + for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i) if((*i)->get_short()==s) return **i; - throw UsageError(string("Unknown option -")+s); + 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) + for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i) if((*i)->get_long()==l) return **i; - throw UsageError(string("Unknown option --")+l); + throw usage_error(string("Unknown option --")+l); } -/** -Processes the given argument as a long option. +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(ArgumentList::const_iterator 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(iget_short()) + { + 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()); + + 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()); + } + result += ']'; + } + } + + for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i) + { + result += ' '; + if((*i)->get_type()==OPTIONAL_ARG) + result += '['; + result += format("<%s>", (*i)->get_name()); + if((*i)->is_list_store()) + result += " ..."; + if((*i)->get_type()==OPTIONAL_ARG) + result += ']'; + } + + return result; +} + +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(); + + string::size_type maxw = 0; + list switches; + for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i) + { + string swtch; + if((*i)->get_short()) + { + swtch += format("-%c", (*i)->get_short()); + if(!(*i)->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(any_short) + swtch += " "; + if(!(*i)->get_long().empty()) + { + swtch += format("--%s", (*i)->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()); + } + switches.push_back(swtch); + maxw = max(maxw, swtch.size()); + } + + list pargs; + for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i) + { + string parg = format("<%s>", (*i)->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) + 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) + 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::OptBase &GetOpt::OptBase::set_help(const string &h) +GetOpt::OptionImpl::~OptionImpl() +{ + delete store; +} + +GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h) +{ + help = h; + return *this; +} + +GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h, const string &m) { - help=h; + help = h; + metavar = m; return *this; } -GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m) +GetOpt::OptionImpl &GetOpt::OptionImpl::bind_seen_count(unsigned &c) { - help=h; - metavar=m; + ext_seen_count = &c; return *this; } -void GetOpt::OptBase::process() +void GetOpt::OptionImpl::process() { if(arg_type==REQUIRED_ARG) - throw UsageError("--"+lng+" requires an argument"); + 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 UsageError("--"+lng+" takes no argument"); + throw usage_error("--"+lng+" takes no argument"); + ++seen_count; + if(ext_seen_count) + *ext_seen_count = seen_count; + + 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; +} - store(arg); +void GetOpt::ArgumentImpl::process(const string &arg) +{ + try + { + store->store(arg); + } + catch(const exception &e) + { + throw usage_error("Invalid "+name+" ("+e.what()+")"); + } } } // namespace Msp