StdOutput.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
  2. /******************************************************************************
  3. *
  4. * file: StdOutput.h
  5. *
  6. * Copyright (c) 2004, Michael E. Smoot
  7. * Copyright (c) 2017, Google LLC
  8. * All rights reserved.
  9. *
  10. * See the file COPYING in the top directory of this distribution for
  11. * more information.
  12. *
  13. * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  16. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. * DEALINGS IN THE SOFTWARE.
  20. *
  21. *****************************************************************************/
  22. #ifndef TCLAP_STDCMDLINEOUTPUT_H
  23. #define TCLAP_STDCMDLINEOUTPUT_H
  24. #include <string>
  25. #include <vector>
  26. #include <list>
  27. #include <iostream>
  28. #include <algorithm>
  29. #include <tclap/CmdLineInterface.h>
  30. #include <tclap/CmdLineOutput.h>
  31. #include <tclap/XorHandler.h>
  32. #include <tclap/Arg.h>
  33. namespace TCLAP {
  34. /**
  35. * A class that isolates any output from the CmdLine object so that it
  36. * may be easily modified.
  37. */
  38. class StdOutput : public CmdLineOutput
  39. {
  40. public:
  41. /**
  42. * Prints the usage to stdout. Can be overridden to
  43. * produce alternative behavior.
  44. * \param c - The CmdLine object the output is generated for.
  45. */
  46. virtual void usage(CmdLineInterface& c);
  47. /**
  48. * Prints the version to stdout. Can be overridden
  49. * to produce alternative behavior.
  50. * \param c - The CmdLine object the output is generated for.
  51. */
  52. virtual void version(CmdLineInterface& c);
  53. /**
  54. * Prints (to stderr) an error message, short usage
  55. * Can be overridden to produce alternative behavior.
  56. * \param c - The CmdLine object the output is generated for.
  57. * \param e - The ArgException that caused the failure.
  58. */
  59. virtual void failure(CmdLineInterface& c,
  60. ArgException& e );
  61. protected:
  62. /**
  63. * Writes a brief usage message with short args.
  64. * \param c - The CmdLine object the output is generated for.
  65. * \param os - The stream to write the message to.
  66. */
  67. void _shortUsage( CmdLineInterface& c, std::ostream& os ) const;
  68. /**
  69. * Writes a longer usage message with long and short args,
  70. * provides descriptions and prints message.
  71. * \param c - The CmdLine object the output is generated for.
  72. * \param os - The stream to write the message to.
  73. */
  74. void _longUsage( CmdLineInterface& c, std::ostream& os ) const;
  75. /**
  76. * This function inserts line breaks and indents long strings
  77. * according the params input. It will only break lines at spaces,
  78. * commas and pipes.
  79. * \param os - The stream to be printed to.
  80. * \param s - The string to be printed.
  81. * \param maxWidth - The maxWidth allowed for the output line.
  82. * \param indentSpaces - The number of spaces to indent the first line.
  83. * \param secondLineOffset - The number of spaces to indent the second
  84. * and all subsequent lines in addition to indentSpaces.
  85. */
  86. void spacePrint( std::ostream& os,
  87. const std::string& s,
  88. int maxWidth,
  89. int indentSpaces,
  90. int secondLineOffset ) const;
  91. };
  92. inline void StdOutput::version(CmdLineInterface& _cmd)
  93. {
  94. std::string progName = _cmd.getProgramName();
  95. std::string xversion = _cmd.getVersion();
  96. std::cout << std::endl << progName << " version: "
  97. << xversion << std::endl << std::endl;
  98. }
  99. inline void StdOutput::usage(CmdLineInterface& _cmd )
  100. {
  101. std::cout << std::endl << "USAGE: " << std::endl << std::endl;
  102. _shortUsage( _cmd, std::cout );
  103. std::cout << std::endl << std::endl << "Where: " << std::endl << std::endl;
  104. _longUsage( _cmd, std::cout );
  105. std::cout << std::endl;
  106. }
  107. inline void StdOutput::failure( CmdLineInterface& _cmd,
  108. ArgException& e )
  109. {
  110. std::string progName = _cmd.getProgramName();
  111. std::cerr << "PARSE ERROR: " << e.argId() << std::endl
  112. << " " << e.error() << std::endl << std::endl;
  113. if ( _cmd.hasHelpAndVersion() )
  114. {
  115. std::cerr << "Brief USAGE: " << std::endl;
  116. _shortUsage( _cmd, std::cerr );
  117. std::cerr << std::endl << "For complete USAGE and HELP type: "
  118. << std::endl << " " << progName << " "
  119. << Arg::nameStartString() << "help"
  120. << std::endl << std::endl;
  121. }
  122. else
  123. usage(_cmd);
  124. throw ExitException(1);
  125. }
  126. inline void
  127. StdOutput::_shortUsage( CmdLineInterface& _cmd,
  128. std::ostream& os ) const
  129. {
  130. std::list<Arg*> argList = _cmd.getArgList();
  131. std::string progName = _cmd.getProgramName();
  132. XorHandler xorHandler = _cmd.getXorHandler();
  133. std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
  134. std::string s = progName + " ";
  135. // first the xor
  136. for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
  137. {
  138. s += " {";
  139. for ( ArgVectorIterator it = xorList[i].begin();
  140. it != xorList[i].end(); it++ )
  141. s += (*it)->shortID() + "|";
  142. s[s.length()-1] = '}';
  143. }
  144. // then the rest
  145. for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
  146. if ( !xorHandler.contains( (*it) ) )
  147. s += " " + (*it)->shortID();
  148. // if the program name is too long, then adjust the second line offset
  149. int secondLineOffset = static_cast<int>(progName.length()) + 2;
  150. if ( secondLineOffset > 75/2 )
  151. secondLineOffset = static_cast<int>(75/2);
  152. spacePrint( os, s, 75, 3, secondLineOffset );
  153. }
  154. inline void
  155. StdOutput::_longUsage( CmdLineInterface& _cmd,
  156. std::ostream& os ) const
  157. {
  158. std::list<Arg*> argList = _cmd.getArgList();
  159. std::string message = _cmd.getMessage();
  160. XorHandler xorHandler = _cmd.getXorHandler();
  161. std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
  162. // first the xor
  163. for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
  164. {
  165. for ( ArgVectorIterator it = xorList[i].begin();
  166. it != xorList[i].end();
  167. it++ )
  168. {
  169. spacePrint( os, (*it)->longID(), 75, 3, 3 );
  170. spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
  171. if ( it+1 != xorList[i].end() )
  172. spacePrint(os, "-- OR --", 75, 9, 0);
  173. }
  174. os << std::endl << std::endl;
  175. }
  176. // then the rest
  177. for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
  178. if ( !xorHandler.contains( (*it) ) )
  179. {
  180. spacePrint( os, (*it)->longID(), 75, 3, 3 );
  181. spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
  182. os << std::endl;
  183. }
  184. os << std::endl;
  185. spacePrint( os, message, 75, 3, 0 );
  186. }
  187. inline void StdOutput::spacePrint( std::ostream& os,
  188. const std::string& s,
  189. int maxWidth,
  190. int indentSpaces,
  191. int secondLineOffset ) const
  192. {
  193. int len = static_cast<int>(s.length());
  194. if ( (len + indentSpaces > maxWidth) && maxWidth > 0 )
  195. {
  196. int allowedLen = maxWidth - indentSpaces;
  197. int start = 0;
  198. while ( start < len )
  199. {
  200. // find the substring length
  201. // int stringLen = std::min<int>( len - start, allowedLen );
  202. // doing it this way to support a VisualC++ 2005 bug
  203. using namespace std;
  204. int stringLen = min<int>( len - start, allowedLen );
  205. // trim the length so it doesn't end in middle of a word
  206. if ( stringLen == allowedLen )
  207. while ( stringLen >= 0 &&
  208. s[stringLen+start] != ' ' &&
  209. s[stringLen+start] != ',' &&
  210. s[stringLen+start] != '|' )
  211. stringLen--;
  212. // ok, the word is longer than the line, so just split
  213. // wherever the line ends
  214. if ( stringLen <= 0 )
  215. stringLen = allowedLen;
  216. // check for newlines
  217. for ( int i = 0; i < stringLen; i++ )
  218. if ( s[start+i] == '\n' )
  219. stringLen = i+1;
  220. // print the indent
  221. for ( int i = 0; i < indentSpaces; i++ )
  222. os << " ";
  223. if ( start == 0 )
  224. {
  225. // handle second line offsets
  226. indentSpaces += secondLineOffset;
  227. // adjust allowed len
  228. allowedLen -= secondLineOffset;
  229. }
  230. os << s.substr(start,stringLen) << std::endl;
  231. // so we don't start a line with a space
  232. while ( s[stringLen+start] == ' ' && start < len )
  233. start++;
  234. start += stringLen;
  235. }
  236. }
  237. else
  238. {
  239. for ( int i = 0; i < indentSpaces; i++ )
  240. os << " ";
  241. os << s << std::endl;
  242. }
  243. }
  244. } //namespace TCLAP
  245. #endif