cmCommandArgumentsHelper.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmCommandArgumentsHelper_h
  4. #define cmCommandArgumentsHelper_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <set>
  7. #include <string>
  8. #include <vector>
  9. class cmCommandArgumentGroup;
  10. class cmCommandArgumentsHelper;
  11. /* cmCommandArgumentsHelper, cmCommandArgumentGroup and cmCommandArgument (i.e.
  12. its derived classes cmCAXXX can be used to simplify the processing of
  13. arguments to cmake commands. Maybe they can also be used to generate
  14. documentation.
  15. For every argument supported by a command one cmCommandArgument is created
  16. and added to cmCommandArgumentsHelper. cmCommand has a cmCommandArgumentsHelper
  17. as member variable so this should be used.
  18. The order of the arguments is defined using the Follows(arg) method. It says
  19. that this argument follows immediateley the given argument. It can be used
  20. with multiple arguments if the argument can follow after different arguments.
  21. Arguments can be arranged in groups using cmCommandArgumentGroup. Every
  22. member of a group can follow any other member of the group. These groups
  23. can also be used to define the order.
  24. Once all arguments and groups are set up, cmCommandArgumentsHelper::Parse()
  25. is called and afterwards the values of the arguments can be evaluated.
  26. For an example see cmExportCommand.cxx.
  27. */
  28. class cmCommandArgument
  29. {
  30. public:
  31. cmCommandArgument(cmCommandArgumentsHelper* args, const char* key,
  32. cmCommandArgumentGroup* group = nullptr);
  33. virtual ~cmCommandArgument() {}
  34. /// this argument may follow after arg. 0 means it comes first.
  35. void Follows(const cmCommandArgument* arg);
  36. /// this argument may follow after any of the arguments in the given group
  37. void FollowsGroup(const cmCommandArgumentGroup* group);
  38. /// Returns true if the argument was found in the argument list
  39. bool WasFound() const { return this->WasActive; }
  40. // The following methods are only called from
  41. // cmCommandArgumentsHelper::Parse(), but making this a friend would
  42. // give it access to everything
  43. /// Make the current argument the currently active argument
  44. void Activate();
  45. /// Consume the current string
  46. bool Consume(const std::string& arg);
  47. /// Return true if this argument may follow after the given argument.
  48. bool MayFollow(const cmCommandArgument* current) const;
  49. /** Returns true if the given key matches the key for this argument.
  50. If this argument has an empty key everything matches. */
  51. bool KeyMatches(const std::string& key) const;
  52. /// Make this argument follow all members of the own group
  53. void ApplyOwnGroup();
  54. /// Reset argument, so it's back to its initial state
  55. void Reset();
  56. private:
  57. const char* Key;
  58. std::set<const cmCommandArgument*> ArgumentsBefore;
  59. cmCommandArgumentGroup* Group;
  60. bool WasActive;
  61. bool ArgumentsBeforeEmpty;
  62. unsigned int CurrentIndex;
  63. virtual bool DoConsume(const std::string& arg, unsigned int index) = 0;
  64. virtual void DoReset() = 0;
  65. };
  66. /** cmCAStringVector is to be used for arguments which can consist of more
  67. than one string, e.g. the FILES argument in INSTALL(FILES f1 f2 f3 ...). */
  68. class cmCAStringVector : public cmCommandArgument
  69. {
  70. public:
  71. cmCAStringVector(cmCommandArgumentsHelper* args, const char* key,
  72. cmCommandArgumentGroup* group = nullptr);
  73. /// Return the vector of strings
  74. const std::vector<std::string>& GetVector() const { return this->Vector; }
  75. /** Is there a keyword which should be skipped in
  76. the arguments (e.g. ARGS for ADD_CUSTOM_COMMAND) ? */
  77. void SetIgnore(const char* ignore) { this->Ignore = ignore; }
  78. private:
  79. std::vector<std::string> Vector;
  80. unsigned int DataStart;
  81. const char* Ignore;
  82. cmCAStringVector();
  83. bool DoConsume(const std::string& arg, unsigned int index) override;
  84. void DoReset() override;
  85. };
  86. /** cmCAString is to be used for arguments which consist of one value,
  87. e.g. the executable name in ADD_EXECUTABLE(). */
  88. class cmCAString : public cmCommandArgument
  89. {
  90. public:
  91. cmCAString(cmCommandArgumentsHelper* args, const char* key,
  92. cmCommandArgumentGroup* group = nullptr);
  93. /// Return the string
  94. const std::string& GetString() const { return this->String; }
  95. const char* GetCString() const { return this->String.c_str(); }
  96. private:
  97. std::string String;
  98. unsigned int DataStart;
  99. bool DoConsume(const std::string& arg, unsigned int index) override;
  100. void DoReset() override;
  101. cmCAString();
  102. };
  103. /** cmCAEnabler is to be used for options which are off by default and can be
  104. enabled using a special argument, e.g. EXCLUDE_FROM_ALL in ADD_EXECUTABLE(). */
  105. class cmCAEnabler : public cmCommandArgument
  106. {
  107. public:
  108. cmCAEnabler(cmCommandArgumentsHelper* args, const char* key,
  109. cmCommandArgumentGroup* group = nullptr);
  110. /// Has it been enabled ?
  111. bool IsEnabled() const { return this->Enabled; }
  112. private:
  113. bool Enabled;
  114. bool DoConsume(const std::string& arg, unsigned int index) override;
  115. void DoReset() override;
  116. cmCAEnabler();
  117. };
  118. /** cmCADisable is to be used for options which are on by default and can be
  119. disabled using a special argument.*/
  120. class cmCADisabler : public cmCommandArgument
  121. {
  122. public:
  123. cmCADisabler(cmCommandArgumentsHelper* args, const char* key,
  124. cmCommandArgumentGroup* group = nullptr);
  125. /// Is it still enabled ?
  126. bool IsEnabled() const { return this->Enabled; }
  127. private:
  128. bool Enabled;
  129. bool DoConsume(const std::string& arg, unsigned int index) override;
  130. void DoReset() override;
  131. cmCADisabler();
  132. };
  133. /** Group of arguments, needed for ordering. E.g. WIN32, EXCLUDE_FROM_ALL and
  134. MACSOX_BUNDLE from ADD_EXECUTABLE() are a group.
  135. */
  136. class cmCommandArgumentGroup
  137. {
  138. friend class cmCommandArgument;
  139. public:
  140. cmCommandArgumentGroup() {}
  141. /// All members of this group may follow the given argument
  142. void Follows(const cmCommandArgument* arg);
  143. /// All members of this group may follow all members of the given group
  144. void FollowsGroup(const cmCommandArgumentGroup* group);
  145. private:
  146. std::vector<cmCommandArgument*> ContainedArguments;
  147. };
  148. class cmCommandArgumentsHelper
  149. {
  150. public:
  151. /// Parse the argument list
  152. void Parse(const std::vector<std::string>* args,
  153. std::vector<std::string>* unconsumedArgs);
  154. /// Add an argument.
  155. void AddArgument(cmCommandArgument* arg);
  156. private:
  157. std::vector<cmCommandArgument*> Arguments;
  158. };
  159. #endif