blob: fb3f9ef30d280826a97fdb9a3ab4e0c8ae5634ed [file] [log] [blame]
Junxiao Shi64567bb2016-09-04 16:00:27 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -05002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, 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#ifndef NFD_TOOLS_NFDC_COMMAND_PARSER_HPP
27#define NFD_TOOLS_NFDC_COMMAND_PARSER_HPP
28
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050029#include "core/common.hpp"
Junxiao Shi64567bb2016-09-04 16:00:27 +000030#include "command-definition.hpp"
Junxiao Shi737c43c2016-09-14 02:51:44 +000031#include "execute-command.hpp"
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050032
Junxiao Shic143afe2016-09-20 13:04:51 +000033#include <type_traits>
Junxiao Shi64567bb2016-09-04 16:00:27 +000034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::tools::nfdc {
Junxiao Shi64567bb2016-09-04 16:00:27 +000036
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040037/** \brief Indicates which modes is a command allowed.
Junxiao Shi64567bb2016-09-04 16:00:27 +000038 */
39enum AvailableIn : uint8_t {
40 AVAILABLE_IN_NONE = 0,
Junxiao Shic143afe2016-09-20 13:04:51 +000041 AVAILABLE_IN_ONE_SHOT = 1 << 0, ///< one-shot mode
42 AVAILABLE_IN_BATCH = 1 << 1, ///< batch mode
Junxiao Shi6c135622016-11-21 14:30:33 +000043 AVAILABLE_IN_HELP = 1 << 7, ///< visible in help listing
Junxiao Shi64567bb2016-09-04 16:00:27 +000044 AVAILABLE_IN_ALL = 0xff
45};
46
47std::ostream&
48operator<<(std::ostream& os, AvailableIn modes);
49
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040050/** \brief Indicates which mode is the parser operated in.
Junxiao Shi64567bb2016-09-04 16:00:27 +000051 */
Junxiao Shic143afe2016-09-20 13:04:51 +000052enum class ParseMode : uint8_t {
Junxiao Shi64567bb2016-09-04 16:00:27 +000053 ONE_SHOT = AVAILABLE_IN_ONE_SHOT, ///< one-shot mode
54 BATCH = AVAILABLE_IN_BATCH ///< batch mode
55};
56
57std::ostream&
58operator<<(std::ostream& os, ParseMode mode);
59
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040060/** \brief Parses a command.
Junxiao Shi64567bb2016-09-04 16:00:27 +000061 */
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050062class CommandParser : boost::noncopyable
Junxiao Shi64567bb2016-09-04 16:00:27 +000063{
64public:
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050065 class NoSuchCommandError : public std::invalid_argument
Junxiao Shi64567bb2016-09-04 16:00:27 +000066 {
67 public:
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050068 NoSuchCommandError(const std::string& noun, const std::string& verb)
69 : std::invalid_argument("No such command: " + noun + " " + verb)
Junxiao Shi64567bb2016-09-04 16:00:27 +000070 {
71 }
72 };
73
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040074 /** \brief Add an available command.
Junxiao Shi64567bb2016-09-04 16:00:27 +000075 * \param def command semantics definition
76 * \param execute a function to execute the command
77 * \param modes parse modes this command should be available in, must not be AVAILABLE_IN_NONE
78 */
79 CommandParser&
Junxiao Shic143afe2016-09-20 13:04:51 +000080 addCommand(const CommandDefinition& def, const ExecuteCommand& execute,
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040081 std::underlying_type_t<AvailableIn> modes = AVAILABLE_IN_ALL);
Junxiao Shi64567bb2016-09-04 16:00:27 +000082
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040083 /** \brief Add an alias "noun verb2" to existing command "noun verb".
Junxiao Shi64567bb2016-09-04 16:00:27 +000084 * \throw std::out_of_range "noun verb" does not exist
85 */
86 CommandParser&
87 addAlias(const std::string& noun, const std::string& verb, const std::string& verb2);
88
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040089 /** \brief List known commands for help.
Junxiao Shi6c135622016-11-21 14:30:33 +000090 * \param noun if not empty, filter results by this noun
91 * \param mode include commands for the specified parse mode
92 * \return commands in insertion order
93 */
94 std::vector<const CommandDefinition*>
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040095 listCommands(std::string_view noun, ParseMode mode) const;
Junxiao Shi6c135622016-11-21 14:30:33 +000096
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040097 /** \brief Parse a command line.
Junxiao Shi64567bb2016-09-04 16:00:27 +000098 * \param tokens command line
99 * \param mode parser mode, must be ParseMode::ONE_SHOT, other modes are not implemented
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500100 * \throw NoSuchCommandError command not found
Junxiao Shi64567bb2016-09-04 16:00:27 +0000101 * \throw CommandDefinition::Error command arguments are invalid
Junxiao Shi737c43c2016-09-14 02:51:44 +0000102 * \return noun, verb, arguments, execute function
Junxiao Shi64567bb2016-09-04 16:00:27 +0000103 */
Junxiao Shi737c43c2016-09-14 02:51:44 +0000104 std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
Davide Pesavento2a588152018-02-19 18:10:03 -0500105 parse(const std::vector<std::string>& tokens, ParseMode mode) const;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000106
107private:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400108 using CommandName = std::pair<std::string, std::string>;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000109
110 struct Command
111 {
112 CommandDefinition def;
Junxiao Shi737c43c2016-09-14 02:51:44 +0000113 ExecuteCommand execute;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000114 AvailableIn modes;
115 };
116
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400117 /** \brief Map from command name or alias to command definition.
Junxiao Shic143afe2016-09-20 13:04:51 +0000118 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400119 using CommandContainer = std::map<CommandName, shared_ptr<Command>>;
Junxiao Shic143afe2016-09-20 13:04:51 +0000120 CommandContainer m_commands;
Junxiao Shi6c135622016-11-21 14:30:33 +0000121
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400122 /** \brief Commands in insertion order.
Junxiao Shi6c135622016-11-21 14:30:33 +0000123 */
124 std::vector<CommandContainer::const_iterator> m_commandOrder;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000125};
126
Davide Pesaventoa9b09b62022-06-04 14:07:25 -0400127void
128registerCommands(CommandParser& parser);
129
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400130} // namespace nfd::tools::nfdc
Junxiao Shi64567bb2016-09-04 16:00:27 +0000131
132#endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP