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-2022, 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 namespace nfd::tools::nfdc {
32 
35 enum class ArgValueType {
41  NONE,
42 
48  ANY,
49 
54  BOOLEAN,
55 
61  UNSIGNED,
62 
67  STRING,
68 
74 
79  NAME,
80 
85  FACE_URI,
86 
92 
98 
103  ROUTE_ORIGIN,
104 };
105 
106 std::ostream&
107 operator<<(std::ostream& os, ArgValueType vt);
108 
111 enum class Required {
112  NO = false,
113  YES = true
114 };
115 
118 enum class Positional {
119  NO = false,
120  YES = true
121 };
122 
127 {
128 public:
129  class Error : public std::invalid_argument
130  {
131  public:
132  using std::invalid_argument::invalid_argument;
133  };
134 
135  CommandDefinition(std::string_view noun, std::string_view verb);
136 
138 
139  const std::string&
140  getNoun() const
141  {
142  return m_noun;
143  }
144 
145  const std::string&
146  getVerb() const
147  {
148  return m_verb;
149  }
150 
151 public: // help
154  const std::string&
155  getTitle() const
156  {
157  return m_title;
158  }
159 
164  setTitle(std::string_view title)
165  {
166  m_title = title;
167  return *this;
168  }
169 
170 public: // arguments
179  addArg(const std::string& name, ArgValueType valueType,
180  Required isRequired = Required::NO,
181  Positional allowPositional = Positional::NO,
182  const std::string& metavar = "");
183 
190  parse(const std::vector<std::string>& tokens, size_t start = 0) const;
191 
192 private:
193  static std::any
194  parseValue(ArgValueType valueType, const std::string& token);
195 
196 private:
197  const std::string m_noun;
198  const std::string m_verb;
199  std::string m_title;
200 
201  struct Arg
202  {
203  std::string name;
204  ArgValueType valueType;
205  bool isRequired;
206  std::string metavar;
207  };
208  std::map<std::string, Arg> m_args;
209  std::set<std::string> m_requiredArgs;
210  std::vector<std::string> m_positionalArgs;
211 };
212 
213 } // namespace nfd::tools::nfdc
214 
215 #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'.