| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2014-2024, Regents of the University of California, |
| * Arizona Board of Regents, |
| * Colorado State University, |
| * University Pierre & Marie Curie, Sorbonne University, |
| * Washington University in St. Louis, |
| * Beijing Institute of Technology, |
| * The University of Memphis. |
| * |
| * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| * See AUTHORS.md for complete list of NFD authors and contributors. |
| * |
| * NFD is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, |
| * either version 3 of the License, or (at your option) any later version. |
| * |
| * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| |
| #ifndef NFD_TOOLS_NFDC_COMMAND_PARSER_HPP |
| #define NFD_TOOLS_NFDC_COMMAND_PARSER_HPP |
| |
| #include "core/common.hpp" |
| #include "command-definition.hpp" |
| #include "execute-command.hpp" |
| |
| #include <type_traits> |
| |
| namespace nfd::tools::nfdc { |
| |
| /** \brief Indicates which modes is a command allowed. |
| */ |
| enum AvailableIn : uint8_t { |
| AVAILABLE_IN_NONE = 0, |
| AVAILABLE_IN_ONE_SHOT = 1 << 0, ///< one-shot mode |
| AVAILABLE_IN_BATCH = 1 << 1, ///< batch mode |
| AVAILABLE_IN_HELP = 1 << 7, ///< visible in help listing |
| AVAILABLE_IN_ALL = 0xff |
| }; |
| |
| std::ostream& |
| operator<<(std::ostream& os, AvailableIn modes); |
| |
| /** \brief Indicates which mode is the parser operated in. |
| */ |
| enum class ParseMode : uint8_t { |
| ONE_SHOT = AVAILABLE_IN_ONE_SHOT, ///< one-shot mode |
| BATCH = AVAILABLE_IN_BATCH ///< batch mode |
| }; |
| |
| std::ostream& |
| operator<<(std::ostream& os, ParseMode mode); |
| |
| /** \brief Parses a command. |
| */ |
| class CommandParser : boost::noncopyable |
| { |
| public: |
| class NoSuchCommandError : public std::invalid_argument |
| { |
| public: |
| NoSuchCommandError(const std::string& noun, const std::string& verb) |
| : std::invalid_argument("No such command: " + noun + " " + verb) |
| { |
| } |
| }; |
| |
| /** \brief Add an available command. |
| * \param def command semantics definition |
| * \param execute a function to execute the command |
| * \param modes parse modes this command should be available in, must not be AVAILABLE_IN_NONE |
| */ |
| CommandParser& |
| addCommand(const CommandDefinition& def, const ExecuteCommand& execute, |
| std::underlying_type_t<AvailableIn> modes = AVAILABLE_IN_ALL); |
| |
| /** \brief Add an alias "noun verb2" to existing command "noun verb". |
| * \throw std::out_of_range "noun verb" does not exist |
| */ |
| CommandParser& |
| addAlias(const std::string& noun, const std::string& verb, const std::string& verb2); |
| |
| /** \brief List known commands for help. |
| * \param noun if not empty, filter results by this noun |
| * \param mode include commands for the specified parse mode |
| * \return commands in insertion order |
| */ |
| std::vector<const CommandDefinition*> |
| listCommands(std::string_view noun, ParseMode mode) const; |
| |
| /** \brief Parse a command line. |
| * \param tokens command line |
| * \param mode parser mode, must be ParseMode::ONE_SHOT, other modes are not implemented |
| * \throw NoSuchCommandError command not found |
| * \throw CommandDefinition::Error command arguments are invalid |
| * \return noun, verb, arguments, execute function |
| */ |
| std::tuple<std::string, std::string, CommandArguments, ExecuteCommand> |
| parse(const std::vector<std::string>& tokens, ParseMode mode) const; |
| |
| private: |
| using CommandName = std::pair<std::string, std::string>; |
| |
| struct Command |
| { |
| CommandDefinition def; |
| ExecuteCommand execute; |
| AvailableIn modes; |
| }; |
| |
| /** \brief Map from command name or alias to command definition. |
| */ |
| using CommandContainer = std::map<CommandName, shared_ptr<Command>>; |
| CommandContainer m_commands; |
| |
| /** \brief Commands in insertion order. |
| */ |
| std::vector<CommandContainer::const_iterator> m_commandOrder; |
| }; |
| |
| void |
| registerCommands(CommandParser& parser); |
| |
| } // namespace nfd::tools::nfdc |
| |
| #endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP |