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-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 #include "command-parser.hpp"
27 #include "format-helpers.hpp"
28 
29 #include <ndn-cxx/util/logger.hpp>
30 
31 namespace nfd {
32 namespace tools {
33 namespace nfdc {
34 
35 NDN_LOG_INIT(nfdc.CommandParser);
36 
37 static_assert(std::is_same<std::underlying_type<AvailableIn>::type,
38  std::underlying_type<ParseMode>::type>::value,
39  "AvailableIn and ParseMode must be declared with same underlying type");
40 
41 std::ostream&
42 operator<<(std::ostream& os, AvailableIn modes)
43 {
44  text::Separator sep("|");
45  if ((modes & AVAILABLE_IN_ONE_SHOT) != 0) {
46  os << sep << "one-shot";
47  }
48  if ((modes & AVAILABLE_IN_BATCH) != 0) {
49  os << sep << "batch";
50  }
51  if ((modes & AVAILABLE_IN_HELP) == 0) {
52  os << sep << "hidden";
53  }
54 
55  if (sep.getCount() == 0) {
56  os << "none";
57  }
58  return os;
59 }
60 
61 std::ostream&
62 operator<<(std::ostream& os, ParseMode mode)
63 {
64  switch (mode) {
66  return os << "one-shot";
67  case ParseMode::BATCH:
68  return os << "batch";
69  }
70  return os << static_cast<int>(mode);
71 }
72 
75  std::underlying_type<AvailableIn>::type modes)
76 {
77  BOOST_ASSERT(modes != AVAILABLE_IN_NONE);
78 
79  m_commands[{def.getNoun(), def.getVerb()}].reset(
80  new Command{def, execute, static_cast<AvailableIn>(modes)});
81 
82  if ((modes & AVAILABLE_IN_HELP) != 0) {
83  m_commandOrder.push_back(m_commands.find({def.getNoun(), def.getVerb()}));
84  }
85 
86  return *this;
87 }
88 
90 CommandParser::addAlias(const std::string& noun, const std::string& verb, const std::string& verb2)
91 {
92  m_commands[{noun, verb2}] = m_commands.at({noun, verb});
93  return *this;
94 }
95 
96 std::vector<const CommandDefinition*>
97 CommandParser::listCommands(const std::string& noun, ParseMode mode) const
98 {
99  std::vector<const CommandDefinition*> results;
100  for (auto i : m_commandOrder) {
101  const Command& command = *i->second;
102  if ((command.modes & static_cast<AvailableIn>(mode)) != 0 &&
103  (noun.empty() || noun == command.def.getNoun())) {
104  results.push_back(&command.def);
105  }
106  }
107  return results;
108 }
109 
110 std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
111 CommandParser::parse(const std::vector<std::string>& tokens, ParseMode mode) const
112 {
113  BOOST_ASSERT(mode == ParseMode::ONE_SHOT);
114 
115  const std::string& noun = tokens.size() > 0 ? tokens[0] : "";
116  const std::string& verb = tokens.size() > 1 ? tokens[1] : "";
117 
118  NDN_LOG_TRACE("parse mode=" << mode << " noun=" << noun << " verb=" << verb);
119 
120  auto i = m_commands.find({noun, verb});
121  if (i == m_commands.end() || (i->second->modes & static_cast<AvailableIn>(mode)) == 0) {
122  BOOST_THROW_EXCEPTION(NoSuchCommandError(noun, verb));
123  }
124 
125  const CommandDefinition& def = i->second->def;
126  NDN_LOG_TRACE("found command noun=" << def.getNoun() << " verb=" << def.getVerb());
127 
128  size_t nConsumed = std::min<size_t>(2, tokens.size());
129  return std::make_tuple(def.getNoun(), def.getVerb(), def.parse(tokens, nConsumed), i->second->execute);
130 }
131 
132 } // namespace nfdc
133 } // namespace tools
134 } // namespace nfd
declares semantics of a command
NDN_LOG_INIT(nfdc.CommandDefinition)
std::ostream & operator<<(std::ostream &os, ArgValueType vt)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
std::vector< const CommandDefinition * > listCommands(const std::string &noun, ParseMode mode) const
list known commands for help
ParseMode
indicates which mode is the parser operated in
CommandParser & addCommand(const CommandDefinition &def, const ExecuteCommand &execute, std::underlying_type< AvailableIn >::type modes=AVAILABLE_IN_ALL)
add an available command
AvailableIn
indicates which modes is a command allowed
std::tuple< std::string, std::string, CommandArguments, ExecuteCommand > parse(const std::vector< std::string > &tokens, ParseMode mode) const
parse a command line
CommandArguments parse(const std::vector< std::string > &tokens, size_t start=0) 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"
print different string on first and subsequent usage
std::function< void(ExecuteContext &ctx)> ExecuteCommand
a function to execute a command