Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_TOOLS_NFDC_COMMAND_PARSER_HPP |
| 27 | #define NFD_TOOLS_NFDC_COMMAND_PARSER_HPP |
| 28 | |
| 29 | #include "command-definition.hpp" |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 30 | #include "execute-command.hpp" |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 31 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 32 | #include <type_traits> |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::tools::nfdc { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 35 | |
| 36 | /** \brief indicates which modes is a command allowed |
| 37 | */ |
| 38 | enum AvailableIn : uint8_t { |
| 39 | AVAILABLE_IN_NONE = 0, |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 40 | AVAILABLE_IN_ONE_SHOT = 1 << 0, ///< one-shot mode |
| 41 | AVAILABLE_IN_BATCH = 1 << 1, ///< batch mode |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 42 | AVAILABLE_IN_HELP = 1 << 7, ///< visible in help listing |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 43 | AVAILABLE_IN_ALL = 0xff |
| 44 | }; |
| 45 | |
| 46 | std::ostream& |
| 47 | operator<<(std::ostream& os, AvailableIn modes); |
| 48 | |
| 49 | /** \brief indicates which mode is the parser operated in |
| 50 | */ |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 51 | enum class ParseMode : uint8_t { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 52 | ONE_SHOT = AVAILABLE_IN_ONE_SHOT, ///< one-shot mode |
| 53 | BATCH = AVAILABLE_IN_BATCH ///< batch mode |
| 54 | }; |
| 55 | |
| 56 | std::ostream& |
| 57 | operator<<(std::ostream& os, ParseMode mode); |
| 58 | |
| 59 | /** \brief parses a command |
| 60 | */ |
| 61 | class CommandParser : noncopyable |
| 62 | { |
| 63 | public: |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 64 | class NoSuchCommandError : public std::invalid_argument |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 65 | { |
| 66 | public: |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 67 | NoSuchCommandError(const std::string& noun, const std::string& verb) |
| 68 | : std::invalid_argument("No such command: " + noun + " " + verb) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 69 | { |
| 70 | } |
| 71 | }; |
| 72 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 73 | /** \brief add an available command |
| 74 | * \param def command semantics definition |
| 75 | * \param execute a function to execute the command |
| 76 | * \param modes parse modes this command should be available in, must not be AVAILABLE_IN_NONE |
| 77 | */ |
| 78 | CommandParser& |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 79 | addCommand(const CommandDefinition& def, const ExecuteCommand& execute, |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 80 | std::underlying_type_t<AvailableIn> modes = AVAILABLE_IN_ALL); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 81 | |
| 82 | /** \brief add an alias "noun verb2" to existing command "noun verb" |
| 83 | * \throw std::out_of_range "noun verb" does not exist |
| 84 | */ |
| 85 | CommandParser& |
| 86 | addAlias(const std::string& noun, const std::string& verb, const std::string& verb2); |
| 87 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 88 | /** \brief list known commands for help |
| 89 | * \param noun if not empty, filter results by this noun |
| 90 | * \param mode include commands for the specified parse mode |
| 91 | * \return commands in insertion order |
| 92 | */ |
| 93 | std::vector<const CommandDefinition*> |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 94 | listCommands(std::string_view noun, ParseMode mode) const; |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 95 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 96 | /** \brief parse a command line |
| 97 | * \param tokens command line |
| 98 | * \param mode parser mode, must be ParseMode::ONE_SHOT, other modes are not implemented |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 99 | * \throw NoSuchCommandError command not found |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 100 | * \throw CommandDefinition::Error command arguments are invalid |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 101 | * \return noun, verb, arguments, execute function |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 102 | */ |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 103 | std::tuple<std::string, std::string, CommandArguments, ExecuteCommand> |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 104 | parse(const std::vector<std::string>& tokens, ParseMode mode) const; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 105 | |
| 106 | private: |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 107 | using CommandName = std::pair<std::string, std::string>; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 108 | |
| 109 | struct Command |
| 110 | { |
| 111 | CommandDefinition def; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 112 | ExecuteCommand execute; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 113 | AvailableIn modes; |
| 114 | }; |
| 115 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 116 | /** \brief map from command name or alias to command definition |
| 117 | */ |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 118 | using CommandContainer = std::map<CommandName, shared_ptr<Command>>; |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 119 | CommandContainer m_commands; |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 120 | |
| 121 | /** \brief commands in insertion order |
| 122 | */ |
| 123 | std::vector<CommandContainer::const_iterator> m_commandOrder; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame^] | 126 | void |
| 127 | registerCommands(CommandParser& parser); |
| 128 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 129 | } // namespace nfd::tools::nfdc |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 130 | |
| 131 | #endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP |