OptionProcessor.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // OptionProcessor.h
  3. //
  4. // Library: Util
  5. // Package: Options
  6. // Module: OptionProcessor
  7. //
  8. // Definition of the OptionProcessor 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_OptionProcessor_INCLUDED
  16. #define Util_OptionProcessor_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #include <set>
  19. namespace Poco {
  20. namespace Util {
  21. class OptionSet;
  22. class Util_API OptionProcessor
  23. /// An OptionProcessor is used to process the command line
  24. /// arguments of an application.
  25. ///
  26. /// The process() method takes an argument from the command line.
  27. /// If that argument starts with an option prefix, the argument
  28. /// is further processed. Otherwise, the argument is ignored and
  29. /// false is returned. The argument must match one of the options
  30. /// given in the OptionSet that is passed to the OptionProcessor
  31. /// with the constructor. If an option is part of a group, at most
  32. /// one option of the group can be passed to the OptionProcessor.
  33. /// Otherwise an IncompatibleOptionsException is thrown.
  34. /// If the same option is given multiple times, but the option
  35. /// is not repeatable, a DuplicateOptionException is thrown.
  36. /// If the option is not recognized, a UnexpectedArgumentException
  37. /// is thrown.
  38. /// If the option requires an argument, but none is given, an
  39. /// MissingArgumentException is thrown.
  40. /// If no argument is expected, but one is present, a
  41. /// UnexpectedArgumentException is thrown.
  42. /// If a partial option name is ambiguous, an AmbiguousOptionException
  43. /// is thrown.
  44. ///
  45. /// The OptionProcessor supports two modes: Unix mode and default mode.
  46. /// In Unix mode, the option prefix is a dash '-'. A dash must be followed
  47. /// by a short option name, or another dash, followed by a (partial)
  48. /// long option name.
  49. /// In default mode, the option prefix is a slash '/', followed by
  50. /// a (partial) long option name.
  51. /// If the special option '--' is encountered in Unix mode, all following
  52. /// options are ignored.
  53. ///
  54. /// Option arguments can be specified in three ways. If a Unix short option
  55. /// ("-o") is given, the argument directly follows the option name, without
  56. /// any delimiting character or space ("-ovalue"). In default option mode, or if a
  57. /// Unix long option ("--option") is given, the option argument is
  58. /// delimited from the option name with either an equal sign ('=') or
  59. /// a colon (':'), as in "--option=value" or "/option:value". Finally,
  60. /// a required option argument can be specified on the command line after the
  61. /// option, delimited with a space, as in "--option value" or "-o value".
  62. /// The latter only works for required option arguments, not optional ones.
  63. {
  64. public:
  65. OptionProcessor(const OptionSet& options);
  66. /// Creates the OptionProcessor, using the given OptionSet.
  67. ~OptionProcessor();
  68. /// Destroys the OptionProcessor.
  69. void setUnixStyle(bool flag);
  70. /// Enables (flag == true) or disables (flag == false) Unix-style
  71. /// option processing.
  72. ///
  73. /// If Unix-style processing is enabled, options are expected to
  74. /// begin with a single or a double dash ('-' or '--', respectively).
  75. /// A single dash must be followed by a short option name. A double
  76. /// dash must be followed by a (partial) full option name.
  77. ///
  78. /// If Unix-style processing is disabled, options are expected to
  79. /// begin with a slash ('/'), followed by a (partial) full option name.
  80. bool isUnixStyle() const;
  81. /// Returns true iff Unix-style option processing is enabled.
  82. bool process(const std::string& argument, std::string& optionName, std::string& optionArg);
  83. /// Examines and processes the given command line argument.
  84. ///
  85. /// If the argument begins with an option prefix, the option is processed
  86. /// and true is returned. The full option name is stored in optionName and the
  87. /// option argument, if present, is stored in optionArg.
  88. ///
  89. /// If the option does not begin with an option prefix, false is returned.
  90. void checkRequired() const;
  91. /// Checks if all required options have been processed.
  92. ///
  93. /// Does nothing if all required options have been processed.
  94. /// Throws a MissingOptionException otherwise.
  95. private:
  96. bool processUnix(const std::string& argument, std::string& optionName, std::string& optionArg);
  97. bool processDefault(const std::string& argument, std::string& optionName, std::string& optionArg);
  98. bool processCommon(const std::string& option, bool isShort, std::string& optionName, std::string& optionArg);
  99. const OptionSet& _options;
  100. bool _unixStyle;
  101. bool _ignore;
  102. std::set<std::string> _groups;
  103. std::set<std::string> _specifiedOptions;
  104. std::string _deferredOption;
  105. };
  106. //
  107. // inlines
  108. //
  109. inline bool OptionProcessor::isUnixStyle() const
  110. {
  111. return _unixStyle;
  112. }
  113. } } // namespace Poco::Util
  114. #endif // Util_OptionProcessor_INCLUDED