💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.0 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_CLI_HPP
  20. #define ANTKEEPER_CLI_HPP
  21. #include <functional>
  22. #include <map>
  23. #include <sstream>
  24. #include <string>
  25. #include <tuple>
  26. /**
  27. * Minimal command-line interpreter.
  28. */
  29. class cli
  30. {
  31. public:
  32. /// String-wrapped function object
  33. typedef std::function<std::string(const std::string&)> command_type;
  34. /**
  35. * Interprets a command line as a function invocation.
  36. *
  37. * @param line Command line.
  38. * @return Stringified return value of the command function.
  39. */
  40. std::string interpret(const std::string& line) const;
  41. /**
  42. * Registers a command with the CLI.
  43. *
  44. * @tparam T Command function return type.
  45. * @tparam Args Command function argument types.
  46. * @param name Command name.
  47. * @param function Command function.
  48. */
  49. template <class T, class... Args>
  50. void register_command(const std::string& name, const std::function<T(Args...)>& function);
  51. /// @copydoc register_command(const std::string, const std::function<T(Args...)>&)
  52. template <class T, class... Args>
  53. void register_command(const std::string& name, T (*function)(Args...));
  54. /**
  55. * Unregisters a command from the CLI.
  56. *
  57. * @param name Command name.
  58. */
  59. void unregister_command(const std::string& name);
  60. private:
  61. /**
  62. * Parses a single argument from a string stream.
  63. *
  64. * @param stream String stream from which an argument should be parsed.
  65. */
  66. template <class T>
  67. static T parse(std::istringstream& stream);
  68. /**
  69. * Wraps a function in an interpreter function that parses strings as arguments then executes the wrapped function with the parsed arguments.
  70. *
  71. * @param function Function to wrap.
  72. * @return Wrapped function.
  73. */
  74. template <class T, class... Args>
  75. static command_type wrap(const std::function<T(Args...)>& function);
  76. std::map<std::string, command_type> commands;
  77. };
  78. std::string cli::interpret(const std::string& line) const
  79. {
  80. std::istringstream stream(line);
  81. std::string command_name;
  82. stream >> command_name;
  83. if (auto it = commands.find(command_name); it != commands.end())
  84. {
  85. return it->second(line.substr(command_name.length() + 1));
  86. }
  87. return std::string();
  88. }
  89. template <class T, class... Args>
  90. void cli::register_command(const std::string& name, const std::function<T(Args...)>& function)
  91. {
  92. commands[name] = wrap(function);
  93. }
  94. template <class T, class... Args>
  95. void cli::register_command(const std::string& name, T (*function)(Args...))
  96. {
  97. commands[name] = wrap(std::function(function));
  98. }
  99. void cli::unregister_command(const std::string& name)
  100. {
  101. if (auto it = commands.find(name); it != commands.end())
  102. commands.erase(it);
  103. }
  104. template <class T>
  105. T cli::parse(std::istringstream& stream)
  106. {
  107. T value;
  108. stream >> value;
  109. return value;
  110. }
  111. template <class T, class... Args>
  112. typename cli::command_type cli::wrap(const std::function<T(Args...)>& function)
  113. {
  114. return std::bind(
  115. [function](const std::string& line) -> std::string
  116. {
  117. //std::size_t argument_count = sizeof...(Args);
  118. // Parse string into tuple of arguments
  119. std::istringstream istream(line);
  120. std::tuple<Args...> arguments{parse<Args>(istream)...};
  121. // Invoke function with arguments and save the result
  122. T result = std::apply(function, arguments);
  123. // Return function result as string
  124. std::ostringstream ostream;
  125. ostream << result;
  126. return ostream.str();
  127. },
  128. std::placeholders::_1);
  129. }
  130. #endif // ANTKEEPER_CLI_HPP