| 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- |
| 2 | |
| 3 | /****************************************************************************** |
| 4 | * |
| 5 | * file: UnlabeledValueArg.h |
| 6 | * |
| 7 | * Copyright (c) 2003, Michael E. Smoot . |
| 8 | * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno. |
| 9 | * Copyright (c) 2017, Google LLC |
| 10 | * All rights reserved. |
| 11 | * |
| 12 | * See the file COPYING in the top directory of this distribution for |
| 13 | * more information. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | * |
| 23 | *****************************************************************************/ |
| 24 | |
| 25 | #ifndef TCLAP_UNLABELED_VALUE_ARG_H |
| 26 | #define TCLAP_UNLABELED_VALUE_ARG_H |
| 27 | |
| 28 | #include <tclap/OptionalUnlabeledTracker.h> |
| 29 | #include <tclap/ValueArg.h> |
| 30 | |
| 31 | #include <list> |
| 32 | #include <string> |
| 33 | #include <vector> |
| 34 | |
| 35 | namespace TCLAP { |
| 36 | |
| 37 | /** |
| 38 | * The basic unlabeled argument that parses a value. |
| 39 | * This is a template class, which means the type T defines the type |
| 40 | * that a given object will attempt to parse when an UnlabeledValueArg |
| 41 | * is reached in the list of args that the CmdLine iterates over. |
| 42 | */ |
| 43 | template <class T> |
| 44 | class UnlabeledValueArg : public ValueArg<T> { |
| 45 | // If compiler has two stage name lookup (as gcc >= 3.4 does) |
| 46 | // this is required to prevent undef. symbols |
| 47 | using ValueArg<T>::_ignoreable; |
| 48 | using ValueArg<T>::_hasBlanks; |
| 49 | using ValueArg<T>::_extractValue; |
| 50 | using ValueArg<T>::_typeDesc; |
| 51 | using ValueArg<T>::_name; |
| 52 | using ValueArg<T>::_description; |
| 53 | using ValueArg<T>::_alreadySet; |
| 54 | using ValueArg<T>::_setBy; |
| 55 | using ValueArg<T>::toString; |
| 56 | |
| 57 | public: |
| 58 | /** |
| 59 | * UnlabeledValueArg constructor. |
| 60 | * \param name - A one word name for the argument. Note that this is used |
| 61 | * for |
| 62 | * identification, not as a long flag. |
| 63 | * \param desc - A description of what the argument is for or |
| 64 | * does. |
| 65 | * \param req - Whether the argument is required on the command |
| 66 | * line. |
| 67 | * \param value - The default value assigned to this argument if it |
| 68 | * is not present on the command line. |
| 69 | * \param typeDesc - A short, human readable description of the |
| 70 | * type that this object expects. This is used in the generation |
| 71 | * of the USAGE statement. The goal is to be helpful to the end user |
| 72 | * of the program. |
| 73 | * \param ignoreable - Allows you to specify that this argument can be |
| 74 | * ignored if the '--' flag is set. This defaults to false (cannot |
| 75 | * be ignored) and should generally stay that way unless you have |
| 76 | * some special need for certain arguments to be ignored. |
| 77 | * \param v - Optional Visitor. You should leave this blank unless |
| 78 | * you have a very good reason. |
| 79 | */ |
| 80 | UnlabeledValueArg(const std::string &name, const std::string &desc, |
| 81 | bool req, T value, const std::string &typeDesc, |
| 82 | bool ignoreable = false, Visitor *v = NULL); |
| 83 | |
| 84 | /** |
| 85 | * UnlabeledValueArg constructor. |
| 86 | * \param name - A one word name for the argument. Note that this is used |
| 87 | * for |
| 88 | * identification, not as a long flag. |
| 89 | * \param desc - A description of what the argument is for or |
| 90 | * does. |
| 91 | * \param req - Whether the argument is required on the command |
| 92 | * line. |
| 93 | * \param value - The default value assigned to this argument if it |
| 94 | * is not present on the command line. |
| 95 | * \param typeDesc - A short, human readable description of the |
| 96 | * type that this object expects. This is used in the generation |
| 97 | * of the USAGE statement. The goal is to be helpful to the end user |
| 98 | * of the program. |
| 99 | * \param parser - A CmdLine parser object to add this Arg to |
| 100 | * \param ignoreable - Allows you to specify that this argument can be |
| 101 | * ignored if the '--' flag is set. This defaults to false (cannot |
| 102 | * be ignored) and should generally stay that way unless you have |
| 103 | * some special need for certain arguments to be ignored. |
| 104 | * \param v - Optional Visitor. You should leave this blank unless |
| 105 | * you have a very good reason. |
| 106 | */ |
| 107 | UnlabeledValueArg(const std::string &name, const std::string &desc, |
| 108 | bool req, T value, const std::string &typeDesc, |
| 109 | CmdLineInterface &parser, bool ignoreable = false, |
| 110 | Visitor *v = NULL); |
| 111 | |
| 112 | /** |
| 113 | * UnlabeledValueArg constructor. |
| 114 | * \param name - A one word name for the argument. Note that this is used |
| 115 | * for |
| 116 | * identification, not as a long flag. |
| 117 | * \param desc - A description of what the argument is for or |
| 118 | * does. |
| 119 | * \param req - Whether the argument is required on the command |
| 120 | * line. |
| 121 | * \param value - The default value assigned to this argument if it |
| 122 | * is not present on the command line. |
| 123 | * \param constraint - A pointer to a Constraint object used |
| 124 | * to constrain this Arg. |
| 125 | * \param ignoreable - Allows you to specify that this argument can be |
| 126 | * ignored if the '--' flag is set. This defaults to false (cannot |
| 127 | * be ignored) and should generally stay that way unless you have |
| 128 | * some special need for certain arguments to be ignored. |
| 129 | * \param v - Optional Visitor. You should leave this blank unless |
| 130 | * you have a very good reason. |
| 131 | */ |
| 132 | UnlabeledValueArg(const std::string &name, const std::string &desc, |
| 133 | bool req, T value, Constraint<T> *constraint, |
| 134 | bool ignoreable = false, Visitor *v = NULL); |
| 135 | |
| 136 | /** |
| 137 | * UnlabeledValueArg constructor. |
| 138 | * \param name - A one word name for the argument. Note that this is used |
| 139 | * for |
| 140 | * identification, not as a long flag. |
| 141 | * \param desc - A description of what the argument is for or |
| 142 | * does. |
| 143 | * \param req - Whether the argument is required on the command |
| 144 | * line. |
| 145 | * \param value - The default value assigned to this argument if it |
| 146 | * is not present on the command line. |
| 147 | * \param constraint - A pointer to a Constraint object used |
| 148 | * to constrain this Arg. |
| 149 | * \param parser - A CmdLine parser object to add this Arg to |
| 150 | * \param ignoreable - Allows you to specify that this argument can be |
| 151 | * ignored if the '--' flag is set. This defaults to false (cannot |
| 152 | * be ignored) and should generally stay that way unless you have |
| 153 | * some special need for certain arguments to be ignored. |
| 154 | * \param v - Optional Visitor. You should leave this blank unless |
| 155 | * you have a very good reason. |
| 156 | */ |
| 157 | UnlabeledValueArg(const std::string &name, const std::string &desc, |
| 158 | bool req, T value, Constraint<T> *constraint, |
| 159 | CmdLineInterface &parser, bool ignoreable = false, |
| 160 | Visitor *v = NULL); |
| 161 | |
| 162 | /** |
| 163 | * Handles the processing of the argument. |
| 164 | * This re-implements the Arg version of this method to set the |
| 165 | * _value of the argument appropriately. Handling specific to |
| 166 | * unlabeled arguments. |
| 167 | * \param i - Pointer the the current argument in the list. |
| 168 | * \param args - Mutable list of strings. |
| 169 | */ |
| 170 | virtual bool processArg(int *i, std::vector<std::string> &args); |
| 171 | |
| 172 | /** |
| 173 | * Overrides shortID for specific behavior. |
| 174 | */ |
| 175 | virtual std::string shortID(const std::string &) const { |
| 176 | return Arg::getName(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Overrides longID for specific behavior. |
| 181 | */ |
| 182 | virtual std::string longID(const std::string &) const { |
| 183 | return Arg::getName() + " <" + _typeDesc + ">"; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Overrides operator== for specific behavior. |
| 188 | */ |
| 189 | virtual bool operator==(const Arg &a) const; |
| 190 | |
| 191 | /** |
| 192 | * Instead of pushing to the front of list, push to the back. |
| 193 | * \param argList - The list to add this to. |
| 194 | */ |
| 195 | virtual void addToList(std::list<Arg *> &argList) const; |
| 196 | |
| 197 | virtual bool hasLabel() const { return false; } |
| 198 | }; |
| 199 | |
| 200 | /** |
| 201 | * Constructor implementation. |
| 202 | */ |
| 203 | template <class T> |
| 204 | UnlabeledValueArg<T>::UnlabeledValueArg(const std::string &name, |
| 205 | const std::string &desc, bool req, |
| 206 | T val, const std::string &typeDesc, |
| 207 | bool ignoreable, Visitor *v) |
| 208 | : ValueArg<T>("", name, desc, req, val, typeDesc, v) { |
| 209 | _ignoreable = ignoreable; |
| 210 | |
| 211 | OptionalUnlabeledTracker::check(req, toString()); |
| 212 | } |
| 213 | |
| 214 | template <class T> |
| 215 | UnlabeledValueArg<T>::UnlabeledValueArg(const std::string &name, |
| 216 | const std::string &desc, bool req, |
| 217 | T val, const std::string &typeDesc, |
| 218 | CmdLineInterface &parser, |
| 219 | bool ignoreable, Visitor *v) |
| 220 | : ValueArg<T>("", name, desc, req, val, typeDesc, v) { |
| 221 | _ignoreable = ignoreable; |
| 222 | OptionalUnlabeledTracker::check(req, toString()); |
| 223 | parser.add(this); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Constructor implementation. |
| 228 | */ |
| 229 | template <class T> |
| 230 | UnlabeledValueArg<T>::UnlabeledValueArg(const std::string &name, |
| 231 | const std::string &desc, bool req, |
| 232 | T val, Constraint<T> *constraint, |
| 233 | bool ignoreable, Visitor *v) |
| 234 | : ValueArg<T>("", name, desc, req, val, constraint, v) { |
| 235 | _ignoreable = ignoreable; |
| 236 | OptionalUnlabeledTracker::check(req, toString()); |
| 237 | } |
| 238 | |
| 239 | template <class T> |
| 240 | UnlabeledValueArg<T>::UnlabeledValueArg(const std::string &name, |
| 241 | const std::string &desc, bool req, |
| 242 | T val, Constraint<T> *constraint, |
| 243 | CmdLineInterface &parser, |
| 244 | bool ignoreable, Visitor *v) |
| 245 | : ValueArg<T>("", name, desc, req, val, constraint, v) { |
| 246 | _ignoreable = ignoreable; |
| 247 | OptionalUnlabeledTracker::check(req, toString()); |
| 248 | parser.add(this); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Implementation of processArg(). |
| 253 | */ |
| 254 | template <class T> |
| 255 | bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string> &args) { |
| 256 | if (_alreadySet) return false; |
| 257 | |
| 258 | if (_hasBlanks(args[*i])) return false; |
| 259 | |
| 260 | // never ignore an unlabeled arg |
| 261 | |
| 262 | _extractValue(args[*i]); |
| 263 | _alreadySet = true; |
| 264 | _setBy = args[*i]; |
| 265 | this->_checkWithVisitor(); |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Overriding operator== for specific behavior. |
| 271 | */ |
| 272 | template <class T> |
| 273 | bool UnlabeledValueArg<T>::operator==(const Arg &a) const { |
| 274 | if (_name == a.getName() || _description == a.getDescription()) |
| 275 | return true; |
| 276 | else |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | template <class T> |
| 281 | void UnlabeledValueArg<T>::addToList(std::list<Arg *> &argList) const { |
| 282 | argList.push_back(const_cast<Arg *>(static_cast<const Arg *const>(this))); |
| 283 | } |
| 284 | } // namespace TCLAP |
| 285 | |
| 286 | #endif // TCLAP_UNLABELED_VALUE_ARG_H |