cmArgumentParser.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. namespace ArgumentParser {
  6. auto ActionMap::Emplace(cm::string_view name, Action action)
  7. -> std::pair<iterator, bool>
  8. {
  9. auto const it =
  10. std::lower_bound(this->begin(), this->end(), name,
  11. [](value_type const& elem, cm::string_view const& k) {
  12. return elem.first < k;
  13. });
  14. return (it != this->end() && it->first == name)
  15. ? std::make_pair(it, false)
  16. : std::make_pair(this->emplace(it, name, std::move(action)), true);
  17. }
  18. auto ActionMap::Find(cm::string_view name) const -> const_iterator
  19. {
  20. auto const it =
  21. std::lower_bound(this->begin(), this->end(), name,
  22. [](value_type const& elem, cm::string_view const& k) {
  23. return elem.first < k;
  24. });
  25. return (it != this->end() && it->first == name) ? it : this->end();
  26. }
  27. void Instance::Bind(bool& val)
  28. {
  29. val = true;
  30. this->CurrentString = nullptr;
  31. this->CurrentList = nullptr;
  32. this->ExpectValue = false;
  33. }
  34. void Instance::Bind(std::string& val)
  35. {
  36. this->CurrentString = &val;
  37. this->CurrentList = nullptr;
  38. this->ExpectValue = true;
  39. }
  40. void Instance::Bind(StringList& val)
  41. {
  42. this->CurrentString = nullptr;
  43. this->CurrentList = &val;
  44. this->ExpectValue = true;
  45. }
  46. void Instance::Bind(MultiStringList& val)
  47. {
  48. this->CurrentString = nullptr;
  49. this->CurrentList = (static_cast<void>(val.emplace_back()), &val.back());
  50. this->ExpectValue = false;
  51. }
  52. void Instance::Consume(cm::string_view arg, void* result,
  53. std::vector<std::string>* unparsedArguments,
  54. std::vector<cm::string_view>* keywordsMissingValue,
  55. std::vector<cm::string_view>* parsedKeywords)
  56. {
  57. auto const it = this->Bindings.Find(arg);
  58. if (it != this->Bindings.end()) {
  59. if (parsedKeywords != nullptr) {
  60. parsedKeywords->emplace_back(it->first);
  61. }
  62. it->second(*this, result);
  63. if (this->ExpectValue && keywordsMissingValue != nullptr) {
  64. keywordsMissingValue->emplace_back(it->first);
  65. }
  66. return;
  67. }
  68. if (this->CurrentString != nullptr) {
  69. this->CurrentString->assign(std::string(arg));
  70. this->CurrentString = nullptr;
  71. this->CurrentList = nullptr;
  72. } else if (this->CurrentList != nullptr) {
  73. this->CurrentList->emplace_back(arg);
  74. } else if (unparsedArguments != nullptr) {
  75. unparsedArguments->emplace_back(arg);
  76. }
  77. if (this->ExpectValue) {
  78. if (keywordsMissingValue != nullptr) {
  79. keywordsMissingValue->pop_back();
  80. }
  81. this->ExpectValue = false;
  82. }
  83. }
  84. } // namespace ArgumentParser