💿🐜 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.

132 lines
3.6 KiB

  1. /*
  2. * Copyright (C) 2021 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. /**
  33. * Interprets a command line as a function invocation.
  34. *
  35. * @param line Command line.
  36. * @return Stringified return value of the command function.
  37. */
  38. std::string interpret(const std::string& line) const;
  39. /**
  40. * Registers a command with the CLI.
  41. *
  42. * @tparam T Command function return type.
  43. * @tparam Args Command function argument types.
  44. * @param name Command name.
  45. * @param function Command function.
  46. */
  47. template <class T, class... Args>
  48. void register_command(const std::string& name, const std::function<T(Args...)>& function);
  49. /// @copydoc register_command(const std::string, const std::function<T(Args...)>&)
  50. template <class T, class... Args>
  51. void register_command(const std::string& name, T (*function)(Args...));
  52. /**
  53. * Unregisters a command from the CLI.
  54. *
  55. * @param name Command name.
  56. */
  57. void unregister_command(const std::string& name);
  58. private:
  59. /// String-wrapped function object
  60. typedef std::function<std::string(const std::string&)> command_type;
  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. template <class T, class... Args>
  79. void cli::register_command(const std::string& name, const std::function<T(Args...)>& function)
  80. {
  81. commands[name] = wrap(function);
  82. }
  83. template <class T, class... Args>
  84. void cli::register_command(const std::string& name, T (*function)(Args...))
  85. {
  86. commands[name] = wrap(std::function(function));
  87. }
  88. template <class T>
  89. T cli::parse(std::istringstream& stream)
  90. {
  91. T value;
  92. stream >> value;
  93. return value;
  94. }
  95. template <class T, class... Args>
  96. typename cli::command_type cli::wrap(const std::function<T(Args...)>& function)
  97. {
  98. return std::bind(
  99. [function](const std::string& line) -> std::string
  100. {
  101. //std::size_t argument_count = sizeof...(Args);
  102. // Parse string into tuple of arguments
  103. std::istringstream istream(line);
  104. std::tuple<Args...> arguments{parse<Args>(istream)...};
  105. // Invoke function with arguments and save the result
  106. T result = std::apply(function, arguments);
  107. // Return function result as string
  108. std::ostringstream ostream;
  109. ostream << result;
  110. return ostream.str();
  111. },
  112. std::placeholders::_1);
  113. }
  114. #endif // ANTKEEPER_CLI_HPP