ValueArg.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
  2. /******************************************************************************
  3. *
  4. * file: ValueArg.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_VALUE_ARGUMENT_H
  24. #define TCLAP_VALUE_ARGUMENT_H
  25. #include <string>
  26. #include <vector>
  27. #include <tclap/Arg.h>
  28. #include <tclap/Constraint.h>
  29. namespace TCLAP {
  30. /**
  31. * The basic labeled 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 the flag/name is matched
  34. * on the command line. While there is nothing stopping you from creating
  35. * an unflagged ValueArg, it is unwise and would cause significant problems.
  36. * Instead use an UnlabeledValueArg.
  37. */
  38. template<class T>
  39. class ValueArg : public Arg
  40. {
  41. protected:
  42. /**
  43. * The value parsed from the command line.
  44. * Can be of any type, as long as the >> operator for the type
  45. * is defined.
  46. */
  47. T _value;
  48. /**
  49. * Used to support the reset() method so that ValueArg can be
  50. * reset to their constructed value.
  51. */
  52. T _default;
  53. /**
  54. * A human readable description of the type to be parsed.
  55. * This is a hack, plain and simple. Ideally we would use RTTI to
  56. * return the name of type T, but until there is some sort of
  57. * consistent support for human readable names, we are left to our
  58. * own devices.
  59. */
  60. std::string _typeDesc;
  61. /**
  62. * A Constraint this Arg must conform to.
  63. */
  64. Constraint<T>* _constraint;
  65. /**
  66. * Extracts the value from the string.
  67. * Attempts to parse string as type T, if this fails an exception
  68. * is thrown.
  69. * \param val - value to be parsed.
  70. */
  71. void _extractValue( const std::string& val );
  72. public:
  73. /**
  74. * Labeled ValueArg constructor.
  75. * You could conceivably call this constructor with a blank flag,
  76. * but that would make you a bad person. It would also cause
  77. * an exception to be thrown. If you want an unlabeled argument,
  78. * use the other constructor.
  79. * \param flag - The one character flag that identifies this
  80. * argument on the command line.
  81. * \param name - A one word name for the argument. Can be
  82. * used as a long flag on the command line.
  83. * \param desc - A description of what the argument is for or
  84. * does.
  85. * \param req - Whether the argument is required on the command
  86. * line.
  87. * \param value - The default value assigned to this argument if it
  88. * is not present on the command line.
  89. * \param typeDesc - A short, human readable description of the
  90. * type that this object expects. This is used in the generation
  91. * of the USAGE statement. The goal is to be helpful to the end user
  92. * of the program.
  93. * \param v - An optional visitor. You probably should not
  94. * use this unless you have a very good reason.
  95. */
  96. ValueArg( const std::string& flag,
  97. const std::string& name,
  98. const std::string& desc,
  99. bool req,
  100. T value,
  101. const std::string& typeDesc,
  102. Visitor* v = NULL);
  103. /**
  104. * Labeled ValueArg constructor.
  105. * You could conceivably call this constructor with a blank flag,
  106. * but that would make you a bad person. It would also cause
  107. * an exception to be thrown. If you want an unlabeled argument,
  108. * use the other constructor.
  109. * \param flag - The one character flag that identifies this
  110. * argument on the command line.
  111. * \param name - A one word name for the argument. Can be
  112. * used as a long flag on the command line.
  113. * \param desc - A description of what the argument is for or
  114. * does.
  115. * \param req - Whether the argument is required on the command
  116. * line.
  117. * \param value - The default value assigned to this argument if it
  118. * is not present on the command line.
  119. * \param typeDesc - A short, human readable description of the
  120. * type that this object expects. This is used in the generation
  121. * of the USAGE statement. The goal is to be helpful to the end user
  122. * of the program.
  123. * \param parser - A CmdLine parser object to add this Arg to
  124. * \param v - An optional visitor. You probably should not
  125. * use this unless you have a very good reason.
  126. */
  127. ValueArg( const std::string& flag,
  128. const std::string& name,
  129. const std::string& desc,
  130. bool req,
  131. T value,
  132. const std::string& typeDesc,
  133. CmdLineInterface& parser,
  134. Visitor* v = NULL );
  135. /**
  136. * Labeled ValueArg constructor.
  137. * You could conceivably call this constructor with a blank flag,
  138. * but that would make you a bad person. It would also cause
  139. * an exception to be thrown. If you want an unlabeled argument,
  140. * use the other constructor.
  141. * \param flag - The one character flag that identifies this
  142. * argument on the command line.
  143. * \param name - A one word name for the argument. Can be
  144. * used as a long flag on the command line.
  145. * \param desc - A description of what the argument is for or
  146. * does.
  147. * \param req - Whether the argument is required on the command
  148. * line.
  149. * \param value - The default value assigned to this argument if it
  150. * is not present on the command line.
  151. * \param constraint - A pointer to a Constraint object used
  152. * to constrain this Arg.
  153. * \param parser - A CmdLine parser object to add this Arg to.
  154. * \param v - An optional visitor. You probably should not
  155. * use this unless you have a very good reason.
  156. */
  157. ValueArg( const std::string& flag,
  158. const std::string& name,
  159. const std::string& desc,
  160. bool req,
  161. T value,
  162. Constraint<T>* constraint,
  163. CmdLineInterface& parser,
  164. Visitor* v = NULL );
  165. /**
  166. * Labeled ValueArg constructor.
  167. * You could conceivably call this constructor with a blank flag,
  168. * but that would make you a bad person. It would also cause
  169. * an exception to be thrown. If you want an unlabeled argument,
  170. * use the other constructor.
  171. * \param flag - The one character flag that identifies this
  172. * argument on the command line.
  173. * \param name - A one word name for the argument. Can be
  174. * used as a long flag on the command line.
  175. * \param desc - A description of what the argument is for or
  176. * does.
  177. * \param req - Whether the argument is required on the command
  178. * line.
  179. * \param value - The default value assigned to this argument if it
  180. * is not present on the command line.
  181. * \param constraint - A pointer to a Constraint object used
  182. * to constrain this Arg.
  183. * \param v - An optional visitor. You probably should not
  184. * use this unless you have a very good reason.
  185. */
  186. ValueArg( const std::string& flag,
  187. const std::string& name,
  188. const std::string& desc,
  189. bool req,
  190. T value,
  191. Constraint<T>* constraint,
  192. Visitor* v = NULL );
  193. /**
  194. * Handles the processing of the argument.
  195. * This re-implements the Arg version of this method to set the
  196. * _value of the argument appropriately. It knows the difference
  197. * between labeled and unlabeled.
  198. * \param i - Pointer the the current argument in the list.
  199. * \param args - Mutable list of strings. Passed
  200. * in from main().
  201. */
  202. virtual bool processArg(int* i, std::vector<std::string>& args);
  203. /**
  204. * Returns the value of the argument.
  205. */
  206. const T& getValue() const { return _value; }
  207. // TODO(macbishop): Non-const variant is deprecated, don't
  208. // use. Remove in next major.
  209. T& getValue() { return _value; }
  210. /**
  211. * A ValueArg can be used as as its value type (T) This is the
  212. * same as calling getValue()
  213. */
  214. operator const T&() const { return getValue(); }
  215. /**
  216. * Specialization of shortID.
  217. * \param val - value to be used.
  218. */
  219. virtual std::string shortID(const std::string& val = "val") const;
  220. /**
  221. * Specialization of longID.
  222. * \param val - value to be used.
  223. */
  224. virtual std::string longID(const std::string& val = "val") const;
  225. virtual void reset() ;
  226. private:
  227. /**
  228. * Prevent accidental copying
  229. */
  230. ValueArg(const ValueArg<T>& rhs);
  231. ValueArg& operator=(const ValueArg<T>& rhs);
  232. };
  233. /**
  234. * Constructor implementation.
  235. */
  236. template<class T>
  237. ValueArg<T>::ValueArg(const std::string& flag,
  238. const std::string& name,
  239. const std::string& desc,
  240. bool req,
  241. T val,
  242. const std::string& typeDesc,
  243. Visitor* v)
  244. : Arg(flag, name, desc, req, true, v),
  245. _value( val ),
  246. _default( val ),
  247. _typeDesc( typeDesc ),
  248. _constraint( NULL )
  249. { }
  250. template<class T>
  251. ValueArg<T>::ValueArg(const std::string& flag,
  252. const std::string& name,
  253. const std::string& desc,
  254. bool req,
  255. T val,
  256. const std::string& typeDesc,
  257. CmdLineInterface& parser,
  258. Visitor* v)
  259. : Arg(flag, name, desc, req, true, v),
  260. _value( val ),
  261. _default( val ),
  262. _typeDesc( typeDesc ),
  263. _constraint( NULL )
  264. {
  265. parser.add( this );
  266. }
  267. template<class T>
  268. ValueArg<T>::ValueArg(const std::string& flag,
  269. const std::string& name,
  270. const std::string& desc,
  271. bool req,
  272. T val,
  273. Constraint<T>* constraint,
  274. Visitor* v)
  275. : Arg(flag, name, desc, req, true, v),
  276. _value( val ),
  277. _default( val ),
  278. _typeDesc( Constraint<T>::shortID(constraint) ),
  279. _constraint( constraint )
  280. { }
  281. template<class T>
  282. ValueArg<T>::ValueArg(const std::string& flag,
  283. const std::string& name,
  284. const std::string& desc,
  285. bool req,
  286. T val,
  287. Constraint<T>* constraint,
  288. CmdLineInterface& parser,
  289. Visitor* v)
  290. : Arg(flag, name, desc, req, true, v),
  291. _value( val ),
  292. _default( val ),
  293. _typeDesc( Constraint<T>::shortID(constraint) ), // TODO(macbishop): Will crash
  294. // if constraint is NULL
  295. _constraint( constraint )
  296. {
  297. parser.add( this );
  298. }
  299. /**
  300. * Implementation of processArg().
  301. */
  302. template<class T>
  303. bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
  304. {
  305. if ( _ignoreable && Arg::ignoreRest() )
  306. return false;
  307. if ( _hasBlanks( args[*i] ) )
  308. return false;
  309. std::string flag = args[*i];
  310. std::string value = "";
  311. trimFlag( flag, value );
  312. if ( argMatches( flag ) )
  313. {
  314. if ( _alreadySet )
  315. {
  316. if ( _xorSet )
  317. throw( CmdLineParseException("Mutually exclusive argument"
  318. " already set!", toString()));
  319. else
  320. throw( CmdLineParseException("Argument already set!",
  321. toString()) );
  322. }
  323. if ( Arg::delimiter() != ' ' && value == "" )
  324. throw( ArgParseException("Couldn't find delimiter for this argument!",
  325. toString() ) );
  326. if ( value == "" )
  327. {
  328. (*i)++;
  329. if ( static_cast<unsigned int>(*i) < args.size() )
  330. _extractValue( args[*i] );
  331. else
  332. throw( ArgParseException("Missing a value for this argument!",
  333. toString() ) );
  334. }
  335. else
  336. _extractValue( value );
  337. _alreadySet = true;
  338. _checkWithVisitor();
  339. return true;
  340. }
  341. else
  342. return false;
  343. }
  344. /**
  345. * Implementation of shortID.
  346. */
  347. template<class T>
  348. std::string ValueArg<T>::shortID(const std::string& val) const
  349. {
  350. static_cast<void>(val); // Ignore input, don't warn
  351. return Arg::shortID( _typeDesc );
  352. }
  353. /**
  354. * Implementation of longID.
  355. */
  356. template<class T>
  357. std::string ValueArg<T>::longID(const std::string& val) const
  358. {
  359. static_cast<void>(val); // Ignore input, don't warn
  360. return Arg::longID( _typeDesc );
  361. }
  362. template<class T>
  363. void ValueArg<T>::_extractValue( const std::string& val )
  364. {
  365. try {
  366. ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
  367. } catch( ArgParseException &e) {
  368. throw ArgParseException(e.error(), toString());
  369. }
  370. if ( _constraint != NULL )
  371. if ( ! _constraint->check( _value ) )
  372. throw( CmdLineParseException( "Value '" + val +
  373. + "' does not meet constraint: "
  374. + _constraint->description(),
  375. toString() ) );
  376. }
  377. template<class T>
  378. void ValueArg<T>::reset()
  379. {
  380. Arg::reset();
  381. _value = _default;
  382. }
  383. } // namespace TCLAP
  384. #endif