cmSubcommandTable.cxx 1.1 KB

12345678910111213141516171819202122232425262728293031
  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 "cmSubcommandTable.h"
  4. #include <algorithm>
  5. #include "cmExecutionStatus.h"
  6. #include "cmStringAlgorithms.h"
  7. cmSubcommandTable::cmSubcommandTable(std::initializer_list<InitElem> init)
  8. : Impl(init.begin(), init.end())
  9. {
  10. std::sort(this->Impl.begin(), this->Impl.end(),
  11. [](Elem const& left, Elem const& right) {
  12. return left.first < right.first;
  13. });
  14. }
  15. bool cmSubcommandTable::operator()(cm::string_view key,
  16. std::vector<std::string> const& args,
  17. cmExecutionStatus& status) const
  18. {
  19. auto const it = std::lower_bound(
  20. this->Impl.begin(), this->Impl.end(), key,
  21. [](Elem const& elem, cm::string_view k) { return elem.first < k; });
  22. if (it != this->Impl.end() && it->first == key) {
  23. return it->second(args, status);
  24. }
  25. status.SetError(cmStrCat("does not recognize sub-command ", key));
  26. return false;
  27. }