| 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- |
| 2 | |
| 3 | /****************************************************************************** |
| 4 | * |
| 5 | * file: MultiSwitchArg.h |
| 6 | * |
| 7 | * Copyright (c) 2003, Michael E. Smoot . |
| 8 | * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno. |
| 9 | * Copyright (c) 2005, Michael E. Smoot, Daniel Aarno, Erik Zeek. |
| 10 | * Copyright (c) 2017, Google LLC |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * See the file COPYING in the top directory of this distribution for |
| 14 | * more information. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | *****************************************************************************/ |
| 25 | |
| 26 | #ifndef TCLAP_MULTI_SWITCH_ARG_H |
| 27 | #define TCLAP_MULTI_SWITCH_ARG_H |
| 28 | |
| 29 | #include <tclap/SwitchArg.h> |
| 30 | |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace TCLAP { |
| 35 | |
| 36 | /** |
| 37 | * A multiple switch argument. If the switch is set on the command line, then |
| 38 | * the getValue method will return the number of times the switch appears. |
| 39 | */ |
| 40 | class MultiSwitchArg : public SwitchArg { |
| 41 | protected: |
| 42 | /** |
| 43 | * The value of the switch. |
| 44 | */ |
| 45 | int _value; |
| 46 | |
| 47 | /** |
| 48 | * Used to support the reset() method so that ValueArg can be |
| 49 | * reset to their constructed value. |
| 50 | */ |
| 51 | int _default; |
| 52 | |
| 53 | public: |
| 54 | /** |
| 55 | * MultiSwitchArg constructor. |
| 56 | * \param flag - The one character flag that identifies this |
| 57 | * argument on the command line. |
| 58 | * \param name - A one word name for the argument. Can be |
| 59 | * used as a long flag on the command line. |
| 60 | * \param desc - A description of what the argument is for or |
| 61 | * does. |
| 62 | * \param init - Optional. The initial/default value of this Arg. |
| 63 | * Defaults to 0. |
| 64 | * \param v - An optional visitor. You probably should not |
| 65 | * use this unless you have a very good reason. |
| 66 | */ |
| 67 | MultiSwitchArg(const std::string &flag, const std::string &name, |
| 68 | const std::string &desc, int init = 0, Visitor *v = NULL); |
| 69 | |
| 70 | /** |
| 71 | * MultiSwitchArg constructor. |
| 72 | * \param flag - The one character flag that identifies this |
| 73 | * argument on the command line. |
| 74 | * \param name - A one word name for the argument. Can be |
| 75 | * used as a long flag on the command line. |
| 76 | * \param desc - A description of what the argument is for or |
| 77 | * does. |
| 78 | * \param parser - A CmdLine parser object to add this Arg to |
| 79 | * \param init - Optional. The initial/default value of this Arg. |
| 80 | * Defaults to 0. |
| 81 | * \param v - An optional visitor. You probably should not |
| 82 | * use this unless you have a very good reason. |
| 83 | */ |
| 84 | MultiSwitchArg(const std::string &flag, const std::string &name, |
| 85 | const std::string &desc, ArgContainer &parser, int init = 0, |
| 86 | Visitor *v = NULL); |
| 87 | |
| 88 | /** |
| 89 | * Handles the processing of the argument. |
| 90 | * This re-implements the SwitchArg version of this method to set the |
| 91 | * _value of the argument appropriately. |
| 92 | * \param i - Pointer the the current argument in the list. |
| 93 | * \param args - Mutable list of strings. Passed |
| 94 | * in from main(). |
| 95 | */ |
| 96 | virtual bool processArg(int *i, std::vector<std::string> &args); |
| 97 | |
| 98 | /** |
| 99 | * Returns int, the number of times the switch has been set. |
| 100 | */ |
| 101 | int getValue() const { return _value; } |
| 102 | |
| 103 | /** |
| 104 | * Returns the shortID for this Arg. |
| 105 | */ |
| 106 | std::string shortID(const std::string &val) const; |
| 107 | |
| 108 | /** |
| 109 | * Returns the longID for this Arg. |
| 110 | */ |
| 111 | std::string longID(const std::string &val) const; |
| 112 | |
| 113 | void reset(); |
| 114 | }; |
| 115 | |
| 116 | inline MultiSwitchArg::MultiSwitchArg(const std::string &flag, |
| 117 | const std::string &name, |
| 118 | const std::string &desc, int init, |
| 119 | Visitor *v) |
| 120 | : SwitchArg(flag, name, desc, false, v), _value(init), _default(init) {} |
| 121 | |
| 122 | inline MultiSwitchArg::MultiSwitchArg(const std::string &flag, |
| 123 | const std::string &name, |
| 124 | const std::string &desc, |
| 125 | ArgContainer &parser, int init, |
| 126 | Visitor *v) |
| 127 | : SwitchArg(flag, name, desc, false, v), _value(init), _default(init) { |
| 128 | parser.add(this); |
| 129 | } |
| 130 | |
| 131 | inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string> &args) { |
| 132 | if (argMatches(args[*i])) { |
| 133 | // so the isSet() method will work |
| 134 | _alreadySet = true; |
| 135 | _setBy = args[*i]; |
| 136 | |
| 137 | // Matched argument: increment value. |
| 138 | ++_value; |
| 139 | |
| 140 | _checkWithVisitor(); |
| 141 | |
| 142 | return true; |
| 143 | } else if (combinedSwitchesMatch(args[*i])) { |
| 144 | // so the isSet() method will work |
| 145 | _alreadySet = true; |
| 146 | |
| 147 | // Matched argument: increment value. |
| 148 | ++_value; |
| 149 | |
| 150 | // Check for more in argument and increment value. |
| 151 | while (combinedSwitchesMatch(args[*i])) ++_value; |
| 152 | |
| 153 | _checkWithVisitor(); |
| 154 | |
| 155 | return false; |
| 156 | } else { |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | inline std::string MultiSwitchArg::shortID(const std::string &val) const { |
| 162 | return Arg::shortID(val) + " ..."; |
| 163 | } |
| 164 | |
| 165 | inline std::string MultiSwitchArg::longID(const std::string &val) const { |
| 166 | return Arg::longID(val) + " (accepted multiple times)"; |
| 167 | } |
| 168 | |
| 169 | inline void MultiSwitchArg::reset() { |
| 170 | MultiSwitchArg::_value = MultiSwitchArg::_default; |
| 171 | } |
| 172 | |
| 173 | } // namespace TCLAP |
| 174 | |
| 175 | #endif // TCLAP_MULTI_SWITCH_ARG_H |