cmCommandArgumentsHelper.h 6.7 KB

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