Arguments.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #ifndef PARSER_H
  2. #define PARSER_H
  3. //==============================================================================
  4. //
  5. // Arguments - the argument parser class
  6. //
  7. // Copyright (C) 2018 Dick van Oudheusden
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Lesser General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 3 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. // Lesser General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU Lesser General Public
  20. // License along with this library; if not, write to the Free
  21. // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. //
  23. //==============================================================================
  24. #include <string>
  25. #include <vector>
  26. namespace arg
  27. {
  28. ///
  29. /// @class Argument
  30. ///
  31. /// @brief The base argument class.
  32. ///
  33. class Arguments;
  34. class Argument
  35. {
  36. public:
  37. ///
  38. /// Constructor for a switch argument
  39. ///
  40. /// @param arguments the arguments
  41. /// @param optional the optional indication
  42. /// @param shortOption the short option
  43. /// @param longOption the long option
  44. /// @param help the help text
  45. ///
  46. Argument(Arguments &arguments, bool optional, char shortOption, const std::string &longOption, const std::string &help);
  47. ///
  48. /// Constructor for a value argument
  49. ///
  50. /// @param arguments the arguments
  51. /// @param optional the optional indication
  52. /// @param shortOption the short option
  53. /// @param longOption the long option
  54. /// @param valueInfo the value info
  55. /// @param helpInfo the help info
  56. /// @param initial the initial value
  57. ///
  58. Argument(Arguments &arguments, bool optional, char shortOption, const std::string &longOption, const std::string &valueInfo, const std::string &helpInfo, const std::string &initial);
  59. ///
  60. /// Deconstructor
  61. ///
  62. virtual ~Argument();
  63. public:
  64. // Properties
  65. ///
  66. /// Return the optional indication
  67. ///
  68. /// @return the optional indication
  69. ///
  70. bool optional() const { return _optional; }
  71. ///
  72. /// Return the used indication
  73. ///
  74. /// @return the indication
  75. ///
  76. bool active() const { return _active; }
  77. ///
  78. /// Set the used indication
  79. ///
  80. /// @param the used indication
  81. ///
  82. void active(bool active) { _active = active; }
  83. ///
  84. /// Return the short option
  85. ///
  86. /// @return the short option
  87. ///
  88. char shortOption() const { return _shortOption; }
  89. ///
  90. /// Return the long option
  91. ///
  92. /// @return the long option
  93. ///
  94. const std::string &longOption() const { return _longOption; }
  95. ///
  96. /// Return the long option info
  97. ///
  98. /// @return the long option info
  99. ///
  100. const std::string &valueInfo() const { return _valueInfo; }
  101. ///
  102. /// Return the indication if the options needs a value
  103. ///
  104. /// @return the indication if the options needs a value
  105. ///
  106. virtual bool withValue() const { return _withValue; }
  107. ///
  108. /// Set the value for the argument
  109. ///
  110. /// @param the value
  111. ///
  112. virtual void value(const std::string &value) { _value = value; }
  113. ///
  114. /// Get the value of the argument
  115. ///
  116. /// @return the value
  117. ///
  118. virtual std::string value() const { return _value; }
  119. ///
  120. /// Print the argument help text
  121. ///
  122. /// @param column the column number for the help text
  123. ///
  124. void print(int column) const;
  125. ///
  126. /// Print an argument help text
  127. ///
  128. /// @param shortOption the short option character
  129. /// @param longOption the long option
  130. /// @param valueInfo the value info text
  131. /// @param helpInfo the help text
  132. /// @param column the column for the help text
  133. ///
  134. static void print(char shortOption, const std::string &longOption, const std::string &valueInfo, const std::string &helpInfo, int column);
  135. ///
  136. /// Return the option width
  137. ///
  138. /// @return the option width
  139. ///
  140. virtual int optionWidth() const;
  141. private:
  142. bool _optional;
  143. bool _withValue;
  144. bool _active;
  145. char _shortOption;
  146. std::string _longOption;
  147. std::string _valueInfo;
  148. std::string _value;
  149. std::string _helpInfo;
  150. // Disable copy constructors
  151. Argument(const Argument &);
  152. Argument& operator=(const Argument &);
  153. };
  154. ///
  155. /// @class Arguments
  156. ///
  157. /// @brief The arguments parser class.
  158. ///
  159. class Arguments
  160. {
  161. public:
  162. ///
  163. /// Constructor
  164. ///
  165. /// @param usage the usage text
  166. /// @param version the version of the program
  167. /// @param help the program help text
  168. ///
  169. Arguments(const std::string &usage, const std::string &version, const std::string &help);
  170. ///
  171. /// Deconstructor
  172. ///
  173. virtual ~Arguments();
  174. ///
  175. /// Add an argument to the arguments parser
  176. ///
  177. /// @param argument an argument
  178. ///
  179. void add(Argument &argument);
  180. ///
  181. /// Parse the arguments
  182. ///
  183. /// @param argc the number of command line arguments
  184. /// @param argv the command line arguments
  185. /// @param unprocessed the unprocessed command line arguments
  186. ///
  187. /// @return success
  188. ///
  189. bool parse(int argc, char *argv[], std::vector<std::string> &unprocessed);
  190. ///
  191. /// Print the help
  192. ///
  193. void printHelp() const;
  194. private:
  195. bool parseShortOption(int argc, char *argv[], int &arg);
  196. bool parseLongOption(int argc, char *argv[], int arg);
  197. bool checkRequiredArgument(Argument *argument);
  198. std::string _usage;
  199. std::string _version;
  200. std::string _help;
  201. std::vector<Argument *> _arguments;
  202. // Disable copy constructors
  203. Arguments(const Arguments &);
  204. Arguments& operator=(const Arguments &);
  205. };
  206. }
  207. #endif