HelpFormatter.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // HelpFormatter.h
  3. //
  4. // Library: Util
  5. // Package: Options
  6. // Module: HelpFormatter
  7. //
  8. // Definition of the HelpFormatter class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Util_HelpFormatter_INCLUDED
  16. #define Util_HelpFormatter_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #include <ostream>
  19. namespace Poco {
  20. namespace Util {
  21. class OptionSet;
  22. class Option;
  23. class Util_API HelpFormatter
  24. /// This class formats a help message from an OptionSet.
  25. {
  26. public:
  27. HelpFormatter(const OptionSet& options);
  28. /// Creates the HelpFormatter, using the given
  29. /// options.
  30. ///
  31. /// The HelpFormatter just stores a reference
  32. /// to the given OptionSet, so the OptionSet must not
  33. /// be destroyed during the lifetime of the HelpFormatter.
  34. ~HelpFormatter();
  35. /// Destroys the HelpFormatter.
  36. void setCommand(const std::string& command);
  37. /// Sets the command name.
  38. const std::string& getCommand() const;
  39. /// Returns the command name.
  40. void setUsage(const std::string& usage);
  41. /// Sets the usage string.
  42. const std::string& getUsage() const;
  43. /// Returns the usage string.
  44. void setHeader(const std::string& header);
  45. /// Sets the header string.
  46. const std::string& getHeader() const;
  47. /// Returns the header string.
  48. void setFooter(const std::string& footer);
  49. /// Sets the footer string.
  50. const std::string& getFooter() const;
  51. /// Returns the footer string.
  52. void format(std::ostream& ostr) const;
  53. /// Writes the formatted help text to the given stream.
  54. void setWidth(int width);
  55. /// Sets the line width for the formatted help text.
  56. int getWidth() const;
  57. /// Returns the line width for the formatted help text.
  58. ///
  59. /// The default width is 72.
  60. void setIndent(int indent);
  61. /// Sets the indentation for description continuation lines.
  62. int getIndent() const;
  63. /// Returns the indentation for description continuation lines.
  64. void setAutoIndent();
  65. /// Sets the indentation for description continuation lines so that
  66. /// the description text is left-aligned.
  67. void setUnixStyle(bool flag);
  68. /// Enables Unix-style options. Both short and long option names
  69. /// are printed if Unix-style is set. Otherwise, only long option
  70. /// names are printed.
  71. ///
  72. /// After calling setUnixStyle(), setAutoIndent() should be called
  73. /// as well to ensure proper help text formatting.
  74. bool isUnixStyle() const;
  75. /// Returns if Unix-style options are set.
  76. std::string shortPrefix() const;
  77. /// Returns the platform-specific prefix for short options.
  78. /// "-" on Unix, "/" on Windows and OpenVMS.
  79. std::string longPrefix() const;
  80. /// Returns the platform-specific prefix for long options.
  81. /// "--" on Unix, "/" on Windows and OpenVMS.
  82. protected:
  83. int calcIndent() const;
  84. /// Calculates the indentation for the option descriptions
  85. /// from the given options.
  86. void formatOptions(std::ostream& ostr) const;
  87. /// Formats all options.
  88. void formatOption(std::ostream& ostr, const Option& option, int width) const;
  89. /// Formats an option, using the platform-specific
  90. /// prefixes.
  91. void formatText(std::ostream& ostr, const std::string& text, int indent) const;
  92. /// Formats the given text.
  93. void formatText(std::ostream& ostr, const std::string& text, int indent, int firstIndent) const;
  94. /// Formats the given text.
  95. void formatWord(std::ostream& ostr, int& pos, const std::string& word, int indent) const;
  96. /// Formats the given word.
  97. void clearWord(std::ostream& ostr, int& pos, std::string& word, int indent) const;
  98. /// Formats and then clears the given word.
  99. private:
  100. HelpFormatter(const HelpFormatter&);
  101. HelpFormatter& operator = (const HelpFormatter&);
  102. const OptionSet& _options;
  103. int _width;
  104. int _indent;
  105. std::string _command;
  106. std::string _usage;
  107. std::string _header;
  108. std::string _footer;
  109. bool _unixStyle;
  110. static const int TAB_WIDTH;
  111. static const int LINE_WIDTH;
  112. };
  113. //
  114. // inlines
  115. //
  116. inline int HelpFormatter::getWidth() const
  117. {
  118. return _width;
  119. }
  120. inline int HelpFormatter::getIndent() const
  121. {
  122. return _indent;
  123. }
  124. inline const std::string& HelpFormatter::getCommand() const
  125. {
  126. return _command;
  127. }
  128. inline const std::string& HelpFormatter::getUsage() const
  129. {
  130. return _usage;
  131. }
  132. inline const std::string& HelpFormatter::getHeader() const
  133. {
  134. return _header;
  135. }
  136. inline const std::string& HelpFormatter::getFooter() const
  137. {
  138. return _footer;
  139. }
  140. inline bool HelpFormatter::isUnixStyle() const
  141. {
  142. return _unixStyle;
  143. }
  144. } } // namespace Poco::Util
  145. #endif // Util_HelpFormatter_INCLUDED