Option.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //
  2. // Option.h
  3. //
  4. // Library: Util
  5. // Package: Options
  6. // Module: Option
  7. //
  8. // Definition of the Option 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_Option_INCLUDED
  16. #define Util_Option_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #include "Poco/Util/OptionCallback.h"
  19. namespace Poco {
  20. namespace Util {
  21. class Application;
  22. class Validator;
  23. class AbstractConfiguration;
  24. class Util_API Option
  25. /// This class represents and stores the properties
  26. /// of a command line option.
  27. ///
  28. /// An option has a full name, an optional short name,
  29. /// a description (used for printing a usage statement),
  30. /// and an optional argument name.
  31. /// An option can be optional or required.
  32. /// An option can be repeatable, which means that it can
  33. /// be given more than once on the command line.
  34. ///
  35. /// An option can be part of an option group. At most one
  36. /// option of each group may be specified on the command
  37. /// line.
  38. ///
  39. /// An option can be bound to a configuration property.
  40. /// In this case, a configuration property will automatically
  41. /// receive the option's argument value.
  42. ///
  43. /// A callback method can be specified for options. This method
  44. /// is called whenever an option is specified on the command line.
  45. ///
  46. /// Option argument values can be automatically validated using a
  47. /// Validator.
  48. ///
  49. /// Option instances are value objects.
  50. ///
  51. /// Typically, after construction, an Option object is immediately
  52. /// passed to an Options object.
  53. ///
  54. /// An Option object can be created by chaining the constructor
  55. /// with any of the setter methods, as in the following example:
  56. ///
  57. /// Option versionOpt("include", "I", "specify an include directory")
  58. /// .required(false)
  59. /// .repeatable(true)
  60. /// .argument("directory");
  61. {
  62. public:
  63. Option();
  64. /// Creates an empty Option.
  65. Option(const Option& option);
  66. /// Creates an option from another one.
  67. Option(const std::string& fullName, const std::string& shortName);
  68. /// Creates an option with the given properties.
  69. Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required = false);
  70. /// Creates an option with the given properties.
  71. Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required, const std::string& argName, bool argRequired = false);
  72. /// Creates an option with the given properties.
  73. ~Option();
  74. /// Destroys the Option.
  75. Option& operator = (const Option& option);
  76. /// Assignment operator.
  77. void swap(Option& option);
  78. /// Swaps the option with another one.
  79. Option& shortName(const std::string& name);
  80. /// Sets the short name of the option.
  81. Option& fullName(const std::string& name);
  82. /// Sets the full name of the option.
  83. Option& description(const std::string& text);
  84. /// Sets the description of the option.
  85. Option& required(bool flag);
  86. /// Sets whether the option is required (flag == true)
  87. /// or optional (flag == false).
  88. Option& repeatable(bool flag);
  89. /// Sets whether the option can be specified more than once
  90. /// (flag == true) or at most once (flag == false).
  91. Option& argument(const std::string& name, bool required = true);
  92. /// Specifies that the option takes an (optional or required)
  93. /// argument.
  94. Option& noArgument();
  95. /// Specifies that the option does not take an argument (default).
  96. Option& group(const std::string& group);
  97. /// Specifies the option group the option is part of.
  98. Option& binding(const std::string& propertyName);
  99. /// Binds the option to the configuration property with the given name.
  100. ///
  101. /// The configuration will automatically receive the option's argument.
  102. Option& binding(const std::string& propertyName, AbstractConfiguration* pConfig);
  103. /// Binds the option to the configuration property with the given name,
  104. /// using the given AbstractConfiguration.
  105. ///
  106. /// The configuration will automatically receive the option's argument.
  107. Option& callback(const AbstractOptionCallback& cb);
  108. /// Binds the option to the given method.
  109. ///
  110. /// The callback method will be called when the option
  111. /// has been specified on the command line.
  112. ///
  113. /// Usage:
  114. /// callback(OptionCallback<MyApplication>(this, &MyApplication::myCallback));
  115. Option& validator(Validator* pValidator);
  116. /// Sets the validator for the given option.
  117. ///
  118. /// The Option takes ownership of the Validator and
  119. /// deletes it when it's no longer needed.
  120. const std::string& shortName() const;
  121. /// Returns the short name of the option.
  122. const std::string& fullName() const;
  123. /// Returns the full name of the option.
  124. const std::string& description() const;
  125. /// Returns the description of the option.
  126. bool required() const;
  127. /// Returns true if the option is required, false if not.
  128. bool repeatable() const;
  129. /// Returns true if the option can be specified more than
  130. /// once, or false if at most once.
  131. bool takesArgument() const;
  132. /// Returns true if the options takes an (optional) argument.
  133. bool argumentRequired() const;
  134. /// Returns true if the argument is required.
  135. const std::string& argumentName() const;
  136. /// Returns the argument name, if specified.
  137. const std::string& group() const;
  138. /// Returns the option group the option is part of,
  139. /// or an empty string, if the option is not part of
  140. /// a group.
  141. const std::string& binding() const;
  142. /// Returns the property name the option is bound to,
  143. /// or an empty string in case it is not bound.
  144. AbstractOptionCallback* callback() const;
  145. /// Returns a pointer to the callback method for the option,
  146. /// or NULL if no callback has been specified.
  147. Validator* validator() const;
  148. /// Returns the option's Validator, if one has been specified,
  149. /// or NULL otherwise.
  150. AbstractConfiguration* config() const;
  151. /// Returns the configuration, if specified, or NULL otherwise.
  152. bool matchesShort(const std::string& option) const;
  153. /// Returns true if the given option string matches the
  154. /// short name.
  155. ///
  156. /// The first characters of the option string must match
  157. /// the short name of the option (case sensitive),
  158. /// or the option string must partially match the full
  159. /// name (case insensitive).
  160. bool matchesFull(const std::string& option) const;
  161. /// Returns true if the given option string matches the
  162. /// full name.
  163. ///
  164. /// The option string must match the full
  165. /// name (case insensitive).
  166. bool matchesPartial(const std::string& option) const;
  167. /// Returns true if the given option string partially matches the
  168. /// full name.
  169. ///
  170. /// The option string must partially match the full
  171. /// name (case insensitive).
  172. void process(const std::string& option, std::string& arg) const;
  173. /// Verifies that the given option string matches the
  174. /// requirements of the option, and extracts the option argument,
  175. /// if present.
  176. ///
  177. /// If the option string is okay and carries an argument,
  178. /// the argument is returned in arg.
  179. ///
  180. /// Throws a MissingArgumentException if a required argument
  181. /// is missing. Throws an UnexpectedArgumentException if an
  182. /// argument has been found, but none is expected.
  183. private:
  184. std::string _shortName;
  185. std::string _fullName;
  186. std::string _description;
  187. bool _required;
  188. bool _repeatable;
  189. std::string _argName;
  190. bool _argRequired;
  191. std::string _group;
  192. std::string _binding;
  193. Validator* _pValidator;
  194. AbstractOptionCallback* _pCallback;
  195. AbstractConfiguration* _pConfig;
  196. };
  197. //
  198. // inlines
  199. //
  200. inline const std::string& Option::shortName() const
  201. {
  202. return _shortName;
  203. }
  204. inline const std::string& Option::fullName() const
  205. {
  206. return _fullName;
  207. }
  208. inline const std::string& Option::description() const
  209. {
  210. return _description;
  211. }
  212. inline bool Option::required() const
  213. {
  214. return _required;
  215. }
  216. inline bool Option::repeatable() const
  217. {
  218. return _repeatable;
  219. }
  220. inline bool Option::takesArgument() const
  221. {
  222. return !_argName.empty();
  223. }
  224. inline bool Option::argumentRequired() const
  225. {
  226. return _argRequired;
  227. }
  228. inline const std::string& Option::argumentName() const
  229. {
  230. return _argName;
  231. }
  232. inline const std::string& Option::group() const
  233. {
  234. return _group;
  235. }
  236. inline const std::string& Option::binding() const
  237. {
  238. return _binding;
  239. }
  240. inline AbstractOptionCallback* Option::callback() const
  241. {
  242. return _pCallback;
  243. }
  244. inline Validator* Option::validator() const
  245. {
  246. return _pValidator;
  247. }
  248. inline AbstractConfiguration* Option::config() const
  249. {
  250. return _pConfig;
  251. }
  252. } } // namespace Poco::Util
  253. #endif // Util_Option_INCLUDED