From 5e440d3753a43641b90f521849851aa7f286793c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 18 Jun 2011 21:34:43 +0300 Subject: [PATCH] Build help messages with format instead of ostringstream --- source/core/getopt.cpp | 59 ++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index b472c58..cfc9d7a 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -1,3 +1,4 @@ +#include #include "getopt.h" using namespace std; @@ -130,35 +131,33 @@ unsigned GetOpt::process_short(const char *const *argp) string GetOpt::generate_usage(const string &argv0) const { - ostringstream line; - - line<::const_iterator i=opts.begin(); i!=opts.end(); ++i) { - line<<" ["; + result += " ["; if((*i)->get_short()) { - line<<'-'<<(*i)->get_short(); + result += format("-%c", (*i)->get_short()); if(!(*i)->get_long().empty()) - line<<'|'; + result += '|'; else if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<'['<<(*i)->get_metavar()<<']'; + result += format("[%s]", (*i)->get_metavar()); else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<' '<<(*i)->get_metavar(); + result += format(" %s", (*i)->get_metavar()); } if(!(*i)->get_long().empty()) { - line<<"--"<<(*i)->get_long(); + result += format("--%s", (*i)->get_long()); if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<"[="<<(*i)->get_metavar()<<']'; + result += format("[=%s]", (*i)->get_metavar()); else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<'='<<(*i)->get_metavar(); + result += format("=%s", (*i)->get_metavar()); } - line<<']'; + result += ']'; } - return line.str(); + return result; } string GetOpt::generate_help() const @@ -171,41 +170,36 @@ string GetOpt::generate_help() const list switches; for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i) { - ostringstream swtch; + string swtch; if((*i)->get_short()) { - swtch<<'-'<<(*i)->get_short(); + swtch += format("-%c", (*i)->get_short()); if(!(*i)->get_long().empty()) - swtch<<", "; + swtch += ", "; else if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch<<'['<<(*i)->get_metavar()<<']'; + swtch += format("[%s]", (*i)->get_metavar()); else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch<<' '<<(*i)->get_metavar(); + swtch += format(" %s", (*i)->get_metavar()); } else if(any_short) - swtch<<" "; + swtch += " "; if(!(*i)->get_long().empty()) { - swtch<<"--"<<(*i)->get_long(); + swtch += format("--%s", (*i)->get_long()); if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch<<"[="<<(*i)->get_metavar()<<']'; + swtch += format("[=%s]", (*i)->get_metavar()); else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch<<'='<<(*i)->get_metavar(); + swtch += format("=%s", (*i)->get_metavar()); } - switches.push_back(swtch.str()); - maxw = max(maxw, switches.back().size()); + switches.push_back(swtch); + maxw = max(maxw, swtch.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'; - } + result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help()); return result; } @@ -217,7 +211,10 @@ GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a): arg_type(a), seen_count(0), metavar("ARG") -{ } +{ + if(lng.empty()) + throw invalid_argument("empty long option name"); +} GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h) { -- 2.43.0