blob: a872fcd776b73898f0cb57064eeeca5cb36abed5 [file] [log] [blame]
Junxiao Shi64567bb2016-09-04 16:00:27 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -05002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shi64567bb2016-09-04 16:00:27 +00004 * 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 Shi737c43c2016-09-14 02:51:44 +000030#include "execute-command.hpp"
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050031
Junxiao Shic143afe2016-09-20 13:04:51 +000032#include <type_traits>
Junxiao Shi64567bb2016-09-04 16:00:27 +000033
34namespace nfd {
35namespace tools {
36namespace nfdc {
37
38/** \brief indicates which modes is a command allowed
39 */
40enum AvailableIn : uint8_t {
41 AVAILABLE_IN_NONE = 0,
Junxiao Shic143afe2016-09-20 13:04:51 +000042 AVAILABLE_IN_ONE_SHOT = 1 << 0, ///< one-shot mode
43 AVAILABLE_IN_BATCH = 1 << 1, ///< batch mode
Junxiao Shi6c135622016-11-21 14:30:33 +000044 AVAILABLE_IN_HELP = 1 << 7, ///< visible in help listing
Junxiao Shi64567bb2016-09-04 16:00:27 +000045 AVAILABLE_IN_ALL = 0xff
46};
47
48std::ostream&
49operator<<(std::ostream& os, AvailableIn modes);
50
51/** \brief indicates which mode is the parser operated in
52 */
Junxiao Shic143afe2016-09-20 13:04:51 +000053enum class ParseMode : uint8_t {
Junxiao Shi64567bb2016-09-04 16:00:27 +000054 ONE_SHOT = AVAILABLE_IN_ONE_SHOT, ///< one-shot mode
55 BATCH = AVAILABLE_IN_BATCH ///< batch mode
56};
57
58std::ostream&
59operator<<(std::ostream& os, ParseMode mode);
60
61/** \brief parses a command
62 */
63class CommandParser : noncopyable
64{
65public:
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050066 class NoSuchCommandError : public std::invalid_argument
Junxiao Shi64567bb2016-09-04 16:00:27 +000067 {
68 public:
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050069 NoSuchCommandError(const std::string& noun, const std::string& verb)
70 : std::invalid_argument("No such command: " + noun + " " + verb)
Junxiao Shi64567bb2016-09-04 16:00:27 +000071 {
72 }
73 };
74
Junxiao Shi64567bb2016-09-04 16:00:27 +000075 /** \brief add an available command
76 * \param def command semantics definition
77 * \param execute a function to execute the command
78 * \param modes parse modes this command should be available in, must not be AVAILABLE_IN_NONE
79 */
80 CommandParser&
Junxiao Shic143afe2016-09-20 13:04:51 +000081 addCommand(const CommandDefinition& def, const ExecuteCommand& execute,
82 std::underlying_type<AvailableIn>::type modes = AVAILABLE_IN_ALL);
Junxiao Shi64567bb2016-09-04 16:00:27 +000083
84 /** \brief add an alias "noun verb2" to existing command "noun verb"
85 * \throw std::out_of_range "noun verb" does not exist
86 */
87 CommandParser&
88 addAlias(const std::string& noun, const std::string& verb, const std::string& verb2);
89
Junxiao Shi6c135622016-11-21 14:30:33 +000090 /** \brief list known commands for help
91 * \param noun if not empty, filter results by this noun
92 * \param mode include commands for the specified parse mode
93 * \return commands in insertion order
94 */
95 std::vector<const CommandDefinition*>
96 listCommands(const std::string& noun, ParseMode mode) const;
97
Junxiao Shi64567bb2016-09-04 16:00:27 +000098 /** \brief parse a command line
99 * \param tokens command line
100 * \param mode parser mode, must be ParseMode::ONE_SHOT, other modes are not implemented
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500101 * \throw NoSuchCommandError command not found
Junxiao Shi64567bb2016-09-04 16:00:27 +0000102 * \throw CommandDefinition::Error command arguments are invalid
Junxiao Shi737c43c2016-09-14 02:51:44 +0000103 * \return noun, verb, arguments, execute function
Junxiao Shi64567bb2016-09-04 16:00:27 +0000104 */
Junxiao Shi737c43c2016-09-14 02:51:44 +0000105 std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
Davide Pesavento2a588152018-02-19 18:10:03 -0500106 parse(const std::vector<std::string>& tokens, ParseMode mode) const;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000107
108private:
109 typedef std::pair<std::string, std::string> CommandName;
110
111 struct Command
112 {
113 CommandDefinition def;
Junxiao Shi737c43c2016-09-14 02:51:44 +0000114 ExecuteCommand execute;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000115 AvailableIn modes;
116 };
117
Junxiao Shic143afe2016-09-20 13:04:51 +0000118 /** \brief map from command name or alias to command definition
119 */
120 typedef std::map<CommandName, shared_ptr<Command>> CommandContainer;
121 CommandContainer m_commands;
Junxiao Shi6c135622016-11-21 14:30:33 +0000122
123 /** \brief commands in insertion order
124 */
125 std::vector<CommandContainer::const_iterator> m_commandOrder;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000126};
127
128} // namespace nfdc
129} // namespace tools
130} // namespace nfd
131
132#endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP