command-definition.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
4  * 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 
31 #include <iosfwd>
32 #include <set>
33 #include <stdexcept>
34 #include <vector>
35 
36 namespace nfd::tools::nfdc {
37 
40 enum class ArgValueType {
46  NONE,
47 
53  ANY,
54 
59  BOOLEAN,
60 
66  UNSIGNED,
67 
72  STRING,
73 
79 
84  NAME,
85 
90  FACE_URI,
91 
97 
103 
108  ROUTE_ORIGIN,
109 };
110 
111 std::ostream&
112 operator<<(std::ostream& os, ArgValueType vt);
113 
116 enum class Required {
117  NO = false,
118  YES = true
119 };
120 
123 enum class Positional {
124  NO = false,
125  YES = true
126 };
127 
132 {
133 public:
134  class Error : public std::invalid_argument
135  {
136  public:
137  using std::invalid_argument::invalid_argument;
138  };
139 
140  CommandDefinition(std::string_view noun, std::string_view verb);
141 
143 
144  const std::string&
145  getNoun() const
146  {
147  return m_noun;
148  }
149 
150  const std::string&
151  getVerb() const
152  {
153  return m_verb;
154  }
155 
156 public: // help
159  const std::string&
160  getTitle() const
161  {
162  return m_title;
163  }
164 
169  setTitle(std::string_view title)
170  {
171  m_title = title;
172  return *this;
173  }
174 
175 public: // arguments
184  addArg(const std::string& name, ArgValueType valueType,
185  Required isRequired = Required::NO,
186  Positional allowPositional = Positional::NO,
187  const std::string& metavar = "");
188 
195  parse(const std::vector<std::string>& tokens, size_t start = 0) const;
196 
197 private:
198  static std::any
199  parseValue(ArgValueType valueType, const std::string& token);
200 
201 private:
202  const std::string m_noun;
203  const std::string m_verb;
204  std::string m_title;
205 
206  struct Arg
207  {
208  std::string name;
209  ArgValueType valueType;
210  bool isRequired;
211  std::string metavar;
212  };
213  std::map<std::string, Arg> m_args;
214  std::set<std::string> m_requiredArgs;
215  std::vector<std::string> m_positionalArgs;
216 };
217 
218 } // namespace nfd::tools::nfdc
219 
220 #endif // NFD_TOOLS_NFDC_COMMAND_DEFINITION_HPP
Contains named command arguments.
const std::string & getVerb() const
const std::string & getNoun() const
CommandArguments parse(const std::vector< std::string > &tokens, size_t start=0) const
Parse a command line.
CommandDefinition & setTitle(std::string_view title)
Set one-line description.
CommandDefinition & addArg(const std::string &name, ArgValueType valueType, Required isRequired=Required::NO, Positional allowPositional=Positional::NO, const std::string &metavar="")
Declare an argument.
CommandDefinition(std::string_view noun, std::string_view verb)
const std::string & getTitle() const
std::ostream & operator<<(std::ostream &os, ArgValueType vt)
Required
Indicates whether an argument is required.
@ YES
argument is required
@ NO
argument is optional
Positional
Indicates whether an argument can be specified as positional.
@ YES
argument can be specified as positional
@ NO
argument must be named
ArgValueType
Indicates argument value type.
@ FACE_ID_OR_URI
FaceId or FaceUri.
@ REPORT_FORMAT
Report format 'xml' or 'text'.
@ UNSIGNED
Non-negative integer.
@ NONE
Boolean argument without value.
@ FACE_PERSISTENCY
Face persistency 'persistent' or 'permanent'.