command-parser.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_PARSER_HPP
27 #define NFD_TOOLS_NFDC_COMMAND_PARSER_HPP
28 
29 #include "command-definition.hpp"
30 #include "execute-command.hpp"
31 #include <type_traits>
32 
33 namespace nfd {
34 namespace tools {
35 namespace nfdc {
36 
39 enum AvailableIn : uint8_t {
42  AVAILABLE_IN_BATCH = 1 << 1,
44 };
45 
46 std::ostream&
47 operator<<(std::ostream& os, AvailableIn modes);
48 
51 enum class ParseMode : uint8_t {
54 };
55 
56 std::ostream&
57 operator<<(std::ostream& os, ParseMode mode);
58 
61 class CommandParser : noncopyable
62 {
63 public:
64  class Error : public std::invalid_argument
65  {
66  public:
67  explicit
68  Error(const std::string& what)
69  : std::invalid_argument(what)
70  {
71  }
72  };
73 
80  addCommand(const CommandDefinition& def, const ExecuteCommand& execute,
81  std::underlying_type<AvailableIn>::type modes = AVAILABLE_IN_ALL);
82 
87  addAlias(const std::string& noun, const std::string& verb, const std::string& verb2);
88 
96  std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
97  parse(const std::vector<std::string>& tokens, ParseMode mode) const;
98 
99 private:
100  typedef std::pair<std::string, std::string> CommandName;
101 
102  struct Command
103  {
104  CommandDefinition def;
105  ExecuteCommand execute;
106  AvailableIn modes;
107  };
108 
111  typedef std::map<CommandName, shared_ptr<Command>> CommandContainer;
112  CommandContainer m_commands;
113 };
114 
115 } // namespace nfdc
116 } // namespace tools
117 } // namespace nfd
118 
119 #endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP
declares semantics of a command
STL namespace.
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
ParseMode
indicates which mode is the parser operated in
std::function< int(ExecuteContext &ctx)> ExecuteCommand
a function to execute a command
CommandParser & addCommand(const CommandDefinition &def, const ExecuteCommand &execute, std::underlying_type< AvailableIn >::type modes=AVAILABLE_IN_ALL)
add an available command
AvailableIn
indicates which modes is a command allowed
std::tuple< std::string, std::string, CommandArguments, ExecuteCommand > parse(const std::vector< std::string > &tokens, ParseMode mode) const
parse a command line
CommandParser & addAlias(const std::string &noun, const std::string &verb, const std::string &verb2)
add an alias "noun verb2" to existing command "noun verb"