No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

217 líneas
5.5 KiB

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