blob: 19bb5a24516a1a143248c38762965b4a98d5764a [file] [log] [blame]
Junxiao Shi64567bb2016-09-04 16:00:27 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi15902ef2017-08-11 22:58:35 +00002/*
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-parser.hpp"
Junxiao Shic143afe2016-09-20 13:04:51 +000027#include "format-helpers.hpp"
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050028
Junxiao Shi64567bb2016-09-04 16:00:27 +000029#include <ndn-cxx/util/logger.hpp>
30
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tools::nfdc {
Junxiao Shi64567bb2016-09-04 16:00:27 +000032
Junxiao Shic143afe2016-09-20 13:04:51 +000033NDN_LOG_INIT(nfdc.CommandParser);
34
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040035static_assert(std::is_same_v<std::underlying_type_t<AvailableIn>,
36 std::underlying_type_t<ParseMode>>,
37 "AvailableIn and ParseMode must be declared with the same underlying type");
Junxiao Shi64567bb2016-09-04 16:00:27 +000038
39std::ostream&
40operator<<(std::ostream& os, AvailableIn modes)
41{
Junxiao Shic143afe2016-09-20 13:04:51 +000042 text::Separator sep("|");
43 if ((modes & AVAILABLE_IN_ONE_SHOT) != 0) {
44 os << sep << "one-shot";
45 }
46 if ((modes & AVAILABLE_IN_BATCH) != 0) {
47 os << sep << "batch";
Junxiao Shi64567bb2016-09-04 16:00:27 +000048 }
Junxiao Shi6c135622016-11-21 14:30:33 +000049 if ((modes & AVAILABLE_IN_HELP) == 0) {
50 os << sep << "hidden";
51 }
Junxiao Shi64567bb2016-09-04 16:00:27 +000052
Junxiao Shic143afe2016-09-20 13:04:51 +000053 if (sep.getCount() == 0) {
Junxiao Shi64567bb2016-09-04 16:00:27 +000054 os << "none";
55 }
56 return os;
57}
58
59std::ostream&
60operator<<(std::ostream& os, ParseMode mode)
61{
62 switch (mode) {
63 case ParseMode::ONE_SHOT:
64 return os << "one-shot";
65 case ParseMode::BATCH:
66 return os << "batch";
67 }
68 return os << static_cast<int>(mode);
69}
70
71CommandParser&
Junxiao Shic143afe2016-09-20 13:04:51 +000072CommandParser::addCommand(const CommandDefinition& def, const ExecuteCommand& execute,
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040073 std::underlying_type_t<AvailableIn> modes)
Junxiao Shi64567bb2016-09-04 16:00:27 +000074{
75 BOOST_ASSERT(modes != AVAILABLE_IN_NONE);
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050076
Junxiao Shic143afe2016-09-20 13:04:51 +000077 m_commands[{def.getNoun(), def.getVerb()}].reset(
78 new Command{def, execute, static_cast<AvailableIn>(modes)});
Junxiao Shi6c135622016-11-21 14:30:33 +000079
80 if ((modes & AVAILABLE_IN_HELP) != 0) {
81 m_commandOrder.push_back(m_commands.find({def.getNoun(), def.getVerb()}));
82 }
83
Junxiao Shi64567bb2016-09-04 16:00:27 +000084 return *this;
85}
86
87CommandParser&
88CommandParser::addAlias(const std::string& noun, const std::string& verb, const std::string& verb2)
89{
90 m_commands[{noun, verb2}] = m_commands.at({noun, verb});
91 return *this;
92}
93
Junxiao Shi6c135622016-11-21 14:30:33 +000094std::vector<const CommandDefinition*>
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040095CommandParser::listCommands(std::string_view noun, ParseMode mode) const
Junxiao Shi6c135622016-11-21 14:30:33 +000096{
97 std::vector<const CommandDefinition*> results;
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050098 for (auto i : m_commandOrder) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040099 const auto& command = *i->second;
Junxiao Shi6c135622016-11-21 14:30:33 +0000100 if ((command.modes & static_cast<AvailableIn>(mode)) != 0 &&
101 (noun.empty() || noun == command.def.getNoun())) {
102 results.push_back(&command.def);
103 }
104 }
105 return results;
106}
107
Junxiao Shi737c43c2016-09-14 02:51:44 +0000108std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
Davide Pesavento2a588152018-02-19 18:10:03 -0500109CommandParser::parse(const std::vector<std::string>& tokens, ParseMode mode) const
Junxiao Shi64567bb2016-09-04 16:00:27 +0000110{
111 BOOST_ASSERT(mode == ParseMode::ONE_SHOT);
112
113 const std::string& noun = tokens.size() > 0 ? tokens[0] : "";
114 const std::string& verb = tokens.size() > 1 ? tokens[1] : "";
Junxiao Shi64567bb2016-09-04 16:00:27 +0000115
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500116 NDN_LOG_TRACE("parse mode=" << mode << " noun=" << noun << " verb=" << verb);
117
Junxiao Shi64567bb2016-09-04 16:00:27 +0000118 auto i = m_commands.find({noun, verb});
Junxiao Shi64567bb2016-09-04 16:00:27 +0000119 if (i == m_commands.end() || (i->second->modes & static_cast<AvailableIn>(mode)) == 0) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500120 NDN_THROW(NoSuchCommandError(noun, verb));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000121 }
122
123 const CommandDefinition& def = i->second->def;
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500124 NDN_LOG_TRACE("found command noun=" << def.getNoun() << " verb=" << def.getVerb());
Junxiao Shi64567bb2016-09-04 16:00:27 +0000125
Davide Pesaventod2147442018-02-19 23:58:17 -0500126 size_t nConsumed = std::min<size_t>(2, tokens.size());
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400127 return {def.getNoun(), def.getVerb(), def.parse(tokens, nConsumed), i->second->execute};
Junxiao Shi64567bb2016-09-04 16:00:27 +0000128}
129
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400130} // namespace nfd::tools::nfdc