blob: 26e309bf39c840f1c981c68fb6d9f1adf5b5819d [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
33/** \brief indicates argument value type
34 */
35enum class ArgValueType {
36 /** \brief boolean argument without value
37 *
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
43 /** \brief any arguments
44 *
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
Eric Newberry84d3adc2017-08-09 23:31:40 -040050 /** \brief boolean
51 *
52 * The argument appears in CommandArguments as bool.
53 */
54 BOOLEAN,
55
Junxiao Shi64567bb2016-09-04 16:00:27 +000056 /** \brief non-negative integer
57 *
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
63 /** \brief arbitrary string
64 *
65 * The argument appears in CommandArguments as std::string.
66 */
67 STRING,
68
69 /** \brief report format 'xml' or 'text'
70 *
71 * The argument appears in CommandArguments as nfd::tools::nfdc::ReportFormat.
72 */
73 REPORT_FORMAT,
74
75 /** \brief Name prefix
76 *
77 * The argument appears in CommandArguments as ndn::Name.
78 */
79 NAME,
80
81 /** \brief FaceUri
82 *
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
87 /** \brief FaceId or FaceUri
88 *
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
93 /** \brief face persistency 'persistent' or 'permanent'
94 *
95 * The argument appears in CommandArguments as ndn::nfd::FacePersistency.
96 */
Junxiao Shi8eda6822017-04-12 02:53:14 +000097 FACE_PERSISTENCY,
98
99 /** \brief route origin
100 *
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
109/** \brief indicates whether an argument is required
110 */
111enum class Required {
112 NO = false, ///< argument is optional
113 YES = true ///< argument is required
114};
115
116/** \brief indicates whether an argument can be specified as positional
117 */
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/**
124 * \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
Junxiao Shi6c135622016-11-21 14:30:33 +0000160 /** \brief set one-line description
161 * \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
171 /** \brief declare an argument
172 * \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
184 /** \brief parse a command line
185 * \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