UnlabeledValueArg.h 11 KB

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