MultiSwitchArg.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
  2. /******************************************************************************
  3. *
  4. * file: MultiSwitchArg.h
  5. *
  6. * Copyright (c) 2003, Michael E. Smoot .
  7. * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
  8. * Copyright (c) 2005, Michael E. Smoot, Daniel Aarno, Erik Zeek.
  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. #ifndef TCLAP_MULTI_SWITCH_ARG_H
  25. #define TCLAP_MULTI_SWITCH_ARG_H
  26. #include <string>
  27. #include <vector>
  28. #include <tclap/SwitchArg.h>
  29. namespace TCLAP {
  30. /**
  31. * A multiple switch argument. If the switch is set on the command line, then
  32. * the getValue method will return the number of times the switch appears.
  33. */
  34. class MultiSwitchArg : public SwitchArg
  35. {
  36. protected:
  37. /**
  38. * The value of the switch.
  39. */
  40. int _value;
  41. /**
  42. * Used to support the reset() method so that ValueArg can be
  43. * reset to their constructed value.
  44. */
  45. int _default;
  46. public:
  47. /**
  48. * MultiSwitchArg constructor.
  49. * \param flag - The one character flag that identifies this
  50. * argument on the command line.
  51. * \param name - A one word name for the argument. Can be
  52. * used as a long flag on the command line.
  53. * \param desc - A description of what the argument is for or
  54. * does.
  55. * \param init - Optional. The initial/default value of this Arg.
  56. * Defaults to 0.
  57. * \param v - An optional visitor. You probably should not
  58. * use this unless you have a very good reason.
  59. */
  60. MultiSwitchArg(const std::string& flag,
  61. const std::string& name,
  62. const std::string& desc,
  63. int init = 0,
  64. Visitor* v = NULL);
  65. /**
  66. * MultiSwitchArg constructor.
  67. * \param flag - The one character flag that identifies this
  68. * argument on the command line.
  69. * \param name - A one word name for the argument. Can be
  70. * used as a long flag on the command line.
  71. * \param desc - A description of what the argument is for or
  72. * does.
  73. * \param parser - A CmdLine parser object to add this Arg to
  74. * \param init - Optional. The initial/default value of this Arg.
  75. * Defaults to 0.
  76. * \param v - An optional visitor. You probably should not
  77. * use this unless you have a very good reason.
  78. */
  79. MultiSwitchArg(const std::string& flag,
  80. const std::string& name,
  81. const std::string& desc,
  82. CmdLineInterface& parser,
  83. int init = 0,
  84. Visitor* v = NULL);
  85. /**
  86. * Handles the processing of the argument.
  87. * This re-implements the SwitchArg version of this method to set the
  88. * _value of the argument appropriately.
  89. * \param i - Pointer the the current argument in the list.
  90. * \param args - Mutable list of strings. Passed
  91. * in from main().
  92. */
  93. virtual bool processArg(int* i, std::vector<std::string>& args);
  94. /**
  95. * Returns int, the number of times the switch has been set.
  96. */
  97. int getValue() const { return _value; }
  98. /**
  99. * Returns the shortID for this Arg.
  100. */
  101. std::string shortID(const std::string& val) const;
  102. /**
  103. * Returns the longID for this Arg.
  104. */
  105. std::string longID(const std::string& val) const;
  106. void reset();
  107. };
  108. //////////////////////////////////////////////////////////////////////
  109. //BEGIN MultiSwitchArg.cpp
  110. //////////////////////////////////////////////////////////////////////
  111. inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
  112. const std::string& name,
  113. const std::string& desc,
  114. int init,
  115. Visitor* v )
  116. : SwitchArg(flag, name, desc, false, v),
  117. _value( init ),
  118. _default( init )
  119. { }
  120. inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
  121. const std::string& name,
  122. const std::string& desc,
  123. CmdLineInterface& parser,
  124. int init,
  125. Visitor* v )
  126. : SwitchArg(flag, name, desc, false, v),
  127. _value( init ),
  128. _default( init )
  129. {
  130. parser.add( this );
  131. }
  132. inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string>& args)
  133. {
  134. if ( _ignoreable && Arg::ignoreRest() )
  135. return false;
  136. if ( argMatches( args[*i] ))
  137. {
  138. // so the isSet() method will work
  139. _alreadySet = true;
  140. // Matched argument: increment value.
  141. ++_value;
  142. _checkWithVisitor();
  143. return true;
  144. }
  145. else if ( combinedSwitchesMatch( args[*i] ) )
  146. {
  147. // so the isSet() method will work
  148. _alreadySet = true;
  149. // Matched argument: increment value.
  150. ++_value;
  151. // Check for more in argument and increment value.
  152. while ( combinedSwitchesMatch( args[*i] ) )
  153. ++_value;
  154. _checkWithVisitor();
  155. return false;
  156. }
  157. else
  158. return false;
  159. }
  160. inline std::string
  161. MultiSwitchArg::shortID(const std::string& val) const
  162. {
  163. return Arg::shortID(val) + " ...";
  164. }
  165. inline std::string
  166. MultiSwitchArg::longID(const std::string& val) const
  167. {
  168. return Arg::longID(val) + " (accepted multiple times)";
  169. }
  170. inline void
  171. MultiSwitchArg::reset()
  172. {
  173. MultiSwitchArg::_value = MultiSwitchArg::_default;
  174. }
  175. //////////////////////////////////////////////////////////////////////
  176. //END MultiSwitchArg.cpp
  177. //////////////////////////////////////////////////////////////////////
  178. } //namespace TCLAP
  179. #endif