X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.cpp;h=b472c58b835a94e27fa94046c488e9777096b60f;hp=498977569e2179ee203de59f0e1624b536031f70;hb=967785734be5c3fc6f75da122c2d93ebbb338271;hpb=1f262a37bf2c0531f7474df3cdb60d608bfdbba1 diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index 4989775..b472c58 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -1,28 +1,133 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ #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) delete *i; } -/** -Generates a single line that gives an overview about the known options. +GetOpt::OptBase &GetOpt::get_option(char s) +{ + for(list::iterator i=opts.begin(); i!=opts.end(); ++i) + if((*i)->get_short()==s) + return **i; + throw usage_error(string("Unknown option -")+s); +} -@param argv0 The program name to be used in the usage string +GetOpt::OptBase &GetOpt::get_option(const string &l) +{ + for(list::iterator i=opts.begin(); i!=opts.end(); ++i) + if((*i)->get_long()==l) + return **i; + throw usage_error(string("Unknown option --")+l); +} + +void GetOpt::operator()(unsigned argc, const char *const *argv) +{ + try + { + unsigned i = 1; + for(; i::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i) - any_short=(*i)->get_short(); + any_short = (*i)->get_short(); - string::size_type maxw=0; + string::size_type maxw = 0; list switches; for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i) { @@ -93,138 +194,22 @@ string GetOpt::generate_help() const swtch<<'='<<(*i)->get_metavar(); } switches.push_back(swtch.str()); - maxw=max(maxw, switches.back().size()); + maxw = max(maxw, switches.back().size()); } string result; - list::const_iterator j=switches.begin(); + 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 += " "+*j; + result += string(maxw+2-j->size(), ' '); + result += (*i)->get_help(); + result += '\n'; } return result; } -void GetOpt::operator()(unsigned argc, const char *const *argv) -{ - unsigned i=1; - for(; i::iterator i=opts.begin(); i!=opts.end(); ++i) - if((*i)->get_short()==s) - return **i; - throw UsageError(string("Unknown option -")+s); -} - -GetOpt::OptBase &GetOpt::get_option(const string &l) -{ - for(list::iterator i=opts.begin(); i!=opts.end(); ++i) - if((*i)->get_long()==l) - return **i; - throw UsageError(string("Unknown option --")+l); -} - -/** -Processes the given argument as a long option. - -@param argp Pointer to the argument - -@return The number of arguments eaten (1 or 2) -*/ -unsigned GetOpt::process_long(const char *const *argp) -{ - // Skip the -- - const char *arg=argp[0]+2; - - // See if the argument contains an = - unsigned equals=0; - for(; arg[equals] && arg[equals]!='='; ++equals) ; - - OptBase &opt=get_option(string(arg, equals)); - - if(arg[equals]) - // Process the part after the = as option argument - opt.process(arg+equals+1); - else if(opt.get_arg_type()==REQUIRED_ARG) - { - if(!argp[1]) - throw UsageError("Premature end of arguments"); - - // Process the next argument as option argument - opt.process(argp[1]); - return 2; - } - else - opt.process(); - - return 1; -} - -/** -Processes short options from the given argument. - -@param argp Pointer to the argument - -@return The number of arguments eaten (1 or 2) -*/ -unsigned GetOpt::process_short(const char *const *argp) -{ - // Skip the - - const char *arg=argp[0]+1; - - // Loop through all characters in the argument - for(; *arg; ++arg) - { - OptBase &opt=get_option(*arg); - - if(arg[1] && opt.get_arg_type()!=NO_ARG) - { - // Need an option argument and we have characters left - use them - opt.process(arg+1); - return 1; - } - else if(opt.get_arg_type()==REQUIRED_ARG) - { - if(!argp[1]) - throw UsageError("Premature end of arguments"); - - // Use the next argument as option argument - opt.process(argp[1]); - return 2; - } - else - opt.process(); - } - - return 1; -} - GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a): shrt(s), @@ -236,21 +221,21 @@ GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a): GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h) { - help=h; + help = h; return *this; } GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m) { - help=h; - metavar=m; + help = h; + metavar = m; return *this; } void GetOpt::OptBase::process() { if(arg_type==REQUIRED_ARG) - throw UsageError("--"+lng+" requires an argument"); + throw usage_error("--"+lng+" requires an argument"); ++seen_count; store(); @@ -259,7 +244,7 @@ void GetOpt::OptBase::process() void GetOpt::OptBase::process(const string &arg) { if(arg_type==NO_ARG) - throw UsageError("--"+lng+" takes no argument"); + throw usage_error("--"+lng+" takes no argument"); ++seen_count; store(arg);