blob: 07ecd1569866df4c2f2cceda0fab8ffbde1b24dd [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#ifndef NFD_TOOLS_NFDC_COMMAND_DEFINITION_HPP
27#define NFD_TOOLS_NFDC_COMMAND_DEFINITION_HPP
28
29#include "command-arguments.hpp"
30
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tools::nfdc {
Junxiao Shi64567bb2016-09-04 16:00:27 +000032
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040033/** \brief Indicates argument value type.
Junxiao Shi64567bb2016-09-04 16:00:27 +000034 */
35enum class ArgValueType {
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040036 /** \brief Boolean argument without value.
Junxiao Shi64567bb2016-09-04 16:00:27 +000037 *
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038 * The argument appears in CommandArguments as bool value `true`.
Junxiao Shi64567bb2016-09-04 16:00:27 +000039 * It must not be declared as positional.
40 */
41 NONE,
42
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040043 /** \brief Any arguments.
Junxiao Shi64567bb2016-09-04 16:00:27 +000044 *
45 * The argument appears in CommandArguments as std::vector<std::string>.
46 * It must be declared as positional, and will consume all subsequent tokens.
47 */
48 ANY,
49
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040050 /** \brief Boolean.
Eric Newberry84d3adc2017-08-09 23:31:40 -040051 *
52 * The argument appears in CommandArguments as bool.
53 */
54 BOOLEAN,
55
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040056 /** \brief Non-negative integer.
Junxiao Shi64567bb2016-09-04 16:00:27 +000057 *
58 * The argument appears in CommandArguments as uint64_t.
59 * Acceptable input range is [0, std::numeric_limits<int64_t>::max()].
60 */
61 UNSIGNED,
62
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040063 /** \brief Arbitrary string.
Junxiao Shi64567bb2016-09-04 16:00:27 +000064 *
65 * The argument appears in CommandArguments as std::string.
66 */
67 STRING,
68
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040069 /** \brief Report format 'xml' or 'text'.
Junxiao Shi64567bb2016-09-04 16:00:27 +000070 *
71 * The argument appears in CommandArguments as nfd::tools::nfdc::ReportFormat.
72 */
73 REPORT_FORMAT,
74
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040075 /** \brief Name prefix.
Junxiao Shi64567bb2016-09-04 16:00:27 +000076 *
77 * The argument appears in CommandArguments as ndn::Name.
78 */
79 NAME,
80
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040081 /** \brief FaceUri.
Junxiao Shi64567bb2016-09-04 16:00:27 +000082 *
Junxiao Shi83be1da2017-06-30 13:37:37 +000083 * The argument appears in CommandArguments as ndn::FaceUri.
Junxiao Shi64567bb2016-09-04 16:00:27 +000084 */
85 FACE_URI,
86
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040087 /** \brief FaceId or FaceUri.
Junxiao Shi64567bb2016-09-04 16:00:27 +000088 *
Junxiao Shi83be1da2017-06-30 13:37:37 +000089 * The argument appears in CommandArguments as either uint64_t or ndn::FaceUri.
Junxiao Shi64567bb2016-09-04 16:00:27 +000090 */
91 FACE_ID_OR_URI,
92
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040093 /** \brief Face persistency 'persistent' or 'permanent'.
Junxiao Shi64567bb2016-09-04 16:00:27 +000094 *
95 * The argument appears in CommandArguments as ndn::nfd::FacePersistency.
96 */
Junxiao Shi8eda6822017-04-12 02:53:14 +000097 FACE_PERSISTENCY,
98
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040099 /** \brief Route origin.
Junxiao Shi8eda6822017-04-12 02:53:14 +0000100 *
101 * The argument appears in CommandArguments as ndn::nfd::RouteOrigin.
102 */
103 ROUTE_ORIGIN,
Junxiao Shi64567bb2016-09-04 16:00:27 +0000104};
105
106std::ostream&
107operator<<(std::ostream& os, ArgValueType vt);
108
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400109/** \brief Indicates whether an argument is required.
Junxiao Shi64567bb2016-09-04 16:00:27 +0000110 */
111enum class Required {
112 NO = false, ///< argument is optional
113 YES = true ///< argument is required
114};
115
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400116/** \brief Indicates whether an argument can be specified as positional.
Junxiao Shi64567bb2016-09-04 16:00:27 +0000117 */
118enum class Positional {
119 NO = false, ///< argument must be named
120 YES = true ///< argument can be specified as positional
121};
122
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400123/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400124 * \brief Defines a command.
Junxiao Shi64567bb2016-09-04 16:00:27 +0000125 */
126class CommandDefinition
127{
128public:
129 class Error : public std::invalid_argument
130 {
131 public:
Davide Pesavento8b663a92018-11-21 22:57:20 -0500132 using std::invalid_argument::invalid_argument;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000133 };
134
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400135 CommandDefinition(std::string_view noun, std::string_view verb);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000136
137 ~CommandDefinition();
138
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400139 const std::string&
Junxiao Shi64567bb2016-09-04 16:00:27 +0000140 getNoun() const
141 {
142 return m_noun;
143 }
144
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400145 const std::string&
Junxiao Shi64567bb2016-09-04 16:00:27 +0000146 getVerb() const
147 {
148 return m_verb;
149 }
150
151public: // help
Junxiao Shi6c135622016-11-21 14:30:33 +0000152 /** \return one-line description
Junxiao Shi64567bb2016-09-04 16:00:27 +0000153 */
154 const std::string&
Junxiao Shi6c135622016-11-21 14:30:33 +0000155 getTitle() const
Junxiao Shi64567bb2016-09-04 16:00:27 +0000156 {
Junxiao Shi6c135622016-11-21 14:30:33 +0000157 return m_title;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000158 }
159
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400160 /** \brief Set one-line description.
Junxiao Shi6c135622016-11-21 14:30:33 +0000161 * \param title one-line description, written in lower case
Junxiao Shi64567bb2016-09-04 16:00:27 +0000162 */
163 CommandDefinition&
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400164 setTitle(std::string_view title)
Junxiao Shi64567bb2016-09-04 16:00:27 +0000165 {
Junxiao Shi6c135622016-11-21 14:30:33 +0000166 m_title = title;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000167 return *this;
168 }
169
170public: // arguments
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400171 /** \brief Declare an argument.
Junxiao Shi64567bb2016-09-04 16:00:27 +0000172 * \param name argument name, must be unique
173 * \param valueType argument value type
174 * \param isRequired whether the argument is required
175 * \param allowPositional whether the argument value can be specified as positional
176 * \param metavar displayed argument value placeholder
177 */
178 CommandDefinition&
179 addArg(const std::string& name, ArgValueType valueType,
180 Required isRequired = Required::NO,
181 Positional allowPositional = Positional::NO,
182 const std::string& metavar = "");
183
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400184 /** \brief Parse a command line.
Junxiao Shi64567bb2016-09-04 16:00:27 +0000185 * \param tokens command line tokens
186 * \param start command line start position, after noun and verb
187 * \throw Error command line is invalid
188 */
189 CommandArguments
190 parse(const std::vector<std::string>& tokens, size_t start = 0) const;
191
192private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400193 static std::any
194 parseValue(ArgValueType valueType, const std::string& token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000195
196private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400197 const std::string m_noun;
198 const std::string m_verb;
Junxiao Shi6c135622016-11-21 14:30:33 +0000199 std::string m_title;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000200
201 struct Arg
202 {
203 std::string name;
204 ArgValueType valueType;
205 bool isRequired;
206 std::string metavar;
207 };
Davide Pesavento8b663a92018-11-21 22:57:20 -0500208 std::map<std::string, Arg> m_args;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000209 std::set<std::string> m_requiredArgs;
210 std::vector<std::string> m_positionalArgs;
211};
212
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400213} // namespace nfd::tools::nfdc
Junxiao Shi64567bb2016-09-04 16:00:27 +0000214
215#endif // NFD_TOOLS_NFDC_COMMAND_DEFINITION_HPP