The default formatter splits the line 40/40, which produces several unnecessary linebreaks for setup.exe and clutters the usage output. Implement a 35/45 split by default and provide an alternative constructor that enables adjustment of these parameters if necessary.
>From 4989f5b105875e635b6043f0c306601ca4bec21d Mon Sep 17 00:00:00 2001 From: Achim Gratz <strom...@stromeko.de> Date: Sat, 10 Aug 2013 15:05:30 +0200 Subject: [PATCH] split line 35/45 by default to have less linebreaks * include/getopt++/DefaultFormatter.h (DefaultFormatter): Introduce private data members and use them, adjust comment accordingly. Split the line 35/45 instead of 40/40 by default to get less linebreaks. Add default initializers for data members to the constructor. Add a second constructor that offers parameters for initialization of the private data members. (DefaultFormatter::operator ()): Remove automatic variable output and directly put the data on the output stream instead. Use private data members to construct the output rather than magic constants. --- include/getopt++/DefaultFormatter.h | 44 +++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/include/getopt++/DefaultFormatter.h b/include/getopt++/DefaultFormatter.h index ced6956..38287f1 100644 --- a/include/getopt++/DefaultFormatter.h +++ b/include/getopt++/DefaultFormatter.h @@ -21,31 +21,47 @@ #include "getopt++/Option.h" /* Show the options on the left, the short description on the right. - * descriptions must be < 40 characters in length + * Option display must be < o_len characters in length. + * Descriptions must be < h_len characters in length. + * For compatibility with default terminal width o_len + h_len <= 80. */ class DefaultFormatter { + private: + const unsigned int o_len; + const unsigned int h_len; + const std::string s_lead; + const std::string l_lead; public: - DefaultFormatter (std::ostream &aStream) : theStream(aStream) {} + DefaultFormatter (std::ostream &aStream) + : o_len(35), h_len(45), + s_lead(" -"), l_lead(" --"), + theStream(aStream) + {} + DefaultFormatter (std::ostream &aStream, + unsigned int o_len, unsigned int h_len, + std::string s_lead, std::string l_lead) + : o_len(o_len), h_len(h_len), + s_lead(s_lead), l_lead(l_lead), + theStream(aStream) + {} void operator () (Option *anOption) { - std::string output = std::string() + " -" + anOption->shortOption ()[0]; - output += " --" ; - output += anOption->longOption (); - output += std::string (40 - output.size(), ' '); + theStream << s_lead << anOption->shortOption ()[0] + << l_lead << anOption->longOption () + << std::string (o_len + - s_lead.size () - 1 - l_lead.size () + - anOption->longOption ().size (), ' '); std::string helpmsg = anOption->shortHelp(); - while (helpmsg.size() > 40) + while (helpmsg.size() > h_len) { // TODO: consider using a line breaking strategy here. - int pos = helpmsg.substr(0,40).find_last_of(" "); - output += helpmsg.substr(0,pos); + int pos = helpmsg.substr(0,h_len).find_last_of(" "); + theStream << helpmsg.substr(0,pos) + << std::endl << std::string (o_len, ' '); helpmsg.erase (0,pos+1); - theStream << output << std::endl; - output = std::string (40, ' '); } - output += helpmsg; - theStream << output << std::endl; + theStream << helpmsg << std::endl; } std::ostream &theStream; }; - #endif // _GETOPT___DEFAULTFORMATTER_H_ -- 1.8.3.1
Regards, Achim. -- +<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+ Samples for the Waldorf Blofeld: http://Synth.Stromeko.net/Downloads.html#BlofeldSamplesExtra