cmCommandLineArgument.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmStringAlgorithms.h"
  5. #include "cmSystemTools.h"
  6. template <typename FunctionSignature>
  7. struct cmCommandLineArgument
  8. {
  9. enum class Values
  10. {
  11. Zero,
  12. One,
  13. Two,
  14. ZeroOrOne,
  15. OneOrMore
  16. };
  17. std::string InvalidSyntaxMessage;
  18. std::string InvalidValueMessage;
  19. std::string Name;
  20. Values Type;
  21. std::function<FunctionSignature> StoreCall;
  22. template <typename FunctionType>
  23. cmCommandLineArgument(std::string n, Values t, FunctionType&& func)
  24. : InvalidSyntaxMessage(cmStrCat("Invalid syntax used with ", n))
  25. , InvalidValueMessage(cmStrCat("Invalid value used with ", n))
  26. , Name(std::move(n))
  27. , Type(t)
  28. , StoreCall(std::forward<FunctionType>(func))
  29. {
  30. }
  31. template <typename FunctionType>
  32. cmCommandLineArgument(std::string n, std::string failedMsg, Values t,
  33. FunctionType&& func)
  34. : InvalidSyntaxMessage(cmStrCat("Invalid syntax used with ", n))
  35. , InvalidValueMessage(std::move(failedMsg))
  36. , Name(std::move(n))
  37. , Type(t)
  38. , StoreCall(std::forward<FunctionType>(func))
  39. {
  40. }
  41. bool matches(std::string const& input) const
  42. {
  43. return (this->Type == Values::Zero) ? (input == this->Name)
  44. : cmHasPrefix(input, this->Name);
  45. }
  46. template <typename T, typename... CallState>
  47. bool parse(std::string const& input, T& index,
  48. std::vector<std::string> const& allArgs,
  49. CallState&&... state) const
  50. {
  51. enum class ParseMode
  52. {
  53. Valid,
  54. Invalid,
  55. SyntaxError,
  56. ValueError
  57. };
  58. ParseMode parseState = ParseMode::Valid;
  59. if (this->Type == Values::Zero) {
  60. if (input.size() == this->Name.size()) {
  61. parseState =
  62. this->StoreCall(std::string{}, std::forward<CallState>(state)...)
  63. ? ParseMode::Valid
  64. : ParseMode::Invalid;
  65. } else {
  66. parseState = ParseMode::SyntaxError;
  67. }
  68. } else if (this->Type == Values::One || this->Type == Values::ZeroOrOne) {
  69. if (input.size() == this->Name.size()) {
  70. auto nextValueIndex = index + 1;
  71. if (nextValueIndex >= allArgs.size() ||
  72. allArgs[nextValueIndex][0] == '-') {
  73. if (this->Type == Values::ZeroOrOne) {
  74. parseState =
  75. this->StoreCall(std::string{}, std::forward<CallState>(state)...)
  76. ? ParseMode::Valid
  77. : ParseMode::Invalid;
  78. } else {
  79. parseState = ParseMode::ValueError;
  80. }
  81. } else {
  82. parseState = this->StoreCall(allArgs[nextValueIndex],
  83. std::forward<CallState>(state)...)
  84. ? ParseMode::Valid
  85. : ParseMode::Invalid;
  86. index = nextValueIndex;
  87. }
  88. } else {
  89. // parse the string to get the value
  90. auto possible_value = cm::string_view(input).substr(this->Name.size());
  91. if (possible_value.empty()) {
  92. parseState = ParseMode::ValueError;
  93. } else if (possible_value[0] == '=') {
  94. possible_value.remove_prefix(1);
  95. if (possible_value.empty()) {
  96. parseState = ParseMode::ValueError;
  97. }
  98. }
  99. if (parseState == ParseMode::Valid) {
  100. parseState = this->StoreCall(std::string(possible_value),
  101. std::forward<CallState>(state)...)
  102. ? ParseMode::Valid
  103. : ParseMode::Invalid;
  104. }
  105. }
  106. } else if (this->Type == Values::Two) {
  107. if (input.size() == this->Name.size()) {
  108. if (index + 2 >= allArgs.size() || allArgs[index + 1][0] == '-' ||
  109. allArgs[index + 2][0] == '-') {
  110. parseState = ParseMode::ValueError;
  111. } else {
  112. index += 2;
  113. parseState =
  114. this->StoreCall(cmStrCat(allArgs[index - 1], ";", allArgs[index]),
  115. std::forward<CallState>(state)...)
  116. ? ParseMode::Valid
  117. : ParseMode::Invalid;
  118. }
  119. }
  120. } else if (this->Type == Values::OneOrMore) {
  121. if (input.size() == this->Name.size()) {
  122. auto nextValueIndex = index + 1;
  123. if (nextValueIndex >= allArgs.size() ||
  124. allArgs[nextValueIndex][0] == '-') {
  125. parseState = ParseMode::ValueError;
  126. } else {
  127. std::string buffer = allArgs[nextValueIndex++];
  128. while (nextValueIndex < allArgs.size() &&
  129. allArgs[nextValueIndex][0] != '-') {
  130. buffer = cmStrCat(buffer, ";", allArgs[nextValueIndex++]);
  131. }
  132. parseState =
  133. this->StoreCall(buffer, std::forward<CallState>(state)...)
  134. ? ParseMode::Valid
  135. : ParseMode::Invalid;
  136. index = (nextValueIndex - 1);
  137. }
  138. } else {
  139. parseState = ParseMode::SyntaxError;
  140. }
  141. }
  142. if (parseState == ParseMode::SyntaxError) {
  143. cmSystemTools::Error(this->InvalidSyntaxMessage);
  144. } else if (parseState == ParseMode::ValueError) {
  145. cmSystemTools::Error(this->InvalidValueMessage);
  146. }
  147. return (parseState == ParseMode::Valid);
  148. }
  149. };