X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fgetopt.cpp;h=74f14761c8636c19c7612cd27972ae5ac683832d;hb=d5dd704b2576f878809e87dbb8ff8591b9bdbce4;hp=e4f00ffceeeda967d8c16357a32269c1dbe77634;hpb=0fe166c896d2b7d7214e0c9c4add53e47d2ace88;p=libs%2Fcore.git diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index e4f00ff..74f1476 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -1,97 +1,41 @@ /* $Id$ This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ + #include "getopt.h" using namespace std; namespace Msp { -/** -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::~GetOpt() { - ostringstream line; - - line<::const_iterator i=opts.begin(); i!=opts.end(); ++i) - { - line<<" ["; - if((*i)->get_short()) - { - line<<'-'<<(*i)->get_short(); - if(!(*i)->get_long().empty()) - line<<'|'; - else if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<"[ARG]"; - else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<" ARG"; - } - if(!(*i)->get_long().empty()) - { - line<<"--"<<(*i)->get_long(); - - if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<"[=ARG]"; - else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<"=ARG"; - } - line<<']'; - } - - return line.str(); + for(list::iterator i=opts.begin(); i!=opts.end(); ++i) + delete *i; } -/** -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::OptBase &GetOpt::get_option(char s) { - string result; - for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i) - { - ostringstream line; - line<<" "; - if((*i)->get_short()) - { - line<<'-'<<(*i)->get_short(); - if(!(*i)->get_long().empty()) - line<<", "; - else if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<"[ARG]"; - else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<" ARG"; - } - if(!(*i)->get_long().empty()) - { - line<<"--"<<(*i)->get_long(); - - if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<"[=ARG]"; - else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<"=ARG"; - } - - line<<" "<<(*i)->get_help()<<'\n'; + for(list::iterator i=opts.begin(); i!=opts.end(); ++i) + if((*i)->get_short()==s) + return **i; + throw UsageError(string("Unknown option -")+s); +} - result+=line.str(); - } - - return result; +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); } void GetOpt::operator()(unsigned argc, const char *const *argv) { - unsigned i=1; + unsigned i = 1; for(; i::iterator i=opts.begin(); i!=opts.end(); ++i) - delete *i; -} - -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 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; + const char *arg = argp[0]+2; // See if the argument contains an = - unsigned equals=0; + unsigned equals = 0; for(; arg[equals] && arg[equals]!='='; ++equals) ; - OptBase &opt=get_option(string(arg, equals)); + OptBase &opt = get_option(string(arg, equals)); if(arg[equals]) // Process the part after the = as option argument @@ -172,22 +87,15 @@ unsigned GetOpt::process_long(const char *const *argp) 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; + const char *arg = argp[0]+1; // Loop through all characters in the argument for(; *arg; ++arg) { - OptBase &opt=get_option(*arg); + OptBase &opt = get_option(*arg); if(arg[1] && opt.get_arg_type()!=NO_ARG) { @@ -211,4 +119,126 @@ unsigned GetOpt::process_short(const char *const *argp) return 1; } +string GetOpt::generate_usage(const string &argv0) const +{ + ostringstream line; + + line<::const_iterator i=opts.begin(); i!=opts.end(); ++i) + { + line<<" ["; + if((*i)->get_short()) + { + 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(); + } + line<<']'; + } + + return line.str(); +} + +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(); + + string::size_type maxw = 0; + list switches; + for(list::const_iterator i=opts.begin(); i!=opts.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()); + } + + 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; +} + + +GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a): + shrt(s), + lng(l), + arg_type(a), + seen_count(0), + metavar("ARG") +{ } + +GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h) +{ + help = h; + return *this; +} + +GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m) +{ + help = h; + metavar = m; + return *this; +} + +void GetOpt::OptBase::process() +{ + if(arg_type==REQUIRED_ARG) + throw UsageError("--"+lng+" requires an argument"); + ++seen_count; + + store(); +} + +void GetOpt::OptBase::process(const string &arg) +{ + if(arg_type==NO_ARG) + throw UsageError("--"+lng+" takes no argument"); + ++seen_count; + + store(arg); +} + } // namespace Msp