cmArgumentParser.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmArgumentParser.h"
  4. #include <algorithm>
  5. #include "cmArgumentParserTypes.h"
  6. #include "cmMakefile.h"
  7. #include "cmMessageType.h"
  8. #include "cmStringAlgorithms.h"
  9. namespace ArgumentParser {
  10. auto KeywordActionMap::Emplace(cm::string_view name, KeywordAction action)
  11. -> std::pair<iterator, bool>
  12. {
  13. auto const it =
  14. std::lower_bound(this->begin(), this->end(), name,
  15. [](value_type const& elem, cm::string_view const& k) {
  16. return elem.first < k;
  17. });
  18. return (it != this->end() && it->first == name)
  19. ? std::make_pair(it, false)
  20. : std::make_pair(this->emplace(it, name, std::move(action)), true);
  21. }
  22. auto KeywordActionMap::Find(cm::string_view name) const -> const_iterator
  23. {
  24. auto const it =
  25. std::lower_bound(this->begin(), this->end(), name,
  26. [](value_type const& elem, cm::string_view const& k) {
  27. return elem.first < k;
  28. });
  29. return (it != this->end() && it->first == name) ? it : this->end();
  30. }
  31. void Instance::Bind(std::function<Continue(cm::string_view)> f,
  32. ExpectAtLeast expect)
  33. {
  34. this->KeywordValueFunc = std::move(f);
  35. this->KeywordValuesExpected = expect.Count;
  36. }
  37. void Instance::Bind(bool& val)
  38. {
  39. val = true;
  40. this->Bind(nullptr, ExpectAtLeast{ 0 });
  41. }
  42. void Instance::Bind(std::string& val)
  43. {
  44. this->Bind(
  45. [&val](cm::string_view arg) -> Continue {
  46. val = std::string(arg);
  47. return Continue::No;
  48. },
  49. ExpectAtLeast{ 1 });
  50. }
  51. void Instance::Bind(Maybe<std::string>& val)
  52. {
  53. this->Bind(
  54. [&val](cm::string_view arg) -> Continue {
  55. static_cast<std::string&>(val) = std::string(arg);
  56. return Continue::No;
  57. },
  58. ExpectAtLeast{ 0 });
  59. }
  60. void Instance::Bind(MaybeEmpty<std::vector<std::string>>& val)
  61. {
  62. this->Bind(
  63. [&val](cm::string_view arg) -> Continue {
  64. val.emplace_back(arg);
  65. return Continue::Yes;
  66. },
  67. ExpectAtLeast{ 0 });
  68. }
  69. void Instance::Bind(NonEmpty<std::vector<std::string>>& val)
  70. {
  71. this->Bind(
  72. [&val](cm::string_view arg) -> Continue {
  73. val.emplace_back(arg);
  74. return Continue::Yes;
  75. },
  76. ExpectAtLeast{ 1 });
  77. }
  78. void Instance::Bind(std::vector<std::vector<std::string>>& multiVal)
  79. {
  80. multiVal.emplace_back();
  81. std::vector<std::string>& val = multiVal.back();
  82. this->Bind(
  83. [&val](cm::string_view arg) -> Continue {
  84. val.emplace_back(arg);
  85. return Continue::Yes;
  86. },
  87. ExpectAtLeast{ 0 });
  88. }
  89. void Instance::Consume(cm::string_view arg)
  90. {
  91. auto const it = this->Bindings.Keywords.Find(arg);
  92. if (it != this->Bindings.Keywords.end()) {
  93. this->FinishKeyword();
  94. this->Keyword = it->first;
  95. this->KeywordValuesSeen = 0;
  96. if (this->Bindings.ParsedKeyword) {
  97. this->Bindings.ParsedKeyword(*this, it->first);
  98. }
  99. it->second(*this);
  100. return;
  101. }
  102. if (this->KeywordValueFunc) {
  103. switch (this->KeywordValueFunc(arg)) {
  104. case Continue::Yes:
  105. break;
  106. case Continue::No:
  107. this->KeywordValueFunc = nullptr;
  108. break;
  109. }
  110. ++this->KeywordValuesSeen;
  111. return;
  112. }
  113. if (this->UnparsedArguments != nullptr) {
  114. this->UnparsedArguments->emplace_back(arg);
  115. }
  116. }
  117. void Instance::FinishKeyword()
  118. {
  119. if (this->Keyword.empty()) {
  120. return;
  121. }
  122. if (this->KeywordValuesSeen < this->KeywordValuesExpected) {
  123. if (this->ParseResults != nullptr) {
  124. this->ParseResults->AddKeywordError(this->Keyword,
  125. " missing required value\n");
  126. }
  127. if (this->Bindings.KeywordMissingValue) {
  128. this->Bindings.KeywordMissingValue(*this, this->Keyword);
  129. }
  130. }
  131. }
  132. bool ParseResult::MaybeReportError(cmMakefile& mf) const
  133. {
  134. if (*this) {
  135. return false;
  136. }
  137. std::string e;
  138. for (auto const& ke : this->KeywordErrors) {
  139. e = cmStrCat(e, "Error after keyword \"", ke.first, "\":\n", ke.second);
  140. }
  141. mf.IssueMessage(MessageType::FATAL_ERROR, e);
  142. return true;
  143. }
  144. } // namespace ArgumentParser