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,
43  AVAILABLE_IN_HELP = 1 << 7,
45 };
46 
47 std::ostream&
48 operator<<(std::ostream& os, AvailableIn modes);
49 
52 enum class ParseMode : uint8_t {
55 };
56 
57 std::ostream&
58 operator<<(std::ostream& os, ParseMode mode);
59 
62 class CommandParser : noncopyable
63 {
64 public:
65  class Error : public std::invalid_argument
66  {
67  public:
68  explicit
69  Error(const std::string& what)
70  : std::invalid_argument(what)
71  {
72  }
73  };
74 
81  addCommand(const CommandDefinition& def, const ExecuteCommand& execute,
82  std::underlying_type<AvailableIn>::type modes = AVAILABLE_IN_ALL);
83 
88  addAlias(const std::string& noun, const std::string& verb, const std::string& verb2);
89 
95  std::vector<const CommandDefinition*>
96  listCommands(const std::string& noun, ParseMode mode) const;
97 
105  std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
106  parse(const std::vector<std::string>& tokens, ParseMode mode) const;
107 
108 private:
109  typedef std::pair<std::string, std::string> CommandName;
110 
111  struct Command
112  {
113  CommandDefinition def;
114  ExecuteCommand execute;
115  AvailableIn modes;
116  };
117 
120  typedef std::map<CommandName, shared_ptr<Command>> CommandContainer;
121  CommandContainer m_commands;
122 
125  std::vector<CommandContainer::const_iterator> m_commandOrder;
126 };
127 
128 } // namespace nfdc
129 } // namespace tools
130 } // namespace nfd
131 
132 #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...
std::vector< const CommandDefinition * > listCommands(const std::string &noun, ParseMode mode) const
list known commands for help
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"