X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.cpp;h=fdeb2f8827b2a48075068d153d1cea2597f6ccb1;hp=e4f00ffceeeda967d8c16357a32269c1dbe77634;hb=6f0c4c39133e08c05ffb1f1d53141a7e80ba6351;hpb=0fe166c896d2b7d7214e0c9c4add53e47d2ace88 diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index e4f00ff..fdeb2f8 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -1,158 +1,105 @@ -/* $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 { -/** -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(): + help(false) { - 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(); + add_option("help", help, NO_ARG).set_help("Displays this help"); } -/** -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::~GetOpt() { - 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'; - - result+=line.str(); - } - - return result; + for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i) + delete *i; } -void GetOpt::operator()(unsigned argc, const char *const *argv) +GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a) { - unsigned i=1; - for(; iget_short()==s) || (*i)->get_long()==l) { - if(argv[i][1]=='-') - { - if(!argv[i][2]) - break; - - i+=process_long(argv+i); - } - else - i+=process_short(argv+i); + delete *i; + opts.erase(i++); } else - args.push_back(argv[i++]); + ++i; } - - for(; i::iterator i=opts.begin(); i!=opts.end(); ++i) - delete *i; + opts.push_back(new OptionImpl(s, l, t, a)); + return *opts.back(); } -GetOpt::OptBase &GetOpt::get_option(char s) +GetOpt::OptionImpl &GetOpt::get_option(char s) { - for(list::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 + { + unsigned i = 1; + for(; 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 += ']'; + } + + 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()); + } + + string result; + 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()); + + return result; +} + + +GetOpt::OptionImpl::OptionImpl(char s, const std::string &l, const Store &t, ArgType a): + shrt(s), + lng(l), + arg_type(a), + seen_count(0), + ext_seen_count(0), + metavar("ARG"), + store(t.clone()) +{ } + +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; + metavar = m; + return *this; +} + +GetOpt::OptionImpl &GetOpt::OptionImpl::bind_seen_count(unsigned &c) +{ + ext_seen_count = &c; + return *this; +} + +void GetOpt::OptionImpl::process() +{ + if(arg_type==REQUIRED_ARG) + throw usage_error("--"+lng+" requires an argument"); + + ++seen_count; + if(ext_seen_count) + *ext_seen_count = seen_count; + + try + { + store->store(); + } + catch(const exception &e) + { + throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")"); + } +} + +void GetOpt::OptionImpl::process(const string &arg) +{ + if(arg_type==NO_ARG) + 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()+")"); + } +} + } // namespace Msp