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  getSynopsis() const
149  {
150  return m_synopsis;
151  }
152 
156  setSynopsis(const std::string& synopsis)
157  {
158  m_synopsis = synopsis;
159  return *this;
160  }
161 
162 public: // arguments
171  addArg(const std::string& name, ArgValueType valueType,
172  Required isRequired = Required::NO,
173  Positional allowPositional = Positional::NO,
174  const std::string& metavar = "");
175 
182  parse(const std::vector<std::string>& tokens, size_t start = 0) const;
183 
184 private:
185  boost::any
186  parseValue(ArgValueType valueType, const std::string& token) const;
187 
188 private:
189  std::string m_noun;
190  std::string m_verb;
191 
192  std::string m_synopsis;
193 
194  struct Arg
195  {
196  std::string name;
197  ArgValueType valueType;
198  bool isRequired;
199  std::string metavar;
200  };
201  typedef std::map<std::string, Arg> ArgMap;
202  ArgMap m_args;
203  std::set<std::string> m_requiredArgs;
204  std::vector<std::string> m_positionalArgs;
205 };
206 
207 } // namespace nfdc
208 } // namespace tools
209 } // namespace nfd
210 
211 #endif // NFD_TOOLS_NFDC_COMMAND_DEFINITION_HPP
boolean argument without value
Required
indicates whether an argument is required
declares semantics of a command
const std::string & getSynopsis() const
CommandDefinition & setSynopsis(const std::string &synopsis)
set one-line synopsis
STL namespace.
ArgValueType
indicates argument value type
CommandDefinition(const std::string &noun, const std::string &verb)
std::ostream & operator<<(std::ostream &os, ArgValueType vt)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
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