Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 8eda682 | 2017-04-12 02:53:14 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 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 | |
| 35 | /** \brief indicates argument value type |
| 36 | */ |
| 37 | enum class ArgValueType { |
| 38 | /** \brief boolean argument without value |
| 39 | * |
| 40 | * The argument appears in CommandArguments as bool value 'true'. |
| 41 | * It must not be declared as positional. |
| 42 | */ |
| 43 | NONE, |
| 44 | |
| 45 | /** \brief any arguments |
| 46 | * |
| 47 | * The argument appears in CommandArguments as std::vector<std::string>. |
| 48 | * It must be declared as positional, and will consume all subsequent tokens. |
| 49 | */ |
| 50 | ANY, |
| 51 | |
| 52 | /** \brief non-negative integer |
| 53 | * |
| 54 | * The argument appears in CommandArguments as uint64_t. |
| 55 | * Acceptable input range is [0, std::numeric_limits<int64_t>::max()]. |
| 56 | */ |
| 57 | UNSIGNED, |
| 58 | |
| 59 | /** \brief arbitrary string |
| 60 | * |
| 61 | * The argument appears in CommandArguments as std::string. |
| 62 | */ |
| 63 | STRING, |
| 64 | |
| 65 | /** \brief report format 'xml' or 'text' |
| 66 | * |
| 67 | * The argument appears in CommandArguments as nfd::tools::nfdc::ReportFormat. |
| 68 | */ |
| 69 | REPORT_FORMAT, |
| 70 | |
| 71 | /** \brief Name prefix |
| 72 | * |
| 73 | * The argument appears in CommandArguments as ndn::Name. |
| 74 | */ |
| 75 | NAME, |
| 76 | |
| 77 | /** \brief FaceUri |
| 78 | * |
| 79 | * The argument appears in CommandArguments as ndn::util::FaceUri. |
| 80 | */ |
| 81 | FACE_URI, |
| 82 | |
| 83 | /** \brief FaceId or FaceUri |
| 84 | * |
| 85 | * The argument appears in CommandArguments as either uint64_t or ndn::util::FaceUri. |
| 86 | */ |
| 87 | FACE_ID_OR_URI, |
| 88 | |
| 89 | /** \brief face persistency 'persistent' or 'permanent' |
| 90 | * |
| 91 | * The argument appears in CommandArguments as ndn::nfd::FacePersistency. |
| 92 | */ |
Junxiao Shi | 8eda682 | 2017-04-12 02:53:14 +0000 | [diff] [blame] | 93 | FACE_PERSISTENCY, |
| 94 | |
| 95 | /** \brief route origin |
| 96 | * |
| 97 | * The argument appears in CommandArguments as ndn::nfd::RouteOrigin. |
| 98 | */ |
| 99 | ROUTE_ORIGIN, |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | std::ostream& |
| 103 | operator<<(std::ostream& os, ArgValueType vt); |
| 104 | |
| 105 | /** \brief indicates whether an argument is required |
| 106 | */ |
| 107 | enum class Required { |
| 108 | NO = false, ///< argument is optional |
| 109 | YES = true ///< argument is required |
| 110 | }; |
| 111 | |
| 112 | /** \brief indicates whether an argument can be specified as positional |
| 113 | */ |
| 114 | enum class Positional { |
| 115 | NO = false, ///< argument must be named |
| 116 | YES = true ///< argument can be specified as positional |
| 117 | }; |
| 118 | |
| 119 | /** \brief declares semantics of a command |
| 120 | */ |
| 121 | class CommandDefinition |
| 122 | { |
| 123 | public: |
| 124 | class Error : public std::invalid_argument |
| 125 | { |
| 126 | public: |
| 127 | explicit |
| 128 | Error(const std::string& what) |
| 129 | : std::invalid_argument(what) |
| 130 | { |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | CommandDefinition(const std::string& noun, const std::string& verb); |
| 135 | |
| 136 | ~CommandDefinition(); |
| 137 | |
| 138 | const std::string |
| 139 | getNoun() const |
| 140 | { |
| 141 | return m_noun; |
| 142 | } |
| 143 | |
| 144 | const std::string |
| 145 | getVerb() const |
| 146 | { |
| 147 | return m_verb; |
| 148 | } |
| 149 | |
| 150 | public: // help |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 151 | /** \return one-line description |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 152 | */ |
| 153 | const std::string& |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 154 | getTitle() const |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 155 | { |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 156 | return m_title; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 159 | /** \brief set one-line description |
| 160 | * \param title one-line description, written in lower case |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 161 | */ |
| 162 | CommandDefinition& |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 163 | setTitle(const std::string& title) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 164 | { |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 165 | m_title = title; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 166 | return *this; |
| 167 | } |
| 168 | |
| 169 | public: // arguments |
| 170 | /** \brief declare an argument |
| 171 | * \param name argument name, must be unique |
| 172 | * \param valueType argument value type |
| 173 | * \param isRequired whether the argument is required |
| 174 | * \param allowPositional whether the argument value can be specified as positional |
| 175 | * \param metavar displayed argument value placeholder |
| 176 | */ |
| 177 | CommandDefinition& |
| 178 | addArg(const std::string& name, ArgValueType valueType, |
| 179 | Required isRequired = Required::NO, |
| 180 | Positional allowPositional = Positional::NO, |
| 181 | const std::string& metavar = ""); |
| 182 | |
| 183 | /** \brief parse a command line |
| 184 | * \param tokens command line tokens |
| 185 | * \param start command line start position, after noun and verb |
| 186 | * \throw Error command line is invalid |
| 187 | */ |
| 188 | CommandArguments |
| 189 | parse(const std::vector<std::string>& tokens, size_t start = 0) const; |
| 190 | |
| 191 | private: |
| 192 | boost::any |
| 193 | parseValue(ArgValueType valueType, const std::string& token) const; |
| 194 | |
| 195 | private: |
| 196 | std::string m_noun; |
| 197 | std::string m_verb; |
| 198 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 199 | std::string m_title; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 200 | |
| 201 | struct Arg |
| 202 | { |
| 203 | std::string name; |
| 204 | ArgValueType valueType; |
| 205 | bool isRequired; |
| 206 | std::string metavar; |
| 207 | }; |
| 208 | typedef std::map<std::string, Arg> ArgMap; |
| 209 | ArgMap m_args; |
| 210 | std::set<std::string> m_requiredArgs; |
| 211 | std::vector<std::string> m_positionalArgs; |
| 212 | }; |
| 213 | |
| 214 | } // namespace nfdc |
| 215 | } // namespace tools |
| 216 | } // namespace nfd |
| 217 | |
| 218 | #endif // NFD_TOOLS_NFDC_COMMAND_DEFINITION_HPP |