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-2024, 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 "core/common.hpp"
30 #include "command-definition.hpp"
31 #include "execute-command.hpp"
32 
33 #include <type_traits>
34 
35 namespace nfd::tools::nfdc {
36 
39 enum AvailableIn : uint8_t {
42  AVAILABLE_IN_BATCH = 1 << 1,
43  AVAILABLE_IN_HELP = 1 << 7,
44  AVAILABLE_IN_ALL = 0xff
45 };
46 
47 std::ostream&
48 operator<<(std::ostream& os, AvailableIn modes);
49 
52 enum class ParseMode : uint8_t {
55 };
56 
57 std::ostream&
58 operator<<(std::ostream& os, ParseMode mode);
59 
62 class CommandParser : boost::noncopyable
63 {
64 public:
65  class NoSuchCommandError : public std::invalid_argument
66  {
67  public:
68  NoSuchCommandError(const std::string& noun, const std::string& verb)
69  : std::invalid_argument("No such command: " + noun + " " + verb)
70  {
71  }
72  };
73 
80  addCommand(const CommandDefinition& def, const ExecuteCommand& execute,
81  std::underlying_type_t<AvailableIn> modes = AVAILABLE_IN_ALL);
82 
87  addAlias(const std::string& noun, const std::string& verb, const std::string& verb2);
88 
94  std::vector<const CommandDefinition*>
95  listCommands(std::string_view noun, ParseMode mode) const;
96 
104  std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
105  parse(const std::vector<std::string>& tokens, ParseMode mode) const;
106 
107 private:
108  using CommandName = std::pair<std::string, std::string>;
109 
110  struct Command
111  {
112  CommandDefinition def;
113  ExecuteCommand execute;
114  AvailableIn modes;
115  };
116 
119  using CommandContainer = std::map<CommandName, shared_ptr<Command>>;
120  CommandContainer m_commands;
121 
124  std::vector<CommandContainer::const_iterator> m_commandOrder;
125 };
126 
127 void
128 registerCommands(CommandParser& parser);
129 
130 } // namespace nfd::tools::nfdc
131 
132 #endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP
NoSuchCommandError(const std::string &noun, const std::string &verb)
CommandParser & addCommand(const CommandDefinition &def, const ExecuteCommand &execute, std::underlying_type_t< AvailableIn > modes=AVAILABLE_IN_ALL)
Add an available command.
std::vector< const CommandDefinition * > listCommands(std::string_view noun, ParseMode mode) const
List known commands for help.
std::tuple< std::string, std::string, CommandArguments, ExecuteCommand > parse(const std::vector< std::string > &tokens, ParseMode mode) const
Parse a command line.
CommandParser & addAlias(const std::string &noun, const std::string &verb, const std::string &verb2)
Add an alias "noun verb2" to existing command "noun verb".
std::ostream & operator<<(std::ostream &os, ArgValueType vt)
AvailableIn
Indicates which modes is a command allowed.
@ AVAILABLE_IN_HELP
visible in help listing
@ AVAILABLE_IN_BATCH
batch mode
@ AVAILABLE_IN_ONE_SHOT
one-shot mode
void registerCommands(CommandParser &parser)
std::function< void(ExecuteContext &)> ExecuteCommand
A function to execute a command.
ParseMode
Indicates which mode is the parser operated in.