Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 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" |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 27 | #include "status-report.hpp" |
Davide Pesavento | 8b663a9 | 2018-11-21 22:57:20 -0500 | [diff] [blame] | 28 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame^] | 29 | #include <boost/lexical_cast.hpp> |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/util/logger.hpp> |
| 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tools::nfdc { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 33 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 34 | NDN_LOG_INIT(nfdc.CommandDefinition); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 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"; |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 44 | case ArgValueType::BOOLEAN: |
| 45 | return os << "boolean"; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 46 | case ArgValueType::UNSIGNED: |
| 47 | return os << "non-negative integer"; |
| 48 | case ArgValueType::STRING: |
| 49 | return os << "string"; |
| 50 | case ArgValueType::REPORT_FORMAT: |
| 51 | return os << "ReportFormat"; |
| 52 | case ArgValueType::NAME: |
| 53 | return os << "Name"; |
| 54 | case ArgValueType::FACE_URI: |
| 55 | return os << "FaceUri"; |
| 56 | case ArgValueType::FACE_ID_OR_URI: |
| 57 | return os << "FaceId or FaceUri"; |
| 58 | case ArgValueType::FACE_PERSISTENCY: |
| 59 | return os << "FacePersistency"; |
Junxiao Shi | 8eda682 | 2017-04-12 02:53:14 +0000 | [diff] [blame] | 60 | case ArgValueType::ROUTE_ORIGIN: |
| 61 | return os << "RouteOrigin"; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 62 | } |
| 63 | return os << static_cast<int>(vt); |
| 64 | } |
| 65 | |
| 66 | static std::string |
| 67 | getMetavarFromType(ArgValueType vt) |
| 68 | { |
| 69 | switch (vt) { |
| 70 | case ArgValueType::NONE: |
| 71 | return ""; |
| 72 | case ArgValueType::ANY: |
| 73 | return "args"; |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 74 | case ArgValueType::BOOLEAN: |
| 75 | return "bool"; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 76 | case ArgValueType::UNSIGNED: |
| 77 | return "uint"; |
| 78 | case ArgValueType::STRING: |
| 79 | return "str"; |
| 80 | case ArgValueType::REPORT_FORMAT: |
| 81 | return "fmt"; |
| 82 | case ArgValueType::NAME: |
| 83 | return "name"; |
| 84 | case ArgValueType::FACE_URI: |
| 85 | return "uri"; |
| 86 | case ArgValueType::FACE_ID_OR_URI: |
| 87 | return "face"; |
| 88 | case ArgValueType::FACE_PERSISTENCY: |
| 89 | return "persistency"; |
Junxiao Shi | 8eda682 | 2017-04-12 02:53:14 +0000 | [diff] [blame] | 90 | case ArgValueType::ROUTE_ORIGIN: |
| 91 | return "origin"; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 92 | } |
Davide Pesavento | 5a89769 | 2019-10-31 01:28:43 -0400 | [diff] [blame] | 93 | NDN_CXX_UNREACHABLE; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 96 | CommandDefinition::CommandDefinition(std::string_view noun, std::string_view verb) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 97 | : m_noun(noun) |
| 98 | , m_verb(verb) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | CommandDefinition::~CommandDefinition() = default; |
| 103 | |
| 104 | CommandDefinition& |
| 105 | CommandDefinition::addArg(const std::string& name, ArgValueType valueType, |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 106 | Required isRequired, Positional allowPositional, |
| 107 | const std::string& metavar) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 108 | { |
| 109 | bool isNew = m_args.emplace(name, |
| 110 | Arg{name, valueType, static_cast<bool>(isRequired), |
| 111 | metavar.empty() ? getMetavarFromType(valueType) : metavar}).second; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 112 | BOOST_VERIFY(isNew); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 113 | |
| 114 | if (static_cast<bool>(isRequired)) { |
| 115 | m_requiredArgs.insert(name); |
| 116 | } |
| 117 | |
| 118 | if (static_cast<bool>(allowPositional)) { |
| 119 | BOOST_ASSERT(valueType != ArgValueType::NONE); |
| 120 | m_positionalArgs.push_back(name); |
| 121 | } |
| 122 | else { |
| 123 | BOOST_ASSERT(valueType != ArgValueType::ANY); |
| 124 | } |
| 125 | |
| 126 | return *this; |
| 127 | } |
| 128 | |
| 129 | CommandArguments |
| 130 | CommandDefinition::parse(const std::vector<std::string>& tokens, size_t start) const |
| 131 | { |
| 132 | CommandArguments ca; |
| 133 | |
| 134 | size_t positionalArgIndex = 0; |
| 135 | for (size_t i = start; i < tokens.size(); ++i) { |
| 136 | const std::string& token = tokens[i]; |
| 137 | |
| 138 | // try to parse as named argument |
| 139 | auto namedArg = m_args.find(token); |
| 140 | if (namedArg != m_args.end() && namedArg->second.valueType != ArgValueType::ANY) { |
| 141 | NDN_LOG_TRACE(token << " is a named argument"); |
| 142 | const Arg& arg = namedArg->second; |
| 143 | if (arg.valueType == ArgValueType::NONE) { |
| 144 | ca[arg.name] = true; |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 145 | NDN_LOG_TRACE(token << " is a no-param argument"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 146 | } |
| 147 | else if (i + 1 >= tokens.size()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 148 | NDN_THROW(Error(arg.name + ": " + arg.metavar + " is missing")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 149 | } |
| 150 | else { |
| 151 | const std::string& valueToken = tokens[++i]; |
| 152 | NDN_LOG_TRACE(arg.name << " has value " << valueToken); |
| 153 | try { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 154 | ca[arg.name] = parseValue(arg.valueType, valueToken); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 155 | } |
| 156 | catch (const std::exception& e) { |
| 157 | NDN_LOG_TRACE(valueToken << " cannot be parsed as " << arg.valueType); |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 158 | NDN_THROW_NESTED(Error(arg.name + ": cannot parse '" + valueToken + "' as " + |
| 159 | arg.metavar + " (" + e.what() + ")")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 160 | } |
| 161 | NDN_LOG_TRACE(valueToken << " is parsed as " << arg.valueType); |
| 162 | } |
| 163 | |
| 164 | // disallow positional arguments after named argument |
| 165 | positionalArgIndex = m_positionalArgs.size(); |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | // try to parse as positional argument |
| 170 | for (; positionalArgIndex < m_positionalArgs.size(); ++positionalArgIndex) { |
| 171 | const Arg& arg = m_args.at(m_positionalArgs[positionalArgIndex]); |
| 172 | |
| 173 | if (arg.valueType == ArgValueType::ANY) { |
| 174 | std::vector<std::string> values; |
| 175 | std::copy(tokens.begin() + i, tokens.end(), std::back_inserter(values)); |
| 176 | ca[arg.name] = values; |
| 177 | NDN_LOG_TRACE((tokens.size() - i) << " tokens are consumed for " << arg.name); |
| 178 | i = tokens.size(); |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | try { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 183 | ca[arg.name] = parseValue(arg.valueType, token); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 184 | NDN_LOG_TRACE(token << " is parsed as value for " << arg.name); |
| 185 | break; |
| 186 | } |
| 187 | catch (const std::exception& e) { |
| 188 | if (arg.isRequired) { // the current token must be parsed as the value for arg |
| 189 | NDN_LOG_TRACE(token << " cannot be parsed as value for " << arg.name); |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 190 | NDN_THROW_NESTED(Error("cannot parse '" + token + "' as an argument name or as " + |
| 191 | arg.metavar + " for " + arg.name + " (" + e.what() + ")")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 192 | } |
| 193 | else { |
| 194 | // the current token may be a value for next positional argument |
| 195 | NDN_LOG_TRACE(token << " cannot be parsed as value for " << arg.name); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (positionalArgIndex >= m_positionalArgs.size()) { |
| 201 | // for loop has reached the end without finding a match, |
| 202 | // which means token is not accepted as a value for positional argument |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 203 | NDN_THROW(Error("cannot parse '" + token + "' as an argument name")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // token is accepted; don't parse as the same positional argument again |
| 207 | ++positionalArgIndex; |
| 208 | } |
| 209 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 210 | for (const auto& argName : m_requiredArgs) { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 211 | if (ca.count(argName) == 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 212 | NDN_THROW(Error(argName + ": required argument is missing")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | return ca; |
| 217 | } |
| 218 | |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 219 | static bool |
| 220 | parseBoolean(const std::string& s) |
| 221 | { |
| 222 | if (s == "on" || s == "true" || s == "enabled" || s == "yes" || s == "1") { |
| 223 | return true; |
| 224 | } |
| 225 | if (s == "off" || s == "false" || s == "disabled" || s == "no" || s == "0") { |
| 226 | return false; |
| 227 | } |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 228 | NDN_THROW(std::invalid_argument("unrecognized boolean value '" + s + "'")); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Junxiao Shi | 215fd0b | 2017-01-28 19:01:04 +0000 | [diff] [blame] | 231 | static FacePersistency |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 232 | parseFacePersistency(const std::string& s) |
| 233 | { |
| 234 | if (s == "persistent") { |
Junxiao Shi | 215fd0b | 2017-01-28 19:01:04 +0000 | [diff] [blame] | 235 | return FacePersistency::FACE_PERSISTENCY_PERSISTENT; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 236 | } |
| 237 | if (s == "permanent") { |
Junxiao Shi | 215fd0b | 2017-01-28 19:01:04 +0000 | [diff] [blame] | 238 | return FacePersistency::FACE_PERSISTENCY_PERMANENT; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 239 | } |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 240 | NDN_THROW(std::invalid_argument("unrecognized FacePersistency '" + s + "'")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 243 | std::any |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 244 | CommandDefinition::parseValue(ArgValueType valueType, const std::string& token) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 245 | { |
| 246 | switch (valueType) { |
| 247 | case ArgValueType::NONE: |
| 248 | case ArgValueType::ANY: |
Davide Pesavento | 5a89769 | 2019-10-31 01:28:43 -0400 | [diff] [blame] | 249 | break; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 250 | |
Davide Pesavento | 8b663a9 | 2018-11-21 22:57:20 -0500 | [diff] [blame] | 251 | case ArgValueType::BOOLEAN: |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 252 | return parseBoolean(token); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 253 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 254 | case ArgValueType::UNSIGNED: { |
| 255 | // boost::lexical_cast<uint64_t> will accept negative number |
| 256 | int64_t v = boost::lexical_cast<int64_t>(token); |
| 257 | if (v < 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 258 | NDN_THROW(std::out_of_range("value '" + token + "' is negative")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 259 | } |
| 260 | return static_cast<uint64_t>(v); |
| 261 | } |
| 262 | |
| 263 | case ArgValueType::STRING: |
| 264 | return token; |
| 265 | |
| 266 | case ArgValueType::REPORT_FORMAT: |
| 267 | return parseReportFormat(token); |
| 268 | |
| 269 | case ArgValueType::NAME: |
| 270 | return Name(token); |
| 271 | |
| 272 | case ArgValueType::FACE_URI: |
Junxiao Shi | 215fd0b | 2017-01-28 19:01:04 +0000 | [diff] [blame] | 273 | return FaceUri(token); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 274 | |
| 275 | case ArgValueType::FACE_ID_OR_URI: |
| 276 | try { |
| 277 | return boost::lexical_cast<uint64_t>(token); |
| 278 | } |
| 279 | catch (const boost::bad_lexical_cast&) { |
Junxiao Shi | 215fd0b | 2017-01-28 19:01:04 +0000 | [diff] [blame] | 280 | return FaceUri(token); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | case ArgValueType::FACE_PERSISTENCY: |
| 284 | return parseFacePersistency(token); |
Junxiao Shi | 8eda682 | 2017-04-12 02:53:14 +0000 | [diff] [blame] | 285 | |
| 286 | case ArgValueType::ROUTE_ORIGIN: |
| 287 | return boost::lexical_cast<RouteOrigin>(token); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Davide Pesavento | 5a89769 | 2019-10-31 01:28:43 -0400 | [diff] [blame] | 290 | NDN_CXX_UNREACHABLE; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 293 | } // namespace nfd::tools::nfdc |