Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

299 linhas
8.2 KiB

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