6c13562d23
refs #3780 Change-Id: Ibd0e37bad127a38e582864fadd81afadc74baf48
133 lines
4.3 KiB
C++
133 lines
4.3 KiB
C++
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
/**
|
|
* Copyright (c) 2014-2016, Regents of the University of California,
|
|
* Arizona Board of Regents,
|
|
* Colorado State University,
|
|
* University Pierre & Marie Curie, Sorbonne University,
|
|
* Washington University in St. Louis,
|
|
* Beijing Institute of Technology,
|
|
* The University of Memphis.
|
|
*
|
|
* This file is part of NFD (Named Data Networking Forwarding Daemon).
|
|
* See AUTHORS.md for complete list of NFD authors and contributors.
|
|
*
|
|
* NFD is free software: you can redistribute it and/or modify it under the terms
|
|
* of the GNU General Public License as published by the Free Software Foundation,
|
|
* either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef NFD_TOOLS_NFDC_COMMAND_PARSER_HPP
|
|
#define NFD_TOOLS_NFDC_COMMAND_PARSER_HPP
|
|
|
|
#include "command-definition.hpp"
|
|
#include "execute-command.hpp"
|
|
#include <type_traits>
|
|
|
|
namespace nfd {
|
|
namespace tools {
|
|
namespace nfdc {
|
|
|
|
/** \brief indicates which modes is a command allowed
|
|
*/
|
|
enum AvailableIn : uint8_t {
|
|
AVAILABLE_IN_NONE = 0,
|
|
AVAILABLE_IN_ONE_SHOT = 1 << 0, ///< one-shot mode
|
|
AVAILABLE_IN_BATCH = 1 << 1, ///< batch mode
|
|
AVAILABLE_IN_HELP = 1 << 7, ///< visible in help listing
|
|
AVAILABLE_IN_ALL = 0xff
|
|
};
|
|
|
|
std::ostream&
|
|
operator<<(std::ostream& os, AvailableIn modes);
|
|
|
|
/** \brief indicates which mode is the parser operated in
|
|
*/
|
|
enum class ParseMode : uint8_t {
|
|
ONE_SHOT = AVAILABLE_IN_ONE_SHOT, ///< one-shot mode
|
|
BATCH = AVAILABLE_IN_BATCH ///< batch mode
|
|
};
|
|
|
|
std::ostream&
|
|
operator<<(std::ostream& os, ParseMode mode);
|
|
|
|
/** \brief parses a command
|
|
*/
|
|
class CommandParser : noncopyable
|
|
{
|
|
public:
|
|
class Error : public std::invalid_argument
|
|
{
|
|
public:
|
|
explicit
|
|
Error(const std::string& what)
|
|
: std::invalid_argument(what)
|
|
{
|
|
}
|
|
};
|
|
|
|
/** \brief add an available command
|
|
* \param def command semantics definition
|
|
* \param execute a function to execute the command
|
|
* \param modes parse modes this command should be available in, must not be AVAILABLE_IN_NONE
|
|
*/
|
|
CommandParser&
|
|
addCommand(const CommandDefinition& def, const ExecuteCommand& execute,
|
|
std::underlying_type<AvailableIn>::type modes = AVAILABLE_IN_ALL);
|
|
|
|
/** \brief add an alias "noun verb2" to existing command "noun verb"
|
|
* \throw std::out_of_range "noun verb" does not exist
|
|
*/
|
|
CommandParser&
|
|
addAlias(const std::string& noun, const std::string& verb, const std::string& verb2);
|
|
|
|
/** \brief list known commands for help
|
|
* \param noun if not empty, filter results by this noun
|
|
* \param mode include commands for the specified parse mode
|
|
* \return commands in insertion order
|
|
*/
|
|
std::vector<const CommandDefinition*>
|
|
listCommands(const std::string& noun, ParseMode mode) const;
|
|
|
|
/** \brief parse a command line
|
|
* \param tokens command line
|
|
* \param mode parser mode, must be ParseMode::ONE_SHOT, other modes are not implemented
|
|
* \throw Error command is not found
|
|
* \throw CommandDefinition::Error command arguments are invalid
|
|
* \return noun, verb, arguments, execute function
|
|
*/
|
|
std::tuple<std::string, std::string, CommandArguments, ExecuteCommand>
|
|
parse(const std::vector<std::string>& tokens, ParseMode mode) const;
|
|
|
|
private:
|
|
typedef std::pair<std::string, std::string> CommandName;
|
|
|
|
struct Command
|
|
{
|
|
CommandDefinition def;
|
|
ExecuteCommand execute;
|
|
AvailableIn modes;
|
|
};
|
|
|
|
/** \brief map from command name or alias to command definition
|
|
*/
|
|
typedef std::map<CommandName, shared_ptr<Command>> CommandContainer;
|
|
CommandContainer m_commands;
|
|
|
|
/** \brief commands in insertion order
|
|
*/
|
|
std::vector<CommandContainer::const_iterator> m_commandOrder;
|
|
};
|
|
|
|
} // namespace nfdc
|
|
} // namespace tools
|
|
} // namespace nfd
|
|
|
|
#endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP
|