Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

341 linhas
11 KiB

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