command-parser.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, 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 #include "execute-command.hpp"
31 
32 #include <type_traits>
33 
34 namespace nfd {
35 namespace tools {
36 namespace nfdc {
37 
40 enum AvailableIn : uint8_t {
43  AVAILABLE_IN_BATCH = 1 << 1,
44  AVAILABLE_IN_HELP = 1 << 7,
46 };
47 
48 std::ostream&
49 operator<<(std::ostream& os, AvailableIn modes);
50 
53 enum class ParseMode : uint8_t {
56 };
57 
58 std::ostream&
59 operator<<(std::ostream& os, ParseMode mode);
60 
63 class CommandParser : noncopyable
64 {
65 public:
66  class NoSuchCommandError : public std::invalid_argument
67  {
68  public:
69  NoSuchCommandError(const std::string& noun, const std::string& verb)
70  : std::invalid_argument("No such command: " + noun + " " + verb)
71  {
72  }
73  };
74 
81  addCommand(const CommandDefinition& def, const ExecuteCommand& execute,
82  std::underlying_type<AvailableIn>::type modes = AVAILABLE_IN_ALL);
83 
88  addAlias(const std::string& noun, const std::string& verb, const std::string& verb2);
89 
95  std::vector<const CommandDefinition*>
96  listCommands(const std::string& noun, ParseMode mode) const;
97 
105  std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
106  parse(const std::vector<std::string>& tokens, ParseMode mode) const;
107 
108 private:
109  typedef std::pair<std::string, std::string> CommandName;
110 
111  struct Command
112  {
113  CommandDefinition def;
114  ExecuteCommand execute;
115  AvailableIn modes;
116  };
117 
120  typedef std::map<CommandName, shared_ptr<Command>> CommandContainer;
121  CommandContainer m_commands;
122 
125  std::vector<CommandContainer::const_iterator> m_commandOrder;
126 };
127 
128 } // namespace nfdc
129 } // namespace tools
130 } // namespace nfd
131 
132 #endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP
declares semantics of a command
STL namespace.
std::ostream & operator<<(std::ostream &os, ArgValueType vt)
NoSuchCommandError(const std::string &noun, const std::string &verb)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
ParseMode
indicates which mode is the parser operated in
AvailableIn
indicates which modes is a command allowed
std::function< void(ExecuteContext &ctx)> ExecuteCommand
a function to execute a command