blob: 914c9b59c84122fdd17715a313a2514ea02a7e6a [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 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_DEFINITION_HPP
27#define NFD_TOOLS_NFDC_COMMAND_DEFINITION_HPP
28
29#include "command-arguments.hpp"
30
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050031#include <iosfwd>
32#include <set>
33#include <stdexcept>
34#include <vector>
35
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::tools::nfdc {
Junxiao Shi64567bb2016-09-04 16:00:27 +000037
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040038/** \brief Indicates argument value type.
Junxiao Shi64567bb2016-09-04 16:00:27 +000039 */
40enum class ArgValueType {
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040041 /** \brief Boolean argument without value.
Junxiao Shi64567bb2016-09-04 16:00:27 +000042 *
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040043 * The argument appears in CommandArguments as bool value `true`.
Junxiao Shi64567bb2016-09-04 16:00:27 +000044 * It must not be declared as positional.
45 */
46 NONE,
47
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040048 /** \brief Any arguments.
Junxiao Shi64567bb2016-09-04 16:00:27 +000049 *
50 * The argument appears in CommandArguments as std::vector<std::string>.
51 * It must be declared as positional, and will consume all subsequent tokens.
52 */
53 ANY,
54
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040055 /** \brief Boolean.
Eric Newberry84d3adc2017-08-09 23:31:40 -040056 *
57 * The argument appears in CommandArguments as bool.
58 */
59 BOOLEAN,
60
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040061 /** \brief Non-negative integer.
Junxiao Shi64567bb2016-09-04 16:00:27 +000062 *
63 * The argument appears in CommandArguments as uint64_t.
64 * Acceptable input range is [0, std::numeric_limits<int64_t>::max()].
65 */
66 UNSIGNED,
67
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040068 /** \brief Arbitrary string.
Junxiao Shi64567bb2016-09-04 16:00:27 +000069 *
70 * The argument appears in CommandArguments as std::string.
71 */
72 STRING,
73
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040074 /** \brief Report format 'xml' or 'text'.
Junxiao Shi64567bb2016-09-04 16:00:27 +000075 *
76 * The argument appears in CommandArguments as nfd::tools::nfdc::ReportFormat.
77 */
78 REPORT_FORMAT,
79
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040080 /** \brief Name prefix.
Junxiao Shi64567bb2016-09-04 16:00:27 +000081 *
82 * The argument appears in CommandArguments as ndn::Name.
83 */
84 NAME,
85
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040086 /** \brief FaceUri.
Junxiao Shi64567bb2016-09-04 16:00:27 +000087 *
Junxiao Shi83be1da2017-06-30 13:37:37 +000088 * The argument appears in CommandArguments as ndn::FaceUri.
Junxiao Shi64567bb2016-09-04 16:00:27 +000089 */
90 FACE_URI,
91
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040092 /** \brief FaceId or FaceUri.
Junxiao Shi64567bb2016-09-04 16:00:27 +000093 *
Junxiao Shi83be1da2017-06-30 13:37:37 +000094 * The argument appears in CommandArguments as either uint64_t or ndn::FaceUri.
Junxiao Shi64567bb2016-09-04 16:00:27 +000095 */
96 FACE_ID_OR_URI,
97
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040098 /** \brief Face persistency 'persistent' or 'permanent'.
Junxiao Shi64567bb2016-09-04 16:00:27 +000099 *
100 * The argument appears in CommandArguments as ndn::nfd::FacePersistency.
101 */
Junxiao Shi8eda6822017-04-12 02:53:14 +0000102 FACE_PERSISTENCY,
103
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400104 /** \brief Route origin.
Junxiao Shi8eda6822017-04-12 02:53:14 +0000105 *
106 * The argument appears in CommandArguments as ndn::nfd::RouteOrigin.
107 */
108 ROUTE_ORIGIN,
Junxiao Shi64567bb2016-09-04 16:00:27 +0000109};
110
111std::ostream&
112operator<<(std::ostream& os, ArgValueType vt);
113
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400114/** \brief Indicates whether an argument is required.
Junxiao Shi64567bb2016-09-04 16:00:27 +0000115 */
116enum class Required {
117 NO = false, ///< argument is optional
118 YES = true ///< argument is required
119};
120
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400121/** \brief Indicates whether an argument can be specified as positional.
Junxiao Shi64567bb2016-09-04 16:00:27 +0000122 */
123enum class Positional {
124 NO = false, ///< argument must be named
125 YES = true ///< argument can be specified as positional
126};
127
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400128/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400129 * \brief Defines a command.
Junxiao Shi64567bb2016-09-04 16:00:27 +0000130 */
131class CommandDefinition
132{
133public:
134 class Error : public std::invalid_argument
135 {
136 public:
Davide Pesavento8b663a92018-11-21 22:57:20 -0500137 using std::invalid_argument::invalid_argument;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000138 };
139
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400140 CommandDefinition(std::string_view noun, std::string_view verb);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000141
142 ~CommandDefinition();
143
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400144 const std::string&
Junxiao Shi64567bb2016-09-04 16:00:27 +0000145 getNoun() const
146 {
147 return m_noun;
148 }
149
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400150 const std::string&
Junxiao Shi64567bb2016-09-04 16:00:27 +0000151 getVerb() const
152 {
153 return m_verb;
154 }
155
156public: // help
Junxiao Shi6c135622016-11-21 14:30:33 +0000157 /** \return one-line description
Junxiao Shi64567bb2016-09-04 16:00:27 +0000158 */
159 const std::string&
Junxiao Shi6c135622016-11-21 14:30:33 +0000160 getTitle() const
Junxiao Shi64567bb2016-09-04 16:00:27 +0000161 {
Junxiao Shi6c135622016-11-21 14:30:33 +0000162 return m_title;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000163 }
164
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400165 /** \brief Set one-line description.
Junxiao Shi6c135622016-11-21 14:30:33 +0000166 * \param title one-line description, written in lower case
Junxiao Shi64567bb2016-09-04 16:00:27 +0000167 */
168 CommandDefinition&
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400169 setTitle(std::string_view title)
Junxiao Shi64567bb2016-09-04 16:00:27 +0000170 {
Junxiao Shi6c135622016-11-21 14:30:33 +0000171 m_title = title;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000172 return *this;
173 }
174
175public: // arguments
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400176 /** \brief Declare an argument.
Junxiao Shi64567bb2016-09-04 16:00:27 +0000177 * \param name argument name, must be unique
178 * \param valueType argument value type
179 * \param isRequired whether the argument is required
180 * \param allowPositional whether the argument value can be specified as positional
181 * \param metavar displayed argument value placeholder
182 */
183 CommandDefinition&
184 addArg(const std::string& name, ArgValueType valueType,
185 Required isRequired = Required::NO,
186 Positional allowPositional = Positional::NO,
187 const std::string& metavar = "");
188
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400189 /** \brief Parse a command line.
Junxiao Shi64567bb2016-09-04 16:00:27 +0000190 * \param tokens command line tokens
191 * \param start command line start position, after noun and verb
192 * \throw Error command line is invalid
193 */
194 CommandArguments
195 parse(const std::vector<std::string>& tokens, size_t start = 0) const;
196
197private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400198 static std::any
199 parseValue(ArgValueType valueType, const std::string& token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000200
201private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400202 const std::string m_noun;
203 const std::string m_verb;
Junxiao Shi6c135622016-11-21 14:30:33 +0000204 std::string m_title;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000205
206 struct Arg
207 {
208 std::string name;
209 ArgValueType valueType;
210 bool isRequired;
211 std::string metavar;
212 };
Davide Pesavento8b663a92018-11-21 22:57:20 -0500213 std::map<std::string, Arg> m_args;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000214 std::set<std::string> m_requiredArgs;
215 std::vector<std::string> m_positionalArgs;
216};
217
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400218} // namespace nfd::tools::nfdc
Junxiao Shi64567bb2016-09-04 16:00:27 +0000219
220#endif // NFD_TOOLS_NFDC_COMMAND_DEFINITION_HPP