blob: 8cd56d9a027553219662fe82371425d12704e75d [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
31namespace nfd {
32namespace tools {
33namespace nfdc {
34
35/** \brief indicates argument value type
36 */
37enum class ArgValueType {
38 /** \brief boolean argument without value
39 *
40 * The argument appears in CommandArguments as bool value 'true'.
41 * It must not be declared as positional.
42 */
43 NONE,
44
45 /** \brief any arguments
46 *
47 * The argument appears in CommandArguments as std::vector<std::string>.
48 * It must be declared as positional, and will consume all subsequent tokens.
49 */
50 ANY,
51
Eric Newberry84d3adc2017-08-09 23:31:40 -040052 /** \brief boolean
53 *
54 * The argument appears in CommandArguments as bool.
55 */
56 BOOLEAN,
57
Junxiao Shi64567bb2016-09-04 16:00:27 +000058 /** \brief non-negative integer
59 *
60 * The argument appears in CommandArguments as uint64_t.
61 * Acceptable input range is [0, std::numeric_limits<int64_t>::max()].
62 */
63 UNSIGNED,
64
65 /** \brief arbitrary string
66 *
67 * The argument appears in CommandArguments as std::string.
68 */
69 STRING,
70
71 /** \brief report format 'xml' or 'text'
72 *
73 * The argument appears in CommandArguments as nfd::tools::nfdc::ReportFormat.
74 */
75 REPORT_FORMAT,
76
77 /** \brief Name prefix
78 *
79 * The argument appears in CommandArguments as ndn::Name.
80 */
81 NAME,
82
83 /** \brief FaceUri
84 *
Junxiao Shi83be1da2017-06-30 13:37:37 +000085 * The argument appears in CommandArguments as ndn::FaceUri.
Junxiao Shi64567bb2016-09-04 16:00:27 +000086 */
87 FACE_URI,
88
89 /** \brief FaceId or FaceUri
90 *
Junxiao Shi83be1da2017-06-30 13:37:37 +000091 * The argument appears in CommandArguments as either uint64_t or ndn::FaceUri.
Junxiao Shi64567bb2016-09-04 16:00:27 +000092 */
93 FACE_ID_OR_URI,
94
95 /** \brief face persistency 'persistent' or 'permanent'
96 *
97 * The argument appears in CommandArguments as ndn::nfd::FacePersistency.
98 */
Junxiao Shi8eda6822017-04-12 02:53:14 +000099 FACE_PERSISTENCY,
100
101 /** \brief route origin
102 *
103 * The argument appears in CommandArguments as ndn::nfd::RouteOrigin.
104 */
105 ROUTE_ORIGIN,
Junxiao Shi64567bb2016-09-04 16:00:27 +0000106};
107
108std::ostream&
109operator<<(std::ostream& os, ArgValueType vt);
110
111/** \brief indicates whether an argument is required
112 */
113enum class Required {
114 NO = false, ///< argument is optional
115 YES = true ///< argument is required
116};
117
118/** \brief indicates whether an argument can be specified as positional
119 */
120enum class Positional {
121 NO = false, ///< argument must be named
122 YES = true ///< argument can be specified as positional
123};
124
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400125/**
126 * \brief Defines a command
Junxiao Shi64567bb2016-09-04 16:00:27 +0000127 */
128class CommandDefinition
129{
130public:
131 class Error : public std::invalid_argument
132 {
133 public:
Davide Pesavento8b663a92018-11-21 22:57:20 -0500134 using std::invalid_argument::invalid_argument;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000135 };
136
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400137 CommandDefinition(std::string_view noun, std::string_view verb);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000138
139 ~CommandDefinition();
140
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400141 const std::string&
Junxiao Shi64567bb2016-09-04 16:00:27 +0000142 getNoun() const
143 {
144 return m_noun;
145 }
146
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400147 const std::string&
Junxiao Shi64567bb2016-09-04 16:00:27 +0000148 getVerb() const
149 {
150 return m_verb;
151 }
152
153public: // help
Junxiao Shi6c135622016-11-21 14:30:33 +0000154 /** \return one-line description
Junxiao Shi64567bb2016-09-04 16:00:27 +0000155 */
156 const std::string&
Junxiao Shi6c135622016-11-21 14:30:33 +0000157 getTitle() const
Junxiao Shi64567bb2016-09-04 16:00:27 +0000158 {
Junxiao Shi6c135622016-11-21 14:30:33 +0000159 return m_title;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000160 }
161
Junxiao Shi6c135622016-11-21 14:30:33 +0000162 /** \brief set one-line description
163 * \param title one-line description, written in lower case
Junxiao Shi64567bb2016-09-04 16:00:27 +0000164 */
165 CommandDefinition&
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400166 setTitle(std::string_view title)
Junxiao Shi64567bb2016-09-04 16:00:27 +0000167 {
Junxiao Shi6c135622016-11-21 14:30:33 +0000168 m_title = title;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000169 return *this;
170 }
171
172public: // arguments
173 /** \brief declare an argument
174 * \param name argument name, must be unique
175 * \param valueType argument value type
176 * \param isRequired whether the argument is required
177 * \param allowPositional whether the argument value can be specified as positional
178 * \param metavar displayed argument value placeholder
179 */
180 CommandDefinition&
181 addArg(const std::string& name, ArgValueType valueType,
182 Required isRequired = Required::NO,
183 Positional allowPositional = Positional::NO,
184 const std::string& metavar = "");
185
186 /** \brief parse a command line
187 * \param tokens command line tokens
188 * \param start command line start position, after noun and verb
189 * \throw Error command line is invalid
190 */
191 CommandArguments
192 parse(const std::vector<std::string>& tokens, size_t start = 0) const;
193
194private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400195 static std::any
196 parseValue(ArgValueType valueType, const std::string& token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000197
198private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400199 const std::string m_noun;
200 const std::string m_verb;
Junxiao Shi6c135622016-11-21 14:30:33 +0000201 std::string m_title;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000202
203 struct Arg
204 {
205 std::string name;
206 ArgValueType valueType;
207 bool isRequired;
208 std::string metavar;
209 };
Davide Pesavento8b663a92018-11-21 22:57:20 -0500210 std::map<std::string, Arg> m_args;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000211 std::set<std::string> m_requiredArgs;
212 std::vector<std::string> m_positionalArgs;
213};
214
215} // namespace nfdc
216} // namespace tools
217} // namespace nfd
218
219#endif // NFD_TOOLS_NFDC_COMMAND_DEFINITION_HPP