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-2017, 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 {
32 namespace tools {
33 namespace nfdc {
34 
37 enum class ArgValueType {
43  NONE,
44 
50  ANY,
51 
56  BOOLEAN,
57 
63  UNSIGNED,
64 
69  STRING,
70 
76 
81  NAME,
82 
87  FACE_URI,
88 
94 
100 
105  ROUTE_ORIGIN,
106 };
107 
108 std::ostream&
109 operator<<(std::ostream& os, ArgValueType vt);
110 
113 enum class Required {
114  NO = false,
115  YES = true
116 };
117 
120 enum class Positional {
121  NO = false,
122  YES = true
123 };
124 
128 {
129 public:
130  class Error : public std::invalid_argument
131  {
132  public:
133  explicit
134  Error(const std::string& what)
135  : std::invalid_argument(what)
136  {
137  }
138  };
139 
140  CommandDefinition(const std::string& noun, const std::string& 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(const std::string& 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  boost::any
199  parseValue(ArgValueType valueType, const std::string& token) const;
200 
201 private:
202  std::string m_noun;
203  std::string m_verb;
204 
205  std::string m_title;
206 
207  struct Arg
208  {
209  std::string name;
210  ArgValueType valueType;
211  bool isRequired;
212  std::string metavar;
213  };
214  typedef std::map<std::string, Arg> ArgMap;
215  ArgMap m_args;
216  std::set<std::string> m_requiredArgs;
217  std::vector<std::string> m_positionalArgs;
218 };
219 
220 } // namespace nfdc
221 } // namespace tools
222 } // namespace nfd
223 
224 #endif // NFD_TOOLS_NFDC_COMMAND_DEFINITION_HPP
boolean argument without value
Required
indicates whether an argument is required
declares semantics of a command
STL namespace.
ArgValueType
indicates argument value type
std::ostream & operator<<(std::ostream &os, ArgValueType vt)
CommandDefinition & setTitle(const std::string &title)
set one-line description
const std::string & getTitle() const
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
contains named command arguments
face persistency &#39;persistent&#39; or &#39;permanent&#39;
argument is required
argument is optional
report format &#39;xml&#39; or &#39;text&#39;
Positional
indicates whether an argument can be specified as positional