command-definition.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
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 
57  UNSIGNED,
58 
63  STRING,
64 
70 
75  NAME,
76 
81  FACE_URI,
82 
88 
94 };
95 
96 std::ostream&
97 operator<<(std::ostream& os, ArgValueType vt);
98 
101 enum class Required {
102  NO = false,
103  YES = true
104 };
105 
108 enum class Positional {
109  NO = false,
110  YES = true
111 };
112 
116 {
117 public:
118  class Error : public std::invalid_argument
119  {
120  public:
121  explicit
122  Error(const std::string& what)
123  : std::invalid_argument(what)
124  {
125  }
126  };
127 
128  CommandDefinition(const std::string& noun, const std::string& verb);
129 
131 
132  const std::string
133  getNoun() const
134  {
135  return m_noun;
136  }
137 
138  const std::string
139  getVerb() const
140  {
141  return m_verb;
142  }
143 
144 public: // help
147  const std::string&
148  getTitle() const
149  {
150  return m_title;
151  }
152 
157  setTitle(const std::string& title)
158  {
159  m_title = title;
160  return *this;
161  }
162 
163 public: // arguments
172  addArg(const std::string& name, ArgValueType valueType,
173  Required isRequired = Required::NO,
174  Positional allowPositional = Positional::NO,
175  const std::string& metavar = "");
176 
183  parse(const std::vector<std::string>& tokens, size_t start = 0) const;
184 
185 private:
186  boost::any
187  parseValue(ArgValueType valueType, const std::string& token) const;
188 
189 private:
190  std::string m_noun;
191  std::string m_verb;
192 
193  std::string m_title;
194 
195  struct Arg
196  {
197  std::string name;
198  ArgValueType valueType;
199  bool isRequired;
200  std::string metavar;
201  };
202  typedef std::map<std::string, Arg> ArgMap;
203  ArgMap m_args;
204  std::set<std::string> m_requiredArgs;
205  std::vector<std::string> m_positionalArgs;
206 };
207 
208 } // namespace nfdc
209 } // namespace tools
210 } // namespace nfd
211 
212 #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
CommandDefinition(const std::string &noun, const std::string &verb)
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...
contains named command arguments
face persistency 'persistent' or 'permanent'
CommandDefinition & addArg(const std::string &name, ArgValueType valueType, Required isRequired=Required::NO, Positional allowPositional=Positional::NO, const std::string &metavar="")
declare an argument
argument is required
argument is optional
CommandArguments parse(const std::vector< std::string > &tokens, size_t start=0) const
parse a command line
report format 'xml' or 'text'
Positional
indicates whether an argument can be specified as positional