Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 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" |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame^] | 31 | #include <type_traits> |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
| 34 | namespace tools { |
| 35 | namespace nfdc { |
| 36 | |
| 37 | /** \brief indicates which modes is a command allowed |
| 38 | */ |
| 39 | enum AvailableIn : uint8_t { |
| 40 | AVAILABLE_IN_NONE = 0, |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame^] | 41 | AVAILABLE_IN_ONE_SHOT = 1 << 0, ///< one-shot mode |
| 42 | AVAILABLE_IN_BATCH = 1 << 1, ///< batch mode |
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: |
| 64 | class Error : public std::invalid_argument |
| 65 | { |
| 66 | public: |
| 67 | explicit |
| 68 | Error(const std::string& what) |
| 69 | : std::invalid_argument(what) |
| 70 | { |
| 71 | } |
| 72 | }; |
| 73 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 74 | /** \brief add an available command |
| 75 | * \param def command semantics definition |
| 76 | * \param execute a function to execute the command |
| 77 | * \param modes parse modes this command should be available in, must not be AVAILABLE_IN_NONE |
| 78 | */ |
| 79 | CommandParser& |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame^] | 80 | addCommand(const CommandDefinition& def, const ExecuteCommand& execute, |
| 81 | std::underlying_type<AvailableIn>::type modes = AVAILABLE_IN_ALL); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 82 | |
| 83 | /** \brief add an alias "noun verb2" to existing command "noun verb" |
| 84 | * \throw std::out_of_range "noun verb" does not exist |
| 85 | */ |
| 86 | CommandParser& |
| 87 | addAlias(const std::string& noun, const std::string& verb, const std::string& verb2); |
| 88 | |
| 89 | /** \brief parse a command line |
| 90 | * \param tokens command line |
| 91 | * \param mode parser mode, must be ParseMode::ONE_SHOT, other modes are not implemented |
| 92 | * \throw Error command is not found |
| 93 | * \throw CommandDefinition::Error command arguments are invalid |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 94 | * \return noun, verb, arguments, execute function |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 95 | */ |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 96 | std::tuple<std::string, std::string, CommandArguments, ExecuteCommand> |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 97 | parse(const std::vector<std::string>& tokens, ParseMode mode) const; |
| 98 | |
| 99 | private: |
| 100 | typedef std::pair<std::string, std::string> CommandName; |
| 101 | |
| 102 | struct Command |
| 103 | { |
| 104 | CommandDefinition def; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 105 | ExecuteCommand execute; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 106 | AvailableIn modes; |
| 107 | }; |
| 108 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame^] | 109 | /** \brief map from command name or alias to command definition |
| 110 | */ |
| 111 | typedef std::map<CommandName, shared_ptr<Command>> CommandContainer; |
| 112 | CommandContainer m_commands; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | } // namespace nfdc |
| 116 | } // namespace tools |
| 117 | } // namespace nfd |
| 118 | |
| 119 | #endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP |