blob: 7a1dbda6d61b136b2ee3e8f9027e1e67e1bf8f70 [file] [log] [blame]
Junxiao Shi64567bb2016-09-04 16:00:27 +00001/* -*- 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"
30
31namespace nfd {
32namespace tools {
33namespace nfdc {
34
35/** \brief indicates which modes is a command allowed
36 */
37enum AvailableIn : uint8_t {
38 AVAILABLE_IN_NONE = 0,
39 AVAILABLE_IN_ONE_SHOT = 1 << 0,
40 AVAILABLE_IN_BATCH = 1 << 1,
41 AVAILABLE_IN_ALL = 0xff
42};
43
44std::ostream&
45operator<<(std::ostream& os, AvailableIn modes);
46
47/** \brief indicates which mode is the parser operated in
48 */
49enum class ParseMode {
50 ONE_SHOT = AVAILABLE_IN_ONE_SHOT, ///< one-shot mode
51 BATCH = AVAILABLE_IN_BATCH ///< batch mode
52};
53
54std::ostream&
55operator<<(std::ostream& os, ParseMode mode);
56
57/** \brief parses a command
58 */
59class CommandParser : noncopyable
60{
61public:
62 class Error : public std::invalid_argument
63 {
64 public:
65 explicit
66 Error(const std::string& what)
67 : std::invalid_argument(what)
68 {
69 }
70 };
71
72 typedef std::function<void(const CommandArguments&)> Execute;
73
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&
80 addCommand(const CommandDefinition& def, const Execute& execute, AvailableIn modes = AVAILABLE_IN_ALL);
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
88 /** \brief parse a command line
89 * \param tokens command line
90 * \param mode parser mode, must be ParseMode::ONE_SHOT, other modes are not implemented
91 * \throw Error command is not found
92 * \throw CommandDefinition::Error command arguments are invalid
93 */
94 std::tuple<Execute*, CommandArguments>
95 parse(const std::vector<std::string>& tokens, ParseMode mode) const;
96
97private:
98 typedef std::pair<std::string, std::string> CommandName;
99
100 struct Command
101 {
102 CommandDefinition def;
103 Execute execute;
104 AvailableIn modes;
105 };
106
107 std::map<CommandName, shared_ptr<Command>> m_commands;
108};
109
110} // namespace nfdc
111} // namespace tools
112} // namespace nfd
113
114#endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP