cmArgumentParser.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. {
  33. this->KeywordValueFunc = std::move(f);
  34. this->ExpectValue = true;
  35. }
  36. void Instance::Bind(bool& val)
  37. {
  38. val = true;
  39. this->KeywordValueFunc = nullptr;
  40. this->ExpectValue = false;
  41. }
  42. void Instance::Bind(std::string& val)
  43. {
  44. this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue {
  45. val = std::string(arg);
  46. return Continue::No;
  47. };
  48. this->ExpectValue = true;
  49. }
  50. void Instance::Bind(Maybe<std::string>& val)
  51. {
  52. this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue {
  53. static_cast<std::string&>(val) = std::string(arg);
  54. return Continue::No;
  55. };
  56. this->ExpectValue = false;
  57. }
  58. void Instance::Bind(MaybeEmpty<std::vector<std::string>>& val)
  59. {
  60. this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue {
  61. val.emplace_back(arg);
  62. return Continue::Yes;
  63. };
  64. this->ExpectValue = false;
  65. }
  66. void Instance::Bind(NonEmpty<std::vector<std::string>>& val)
  67. {
  68. this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue {
  69. val.emplace_back(arg);
  70. return Continue::Yes;
  71. };
  72. this->ExpectValue = true;
  73. }
  74. void Instance::Bind(std::vector<std::vector<std::string>>& multiVal)
  75. {
  76. multiVal.emplace_back();
  77. std::vector<std::string>& val = multiVal.back();
  78. this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue {
  79. val.emplace_back(arg);
  80. return Continue::Yes;
  81. };
  82. this->ExpectValue = false;
  83. }
  84. void Instance::Consume(cm::string_view arg)
  85. {
  86. auto const it = this->Bindings.Keywords.Find(arg);
  87. if (it != this->Bindings.Keywords.end()) {
  88. this->FinishKeyword();
  89. this->Keyword = it->first;
  90. if (this->Bindings.ParsedKeyword) {
  91. this->Bindings.ParsedKeyword(*this, it->first);
  92. }
  93. it->second(*this);
  94. return;
  95. }
  96. if (this->KeywordValueFunc) {
  97. switch (this->KeywordValueFunc(arg)) {
  98. case Continue::Yes:
  99. break;
  100. case Continue::No:
  101. this->KeywordValueFunc = nullptr;
  102. break;
  103. }
  104. this->ExpectValue = false;
  105. return;
  106. }
  107. if (this->UnparsedArguments != nullptr) {
  108. this->UnparsedArguments->emplace_back(arg);
  109. }
  110. }
  111. void Instance::FinishKeyword()
  112. {
  113. if (this->Keyword.empty()) {
  114. return;
  115. }
  116. if (this->ExpectValue) {
  117. if (this->ParseResults != nullptr) {
  118. this->ParseResults->AddKeywordError(this->Keyword,
  119. " missing required value\n");
  120. }
  121. if (this->Bindings.KeywordMissingValue) {
  122. this->Bindings.KeywordMissingValue(*this, this->Keyword);
  123. }
  124. }
  125. }
  126. bool ParseResult::MaybeReportError(cmMakefile& mf) const
  127. {
  128. if (*this) {
  129. return false;
  130. }
  131. std::string e;
  132. for (auto const& ke : this->KeywordErrors) {
  133. e = cmStrCat(e, "Error after keyword \"", ke.first, "\":\n", ke.second);
  134. }
  135. mf.IssueMessage(MessageType::FATAL_ERROR, e);
  136. return true;
  137. }
  138. } // namespace ArgumentParser