blob: 7952e25e6eecd4123047007bf8980c1ccf7a857a [file] [log] [blame]
Junxiao Shi64567bb2016-09-04 16:00:27 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry84d3adc2017-08-09 23:31:40 -04002/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi64567bb2016-09-04 16:00:27 +00004 * 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"
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040027#include "status-report.hpp"
Davide Pesavento8b663a92018-11-21 22:57:20 -050028
Junxiao Shi64567bb2016-09-04 16:00:27 +000029#include <ndn-cxx/util/logger.hpp>
30
31namespace nfd {
32namespace tools {
33namespace nfdc {
34
Junxiao Shic143afe2016-09-20 13:04:51 +000035NDN_LOG_INIT(nfdc.CommandDefinition);
Junxiao Shi64567bb2016-09-04 16:00:27 +000036
37std::ostream&
38operator<<(std::ostream& os, ArgValueType vt)
39{
40 switch (vt) {
41 case ArgValueType::NONE:
42 return os << "none";
43 case ArgValueType::ANY:
44 return os << "any";
Eric Newberry84d3adc2017-08-09 23:31:40 -040045 case ArgValueType::BOOLEAN:
46 return os << "boolean";
Junxiao Shi64567bb2016-09-04 16:00:27 +000047 case ArgValueType::UNSIGNED:
48 return os << "non-negative integer";
49 case ArgValueType::STRING:
50 return os << "string";
51 case ArgValueType::REPORT_FORMAT:
52 return os << "ReportFormat";
53 case ArgValueType::NAME:
54 return os << "Name";
55 case ArgValueType::FACE_URI:
56 return os << "FaceUri";
57 case ArgValueType::FACE_ID_OR_URI:
58 return os << "FaceId or FaceUri";
59 case ArgValueType::FACE_PERSISTENCY:
60 return os << "FacePersistency";
Junxiao Shi8eda6822017-04-12 02:53:14 +000061 case ArgValueType::ROUTE_ORIGIN:
62 return os << "RouteOrigin";
Junxiao Shi64567bb2016-09-04 16:00:27 +000063 }
64 return os << static_cast<int>(vt);
65}
66
67static std::string
68getMetavarFromType(ArgValueType vt)
69{
70 switch (vt) {
71 case ArgValueType::NONE:
72 return "";
73 case ArgValueType::ANY:
74 return "args";
Eric Newberry84d3adc2017-08-09 23:31:40 -040075 case ArgValueType::BOOLEAN:
76 return "bool";
Junxiao Shi64567bb2016-09-04 16:00:27 +000077 case ArgValueType::UNSIGNED:
78 return "uint";
79 case ArgValueType::STRING:
80 return "str";
81 case ArgValueType::REPORT_FORMAT:
82 return "fmt";
83 case ArgValueType::NAME:
84 return "name";
85 case ArgValueType::FACE_URI:
86 return "uri";
87 case ArgValueType::FACE_ID_OR_URI:
88 return "face";
89 case ArgValueType::FACE_PERSISTENCY:
90 return "persistency";
Junxiao Shi8eda6822017-04-12 02:53:14 +000091 case ArgValueType::ROUTE_ORIGIN:
92 return "origin";
Junxiao Shi64567bb2016-09-04 16:00:27 +000093 }
Davide Pesavento5a897692019-10-31 01:28:43 -040094 NDN_CXX_UNREACHABLE;
Junxiao Shi64567bb2016-09-04 16:00:27 +000095}
96
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040097CommandDefinition::CommandDefinition(std::string_view noun, std::string_view verb)
Junxiao Shi64567bb2016-09-04 16:00:27 +000098 : m_noun(noun)
99 , m_verb(verb)
100{
101}
102
103CommandDefinition::~CommandDefinition() = default;
104
105CommandDefinition&
106CommandDefinition::addArg(const std::string& name, ArgValueType valueType,
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400107 Required isRequired, Positional allowPositional,
108 const std::string& metavar)
Junxiao Shi64567bb2016-09-04 16:00:27 +0000109{
110 bool isNew = m_args.emplace(name,
111 Arg{name, valueType, static_cast<bool>(isRequired),
112 metavar.empty() ? getMetavarFromType(valueType) : metavar}).second;
Junxiao Shi737c43c2016-09-14 02:51:44 +0000113 BOOST_VERIFY(isNew);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000114
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
130CommandArguments
131CommandDefinition::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;
Eric Newberry84d3adc2017-08-09 23:31:40 -0400146 NDN_LOG_TRACE(token << " is a no-param argument");
Junxiao Shi64567bb2016-09-04 16:00:27 +0000147 }
148 else if (i + 1 >= tokens.size()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500149 NDN_THROW(Error(arg.name + ": " + arg.metavar + " is missing"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000150 }
151 else {
152 const std::string& valueToken = tokens[++i];
153 NDN_LOG_TRACE(arg.name << " has value " << valueToken);
154 try {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400155 ca[arg.name] = parseValue(arg.valueType, valueToken);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000156 }
157 catch (const std::exception& e) {
158 NDN_LOG_TRACE(valueToken << " cannot be parsed as " << arg.valueType);
Davide Pesavento19779d82019-02-14 13:40:04 -0500159 NDN_THROW_NESTED(Error(arg.name + ": cannot parse '" + valueToken + "' as " +
160 arg.metavar + " (" + e.what() + ")"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000161 }
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 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400184 ca[arg.name] = parseValue(arg.valueType, token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000185 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);
Davide Pesavento19779d82019-02-14 13:40:04 -0500191 NDN_THROW_NESTED(Error("cannot parse '" + token + "' as an argument name or as " +
192 arg.metavar + " for " + arg.name + " (" + e.what() + ")"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000193 }
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
Davide Pesavento19779d82019-02-14 13:40:04 -0500204 NDN_THROW(Error("cannot parse '" + token + "' as an argument name"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000205 }
206
207 // token is accepted; don't parse as the same positional argument again
208 ++positionalArgIndex;
209 }
210
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400211 for (const auto& argName : m_requiredArgs) {
Junxiao Shi64567bb2016-09-04 16:00:27 +0000212 if (ca.count(argName) == 0) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500213 NDN_THROW(Error(argName + ": required argument is missing"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000214 }
215 }
216
217 return ca;
218}
219
Eric Newberry84d3adc2017-08-09 23:31:40 -0400220static bool
221parseBoolean(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 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500229 NDN_THROW(std::invalid_argument("unrecognized boolean value '" + s + "'"));
Eric Newberry84d3adc2017-08-09 23:31:40 -0400230}
231
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000232static FacePersistency
Junxiao Shi64567bb2016-09-04 16:00:27 +0000233parseFacePersistency(const std::string& s)
234{
235 if (s == "persistent") {
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000236 return FacePersistency::FACE_PERSISTENCY_PERSISTENT;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000237 }
238 if (s == "permanent") {
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000239 return FacePersistency::FACE_PERSISTENCY_PERMANENT;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000240 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500241 NDN_THROW(std::invalid_argument("unrecognized FacePersistency '" + s + "'"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000242}
243
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400244std::any
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400245CommandDefinition::parseValue(ArgValueType valueType, const std::string& token)
Junxiao Shi64567bb2016-09-04 16:00:27 +0000246{
247 switch (valueType) {
248 case ArgValueType::NONE:
249 case ArgValueType::ANY:
Davide Pesavento5a897692019-10-31 01:28:43 -0400250 break;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000251
Davide Pesavento8b663a92018-11-21 22:57:20 -0500252 case ArgValueType::BOOLEAN:
Eric Newberry84d3adc2017-08-09 23:31:40 -0400253 return parseBoolean(token);
Eric Newberry84d3adc2017-08-09 23:31:40 -0400254
Junxiao Shi64567bb2016-09-04 16:00:27 +0000255 case ArgValueType::UNSIGNED: {
256 // boost::lexical_cast<uint64_t> will accept negative number
257 int64_t v = boost::lexical_cast<int64_t>(token);
258 if (v < 0) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500259 NDN_THROW(std::out_of_range("value '" + token + "' is negative"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000260 }
261 return static_cast<uint64_t>(v);
262 }
263
264 case ArgValueType::STRING:
265 return token;
266
267 case ArgValueType::REPORT_FORMAT:
268 return parseReportFormat(token);
269
270 case ArgValueType::NAME:
271 return Name(token);
272
273 case ArgValueType::FACE_URI:
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000274 return FaceUri(token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000275
276 case ArgValueType::FACE_ID_OR_URI:
277 try {
278 return boost::lexical_cast<uint64_t>(token);
279 }
280 catch (const boost::bad_lexical_cast&) {
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000281 return FaceUri(token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000282 }
283
284 case ArgValueType::FACE_PERSISTENCY:
285 return parseFacePersistency(token);
Junxiao Shi8eda6822017-04-12 02:53:14 +0000286
287 case ArgValueType::ROUTE_ORIGIN:
288 return boost::lexical_cast<RouteOrigin>(token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000289 }
290
Davide Pesavento5a897692019-10-31 01:28:43 -0400291 NDN_CXX_UNREACHABLE;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000292}
293
294} // namespace nfdc
295} // namespace tools
296} // namespace nfd