cmCommandArgumentsHelper.h 6.3 KB

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