command-definition.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-definition.hpp"
27 
28 #include <ndn-cxx/util/logger.hpp>
29 
30 namespace nfd {
31 namespace tools {
32 namespace nfdc {
33 
34 NDN_LOG_INIT(nfdc.CommandDefinition);
35 
36 std::ostream&
37 operator<<(std::ostream& os, ArgValueType vt)
38 {
39  switch (vt) {
40  case ArgValueType::NONE:
41  return os << "none";
42  case ArgValueType::ANY:
43  return os << "any";
45  return os << "boolean";
47  return os << "non-negative integer";
49  return os << "string";
51  return os << "ReportFormat";
52  case ArgValueType::NAME:
53  return os << "Name";
55  return os << "FaceUri";
57  return os << "FaceId or FaceUri";
59  return os << "FacePersistency";
61  return os << "RouteOrigin";
62  }
63  return os << static_cast<int>(vt);
64 }
65 
66 static std::string
68 {
69  switch (vt) {
70  case ArgValueType::NONE:
71  return "";
72  case ArgValueType::ANY:
73  return "args";
75  return "bool";
77  return "uint";
79  return "str";
81  return "fmt";
82  case ArgValueType::NAME:
83  return "name";
85  return "uri";
87  return "face";
89  return "persistency";
91  return "origin";
92  }
93  BOOST_ASSERT(false);
94  return "";
95 }
96 
97 CommandDefinition::CommandDefinition(const std::string& noun, const std::string& verb)
98  : m_noun(noun)
99  , m_verb(verb)
100 {
101 }
102 
104 
106 CommandDefinition::addArg(const std::string& name, ArgValueType valueType,
107  Required isRequired, Positional allowPositional,
108  const std::string& metavar)
109 {
110  bool isNew = m_args.emplace(name,
111  Arg{name, valueType, static_cast<bool>(isRequired),
112  metavar.empty() ? getMetavarFromType(valueType) : metavar}).second;
113  BOOST_VERIFY(isNew);
114 
115  if (static_cast<bool>(isRequired)) {
116  m_requiredArgs.insert(name);
117  }
118 
119  if (static_cast<bool>(allowPositional)) {
120  BOOST_ASSERT(valueType != ArgValueType::NONE);
121  m_positionalArgs.push_back(name);
122  }
123  else {
124  BOOST_ASSERT(valueType != ArgValueType::ANY);
125  }
126 
127  return *this;
128 }
129 
131 CommandDefinition::parse(const std::vector<std::string>& tokens, size_t start) const
132 {
133  CommandArguments ca;
134 
135  size_t positionalArgIndex = 0;
136  for (size_t i = start; i < tokens.size(); ++i) {
137  const std::string& token = tokens[i];
138 
139  // try to parse as named argument
140  auto namedArg = m_args.find(token);
141  if (namedArg != m_args.end() && namedArg->second.valueType != ArgValueType::ANY) {
142  NDN_LOG_TRACE(token << " is a named argument");
143  const Arg& arg = namedArg->second;
144  if (arg.valueType == ArgValueType::NONE) {
145  ca[arg.name] = true;
146  NDN_LOG_TRACE(token << " is a no-param argument");
147  }
148  else if (i + 1 >= tokens.size()) {
149  BOOST_THROW_EXCEPTION(Error(arg.name + ": " + arg.metavar + " is missing"));
150  }
151  else {
152  const std::string& valueToken = tokens[++i];
153  NDN_LOG_TRACE(arg.name << " has value " << valueToken);
154  try {
155  ca[arg.name] = this->parseValue(arg.valueType, valueToken);
156  }
157  catch (const std::exception& e) {
158  NDN_LOG_TRACE(valueToken << " cannot be parsed as " << arg.valueType);
159  BOOST_THROW_EXCEPTION(Error(arg.name + ": cannot parse '" + valueToken + "' as " +
160  arg.metavar + " (" + e.what() + ")"));
161  }
162  NDN_LOG_TRACE(valueToken << " is parsed as " << arg.valueType);
163  }
164 
165  // disallow positional arguments after named argument
166  positionalArgIndex = m_positionalArgs.size();
167  continue;
168  }
169 
170  // try to parse as positional argument
171  for (; positionalArgIndex < m_positionalArgs.size(); ++positionalArgIndex) {
172  const Arg& arg = m_args.at(m_positionalArgs[positionalArgIndex]);
173 
174  if (arg.valueType == ArgValueType::ANY) {
175  std::vector<std::string> values;
176  std::copy(tokens.begin() + i, tokens.end(), std::back_inserter(values));
177  ca[arg.name] = values;
178  NDN_LOG_TRACE((tokens.size() - i) << " tokens are consumed for " << arg.name);
179  i = tokens.size();
180  break;
181  }
182 
183  try {
184  ca[arg.name] = this->parseValue(arg.valueType, token);
185  NDN_LOG_TRACE(token << " is parsed as value for " << arg.name);
186  break;
187  }
188  catch (const std::exception& e) {
189  if (arg.isRequired) { // the current token must be parsed as the value for arg
190  NDN_LOG_TRACE(token << " cannot be parsed as value for " << arg.name);
191  BOOST_THROW_EXCEPTION(Error("cannot parse '" + token + "' as an argument name or as " +
192  arg.metavar + " for " + arg.name + " (" + e.what() + ")"));
193  }
194  else {
195  // the current token may be a value for next positional argument
196  NDN_LOG_TRACE(token << " cannot be parsed as value for " << arg.name);
197  }
198  }
199  }
200 
201  if (positionalArgIndex >= m_positionalArgs.size()) {
202  // for loop has reached the end without finding a match,
203  // which means token is not accepted as a value for positional argument
204  BOOST_THROW_EXCEPTION(Error("cannot parse '" + token + "' as an argument name"));
205  }
206 
207  // token is accepted; don't parse as the same positional argument again
208  ++positionalArgIndex;
209  }
210 
211  for (const std::string& argName : m_requiredArgs) {
212  if (ca.count(argName) == 0) {
213  BOOST_THROW_EXCEPTION(Error(argName + ": required argument is missing"));
214  }
215  }
216 
217  return ca;
218 }
219 
220 static bool
221 parseBoolean(const std::string& s)
222 {
223  if (s == "on" || s == "true" || s == "enabled" || s == "yes" || s == "1") {
224  return true;
225  }
226  if (s == "off" || s == "false" || s == "disabled" || s == "no" || s == "0") {
227  return false;
228  }
229  BOOST_THROW_EXCEPTION(std::invalid_argument("unrecognized boolean value '" + s + "'"));
230 }
231 
232 static FacePersistency
233 parseFacePersistency(const std::string& s)
234 {
235  if (s == "persistent") {
236  return FacePersistency::FACE_PERSISTENCY_PERSISTENT;
237  }
238  if (s == "permanent") {
239  return FacePersistency::FACE_PERSISTENCY_PERMANENT;
240  }
241  BOOST_THROW_EXCEPTION(std::invalid_argument("unrecognized FacePersistency '" + s + "'"));
242 }
243 
244 ndn::any
245 CommandDefinition::parseValue(ArgValueType valueType, const std::string& token) const
246 {
247  switch (valueType) {
248  case ArgValueType::NONE:
249  case ArgValueType::ANY:
250  BOOST_ASSERT(false);
251  return {};
252 
254  return parseBoolean(token);
255 
256  case ArgValueType::UNSIGNED: {
257  // boost::lexical_cast<uint64_t> will accept negative number
258  int64_t v = boost::lexical_cast<int64_t>(token);
259  if (v < 0) {
260  BOOST_THROW_EXCEPTION(std::out_of_range("value '" + token + "' is negative"));
261  }
262  return static_cast<uint64_t>(v);
263  }
264 
266  return token;
267 
269  return parseReportFormat(token);
270 
271  case ArgValueType::NAME:
272  return Name(token);
273 
275  return FaceUri(token);
276 
278  try {
279  return boost::lexical_cast<uint64_t>(token);
280  }
281  catch (const boost::bad_lexical_cast&) {
282  return FaceUri(token);
283  }
284 
286  return parseFacePersistency(token);
287 
289  return boost::lexical_cast<RouteOrigin>(token);
290  }
291 
292  BOOST_ASSERT(false);
293  return {};
294 }
295 
296 } // namespace nfdc
297 } // namespace tools
298 } // namespace nfd
ReportFormat parseReportFormat(const std::string &s)
boolean argument without value
Required
indicates whether an argument is required
declares semantics of a command
NDN_LOG_INIT(nfdc.CommandDefinition)
ArgValueType
indicates argument value type
static std::string getMetavarFromType(ArgValueType vt)
CommandDefinition(const std::string &noun, const std::string &verb)
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
contains named command arguments
face persistency &#39;persistent&#39; or &#39;permanent&#39;
static FacePersistency parseFacePersistency(const std::string &s)
CommandDefinition & addArg(const std::string &name, ArgValueType valueType, Required isRequired=Required::NO, Positional allowPositional=Positional::NO, const std::string &metavar="")
declare an argument
static bool parseBoolean(const std::string &s)
CommandArguments parse(const std::vector< std::string > &tokens, size_t start=0) const
parse a command line
report format &#39;xml&#39; or &#39;text&#39;
Positional
indicates whether an argument can be specified as positional