command-parser.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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 #include "command-parser.hpp"
27 
28 #include "cs-module.hpp"
29 #include "face-module.hpp"
30 #include "rib-module.hpp"
31 #include "status.hpp"
33 
34 #include <ndn-cxx/util/logger.hpp>
35 
36 namespace nfd::tools::nfdc {
37 
38 NDN_LOG_INIT(nfdc.CommandParser);
39 
40 static_assert(std::is_same_v<std::underlying_type_t<AvailableIn>,
41  std::underlying_type_t<ParseMode>>,
42  "AvailableIn and ParseMode must be declared with the same underlying type");
43 
44 std::ostream&
45 operator<<(std::ostream& os, AvailableIn modes)
46 {
47  text::Separator sep("|");
48  if ((modes & AVAILABLE_IN_ONE_SHOT) != 0) {
49  os << sep << "one-shot";
50  }
51  if ((modes & AVAILABLE_IN_BATCH) != 0) {
52  os << sep << "batch";
53  }
54  if ((modes & AVAILABLE_IN_HELP) == 0) {
55  os << sep << "hidden";
56  }
57 
58  if (sep.getCount() == 0) {
59  os << "none";
60  }
61  return os;
62 }
63 
64 std::ostream&
65 operator<<(std::ostream& os, ParseMode mode)
66 {
67  switch (mode) {
69  return os << "one-shot";
70  case ParseMode::BATCH:
71  return os << "batch";
72  }
73  return os << static_cast<int>(mode);
74 }
75 
76 CommandParser&
78  std::underlying_type_t<AvailableIn> modes)
79 {
80  BOOST_ASSERT(modes != AVAILABLE_IN_NONE);
81 
82  m_commands[{def.getNoun(), def.getVerb()}].reset(
83  new Command{def, execute, static_cast<AvailableIn>(modes)});
84 
85  if ((modes & AVAILABLE_IN_HELP) != 0) {
86  m_commandOrder.push_back(m_commands.find({def.getNoun(), def.getVerb()}));
87  }
88 
89  return *this;
90 }
91 
93 CommandParser::addAlias(const std::string& noun, const std::string& verb, const std::string& verb2)
94 {
95  m_commands[{noun, verb2}] = m_commands.at({noun, verb});
96  return *this;
97 }
98 
99 std::vector<const CommandDefinition*>
100 CommandParser::listCommands(std::string_view noun, ParseMode mode) const
101 {
102  std::vector<const CommandDefinition*> results;
103  for (auto i : m_commandOrder) {
104  const auto& command = *i->second;
105  if ((command.modes & static_cast<AvailableIn>(mode)) != 0 &&
106  (noun.empty() || noun == command.def.getNoun())) {
107  results.push_back(&command.def);
108  }
109  }
110  return results;
111 }
112 
113 std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
114 CommandParser::parse(const std::vector<std::string>& tokens, ParseMode mode) const
115 {
116  BOOST_ASSERT(mode == ParseMode::ONE_SHOT);
117 
118  const std::string& noun = tokens.size() > 0 ? tokens[0] : "";
119  const std::string& verb = tokens.size() > 1 ? tokens[1] : "";
120 
121  NDN_LOG_TRACE("parse mode=" << mode << " noun=" << noun << " verb=" << verb);
122 
123  auto i = m_commands.find({noun, verb});
124  if (i == m_commands.end() || (i->second->modes & static_cast<AvailableIn>(mode)) == 0) {
125  NDN_THROW(NoSuchCommandError(noun, verb));
126  }
127 
128  const CommandDefinition& def = i->second->def;
129  NDN_LOG_TRACE("found command noun=" << def.getNoun() << " verb=" << def.getVerb());
130 
131  size_t nConsumed = std::min<size_t>(2, tokens.size());
132  return {def.getNoun(), def.getVerb(), def.parse(tokens, nConsumed), i->second->execute};
133 }
134 
135 void
137 {
138  registerStatusCommands(parser);
143 }
144 
145 } // namespace nfd::tools::nfdc
const std::string & getVerb() const
const std::string & getNoun() const
CommandArguments parse(const std::vector< std::string > &tokens, size_t start=0) const
Parse a command line.
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".
static void registerCommands(CommandParser &parser)
Register 'cs config' command.
Definition: cs-module.cpp:34
static void registerCommands(CommandParser &parser)
Register 'face list', 'face show', 'face create', 'face destroy' commands.
Definition: face-module.cpp:34
static void registerCommands(CommandParser &parser)
Register 'route list', 'route show', 'route add', 'route remove' commands.
Definition: rib-module.cpp:34
static void registerCommands(CommandParser &parser)
Register 'strategy list', 'strategy show', 'strategy set', 'strategy unset' commands.
Print different string on first and subsequent usage.
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.
NDN_LOG_INIT(nfdc.CommandDefinition)
void registerStatusCommands(CommandParser &parser)
Registers status commands.
Definition: status.cpp:118
ParseMode
Indicates which mode is the parser operated in.