blob: a2ceb338e6022307e8bda236bc3622aef81cd6bc [file] [log] [blame]
Junxiao Shi64567bb2016-09-04 16:00:27 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi215fd0b2017-01-28 19:01:04 +00003 * Copyright (c) 2014-2017, 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"
Junxiao Shi64567bb2016-09-04 16:00:27 +000027#include <ndn-cxx/util/logger.hpp>
28
29namespace nfd {
30namespace tools {
31namespace nfdc {
32
Junxiao Shic143afe2016-09-20 13:04:51 +000033NDN_LOG_INIT(nfdc.CommandDefinition);
Junxiao Shi64567bb2016-09-04 16:00:27 +000034
35std::ostream&
36operator<<(std::ostream& os, ArgValueType vt)
37{
38 switch (vt) {
39 case ArgValueType::NONE:
40 return os << "none";
41 case ArgValueType::ANY:
42 return os << "any";
43 case ArgValueType::UNSIGNED:
44 return os << "non-negative integer";
45 case ArgValueType::STRING:
46 return os << "string";
47 case ArgValueType::REPORT_FORMAT:
48 return os << "ReportFormat";
49 case ArgValueType::NAME:
50 return os << "Name";
51 case ArgValueType::FACE_URI:
52 return os << "FaceUri";
53 case ArgValueType::FACE_ID_OR_URI:
54 return os << "FaceId or FaceUri";
55 case ArgValueType::FACE_PERSISTENCY:
56 return os << "FacePersistency";
Junxiao Shi8eda6822017-04-12 02:53:14 +000057 case ArgValueType::ROUTE_ORIGIN:
58 return os << "RouteOrigin";
Junxiao Shi64567bb2016-09-04 16:00:27 +000059 }
60 return os << static_cast<int>(vt);
61}
62
63static std::string
64getMetavarFromType(ArgValueType vt)
65{
66 switch (vt) {
67 case ArgValueType::NONE:
68 return "";
69 case ArgValueType::ANY:
70 return "args";
71 case ArgValueType::UNSIGNED:
72 return "uint";
73 case ArgValueType::STRING:
74 return "str";
75 case ArgValueType::REPORT_FORMAT:
76 return "fmt";
77 case ArgValueType::NAME:
78 return "name";
79 case ArgValueType::FACE_URI:
80 return "uri";
81 case ArgValueType::FACE_ID_OR_URI:
82 return "face";
83 case ArgValueType::FACE_PERSISTENCY:
84 return "persistency";
Junxiao Shi8eda6822017-04-12 02:53:14 +000085 case ArgValueType::ROUTE_ORIGIN:
86 return "origin";
Junxiao Shi64567bb2016-09-04 16:00:27 +000087 }
88 BOOST_ASSERT(false);
89 return "";
90}
91
92CommandDefinition::CommandDefinition(const std::string& noun, const std::string& verb)
93 : m_noun(noun)
94 , m_verb(verb)
95{
96}
97
98CommandDefinition::~CommandDefinition() = default;
99
100CommandDefinition&
101CommandDefinition::addArg(const std::string& name, ArgValueType valueType,
102 Required isRequired, Positional allowPositional,
103 const std::string& metavar)
104{
105 bool isNew = m_args.emplace(name,
106 Arg{name, valueType, static_cast<bool>(isRequired),
107 metavar.empty() ? getMetavarFromType(valueType) : metavar}).second;
Junxiao Shi737c43c2016-09-14 02:51:44 +0000108 BOOST_VERIFY(isNew);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000109
110 if (static_cast<bool>(isRequired)) {
111 m_requiredArgs.insert(name);
112 }
113
114 if (static_cast<bool>(allowPositional)) {
115 BOOST_ASSERT(valueType != ArgValueType::NONE);
116 m_positionalArgs.push_back(name);
117 }
118 else {
119 BOOST_ASSERT(valueType != ArgValueType::ANY);
120 }
121
122 return *this;
123}
124
125CommandArguments
126CommandDefinition::parse(const std::vector<std::string>& tokens, size_t start) const
127{
128 CommandArguments ca;
129
130 size_t positionalArgIndex = 0;
131 for (size_t i = start; i < tokens.size(); ++i) {
132 const std::string& token = tokens[i];
133
134 // try to parse as named argument
135 auto namedArg = m_args.find(token);
136 if (namedArg != m_args.end() && namedArg->second.valueType != ArgValueType::ANY) {
137 NDN_LOG_TRACE(token << " is a named argument");
138 const Arg& arg = namedArg->second;
139 if (arg.valueType == ArgValueType::NONE) {
140 ca[arg.name] = true;
141 NDN_LOG_TRACE(token << " is a boolean argument");
142 }
143 else if (i + 1 >= tokens.size()) {
144 BOOST_THROW_EXCEPTION(Error(arg.name + ": " + arg.metavar + " is missing"));
145 }
146 else {
147 const std::string& valueToken = tokens[++i];
148 NDN_LOG_TRACE(arg.name << " has value " << valueToken);
149 try {
150 ca[arg.name] = this->parseValue(arg.valueType, valueToken);
151 }
152 catch (const std::exception& e) {
153 NDN_LOG_TRACE(valueToken << " cannot be parsed as " << arg.valueType);
154 BOOST_THROW_EXCEPTION(Error(arg.name + ": cannot parse '" + valueToken + "' as " +
155 arg.metavar + " (" + e.what() + ")"));
156 }
157 NDN_LOG_TRACE(valueToken << " is parsed as " << arg.valueType);
158 }
159
160 // disallow positional arguments after named argument
161 positionalArgIndex = m_positionalArgs.size();
162 continue;
163 }
164
165 // try to parse as positional argument
166 for (; positionalArgIndex < m_positionalArgs.size(); ++positionalArgIndex) {
167 const Arg& arg = m_args.at(m_positionalArgs[positionalArgIndex]);
168
169 if (arg.valueType == ArgValueType::ANY) {
170 std::vector<std::string> values;
171 std::copy(tokens.begin() + i, tokens.end(), std::back_inserter(values));
172 ca[arg.name] = values;
173 NDN_LOG_TRACE((tokens.size() - i) << " tokens are consumed for " << arg.name);
174 i = tokens.size();
175 break;
176 }
177
178 try {
179 ca[arg.name] = this->parseValue(arg.valueType, token);
180 NDN_LOG_TRACE(token << " is parsed as value for " << arg.name);
181 break;
182 }
183 catch (const std::exception& e) {
184 if (arg.isRequired) { // the current token must be parsed as the value for arg
185 NDN_LOG_TRACE(token << " cannot be parsed as value for " << arg.name);
186 BOOST_THROW_EXCEPTION(Error("cannot parse '" + token + "' as an argument name or as " +
187 arg.metavar + " for " + arg.name + " (" + e.what() + ")"));
188 }
189 else {
190 // the current token may be a value for next positional argument
191 NDN_LOG_TRACE(token << " cannot be parsed as value for " << arg.name);
192 }
193 }
194 }
195
196 if (positionalArgIndex >= m_positionalArgs.size()) {
197 // for loop has reached the end without finding a match,
198 // which means token is not accepted as a value for positional argument
199 BOOST_THROW_EXCEPTION(Error("cannot parse '" + token + "' as an argument name"));
200 }
201
202 // token is accepted; don't parse as the same positional argument again
203 ++positionalArgIndex;
204 }
205
206 for (const std::string& argName : m_requiredArgs) {
207 if (ca.count(argName) == 0) {
208 BOOST_THROW_EXCEPTION(Error(argName + ": required argument is missing"));
209 }
210 }
211
212 return ca;
213}
214
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000215static FacePersistency
Junxiao Shi64567bb2016-09-04 16:00:27 +0000216parseFacePersistency(const std::string& s)
217{
218 if (s == "persistent") {
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000219 return FacePersistency::FACE_PERSISTENCY_PERSISTENT;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000220 }
221 if (s == "permanent") {
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000222 return FacePersistency::FACE_PERSISTENCY_PERMANENT;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000223 }
224 BOOST_THROW_EXCEPTION(std::invalid_argument("unrecognized FacePersistency"));
225}
226
227boost::any
228CommandDefinition::parseValue(ArgValueType valueType, const std::string& token) const
229{
230 switch (valueType) {
231 case ArgValueType::NONE:
232 case ArgValueType::ANY:
233 BOOST_ASSERT(false);
234 return boost::any();
235
236 case ArgValueType::UNSIGNED: {
237 // boost::lexical_cast<uint64_t> will accept negative number
238 int64_t v = boost::lexical_cast<int64_t>(token);
239 if (v < 0) {
240 BOOST_THROW_EXCEPTION(std::out_of_range("value is negative"));
241 }
242 return static_cast<uint64_t>(v);
243 }
244
245 case ArgValueType::STRING:
246 return token;
247
248 case ArgValueType::REPORT_FORMAT:
249 return parseReportFormat(token);
250
251 case ArgValueType::NAME:
252 return Name(token);
253
254 case ArgValueType::FACE_URI:
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000255 return FaceUri(token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000256
257 case ArgValueType::FACE_ID_OR_URI:
258 try {
259 return boost::lexical_cast<uint64_t>(token);
260 }
261 catch (const boost::bad_lexical_cast&) {
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000262 return FaceUri(token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000263 }
264
265 case ArgValueType::FACE_PERSISTENCY:
266 return parseFacePersistency(token);
Junxiao Shi8eda6822017-04-12 02:53:14 +0000267
268 case ArgValueType::ROUTE_ORIGIN:
269 return boost::lexical_cast<RouteOrigin>(token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000270 }
271
272 BOOST_ASSERT(false);
273 return boost::any();
274}
275
276} // namespace nfdc
277} // namespace tools
278} // namespace nfd