tclap  1.2.2
UnlabeledValueArg.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 
4 /******************************************************************************
5  *
6  * file: UnlabeledValueArg.h
7  *
8  * Copyright (c) 2003, Michael E. Smoot .
9  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
10  * Copyright (c) 2017, Google LLC
11  * All rights reserved.
12  *
13  * See the file COPYING in the top directory of this distribution for
14  * more information.
15  *
16  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  *****************************************************************************/
25 
26 
27 #ifndef TCLAP_UNLABELED_VALUE_ARGUMENT_H
28 #define TCLAP_UNLABELED_VALUE_ARGUMENT_H
29 
30 #include <string>
31 #include <vector>
32 
33 #include <tclap/ValueArg.h>
35 
36 
37 namespace TCLAP {
38 
45 template<class T>
46 class UnlabeledValueArg : public ValueArg<T>
47 {
48 
49  // If compiler has two stage name lookup (as gcc >= 3.4 does)
50  // this is required to prevent undef. symbols
55  using ValueArg<T>::_name;
59 
60  public:
61 
83  UnlabeledValueArg( const std::string& name,
84  const std::string& desc,
85  bool req,
86  T value,
87  const std::string& typeDesc,
88  bool ignoreable = false,
89  Visitor* v = NULL);
90 
113  UnlabeledValueArg( const std::string& name,
114  const std::string& desc,
115  bool req,
116  T value,
117  const std::string& typeDesc,
118  CmdLineInterface& parser,
119  bool ignoreable = false,
120  Visitor* v = NULL );
121 
141  UnlabeledValueArg( const std::string& name,
142  const std::string& desc,
143  bool req,
144  T value,
145  Constraint<T>* constraint,
146  bool ignoreable = false,
147  Visitor* v = NULL );
148 
149 
170  UnlabeledValueArg( const std::string& name,
171  const std::string& desc,
172  bool req,
173  T value,
174  Constraint<T>* constraint,
175  CmdLineInterface& parser,
176  bool ignoreable = false,
177  Visitor* v = NULL);
178 
187  virtual bool processArg(int* i, std::vector<std::string>& args);
188 
192  virtual std::string shortID(const std::string& val="val") const;
193 
197  virtual std::string longID(const std::string& val="val") const;
198 
202  virtual bool operator==(const Arg& a ) const;
203 
208  virtual void addToList( std::list<Arg*>& argList ) const;
209 
210 };
211 
215 template<class T>
217  const std::string& desc,
218  bool req,
219  T val,
220  const std::string& typeDesc,
221  bool ignoreable,
222  Visitor* v)
223 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
224 {
225  _ignoreable = ignoreable;
226 
228 
229 }
230 
231 template<class T>
233  const std::string& desc,
234  bool req,
235  T val,
236  const std::string& typeDesc,
237  CmdLineInterface& parser,
238  bool ignoreable,
239  Visitor* v)
240 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
241 {
242  _ignoreable = ignoreable;
244  parser.add( this );
245 }
246 
250 template<class T>
252  const std::string& desc,
253  bool req,
254  T val,
255  Constraint<T>* constraint,
256  bool ignoreable,
257  Visitor* v)
258 : ValueArg<T>("", name, desc, req, val, constraint, v)
259 {
260  _ignoreable = ignoreable;
262 }
263 
264 template<class T>
266  const std::string& desc,
267  bool req,
268  T val,
269  Constraint<T>* constraint,
270  CmdLineInterface& parser,
271  bool ignoreable,
272  Visitor* v)
273 : ValueArg<T>("", name, desc, req, val, constraint, v)
274 {
275  _ignoreable = ignoreable;
277  parser.add( this );
278 }
279 
283 template<class T>
284 bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string>& args)
285 {
286 
287  if ( _alreadySet )
288  return false;
289 
290  if ( _hasBlanks( args[*i] ) )
291  return false;
292 
293  // never ignore an unlabeled arg
294 
295  _extractValue( args[*i] );
296  _alreadySet = true;
297  return true;
298 }
299 
303 template<class T>
304 std::string UnlabeledValueArg<T>::shortID(const std::string& val) const
305 {
306  static_cast<void>(val); // Ignore input, don't warn
307  return std::string("<") + _typeDesc + ">";
308 }
309 
313 template<class T>
314 std::string UnlabeledValueArg<T>::longID(const std::string& val) const
315 {
316  static_cast<void>(val); // Ignore input, don't warn
317 
318  // Ideally we would like to be able to use RTTI to return the name
319  // of the type required for this argument. However, g++ at least,
320  // doesn't appear to return terribly useful "names" of the types.
321  return std::string("<") + _typeDesc + ">";
322 }
323 
327 template<class T>
329 {
330  if ( _name == a.getName() || _description == a.getDescription() )
331  return true;
332  else
333  return false;
334 }
335 
336 template<class T>
337 void UnlabeledValueArg<T>::addToList( std::list<Arg*>& argList ) const
338 {
339  argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
340 }
341 
342 }
343 #endif
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:128
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: ValueArg.h:405
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:55
static void check(bool req, const std::string &argName)
The basic labeled argument that parses a value.
Definition: ValueArg.h:46
UnlabeledValueArg(const std::string &name, const std::string &desc, bool req, T value, const std::string &typeDesc, bool ignoreable=false, Visitor *v=NULL)
UnlabeledValueArg constructor.
virtual std::string longID(const std::string &val="val") const
Overrides longID for specific behavior.
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:42
virtual bool operator==(const Arg &a) const
Overrides operator== for specific behavior.
A base class that defines the interface for visitors.
Definition: Visitor.h:34
const std::string & getName() const
Returns the argument name.
Definition: Arg.h:560
std::string _description
Description of the argument.
Definition: Arg.h:103
The base class that manages the command line definition and passes along the parsing to the appropria...
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:590
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:141
std::string _typeDesc
A human readable description of the type to be parsed.
Definition: ValueArg.h:70
std::string getDescription() const
Returns the argument description.
Definition: Arg.h:545
virtual void addToList(std::list< Arg *> &argList) const
Instead of pushing to the front of list, push to the back.
std::string _name
A single word namd identifying the argument.
Definition: Arg.h:98
virtual std::string shortID(const std::string &val="val") const
Overrides shortID for specific behavior.
Definition: Arg.h:48
bool _hasBlanks(const std::string &s) const
Checks whether a given string has blank chars, indicating that it is a combined SwitchArg.
Definition: Arg.h:632
The basic unlabeled argument that parses a value.