tclap  1.2.2
UnlabeledValueArg.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * file: UnlabeledValueArg.h
5  *
6  * Copyright (c) 2003, Michael E. Smoot .
7  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
8  * All rights reserved.
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 
23 
24 #ifndef TCLAP_UNLABELED_VALUE_ARGUMENT_H
25 #define TCLAP_UNLABELED_VALUE_ARGUMENT_H
26 
27 #include <string>
28 #include <vector>
29 
30 #include <tclap/ValueArg.h>
32 
33 
34 namespace TCLAP {
35 
42 template<class T>
43 class UnlabeledValueArg : public ValueArg<T>
44 {
45 
46  // If compiler has two stage name lookup (as gcc >= 3.4 does)
47  // this is required to prevent undef. symbols
52  using ValueArg<T>::_name;
56 
57  public:
58 
80  UnlabeledValueArg( const std::string& name,
81  const std::string& desc,
82  bool req,
83  T value,
84  const std::string& typeDesc,
85  bool ignoreable = false,
86  Visitor* v = NULL);
87 
110  UnlabeledValueArg( const std::string& name,
111  const std::string& desc,
112  bool req,
113  T value,
114  const std::string& typeDesc,
115  CmdLineInterface& parser,
116  bool ignoreable = false,
117  Visitor* v = NULL );
118 
138  UnlabeledValueArg( const std::string& name,
139  const std::string& desc,
140  bool req,
141  T value,
142  Constraint<T>* constraint,
143  bool ignoreable = false,
144  Visitor* v = NULL );
145 
146 
167  UnlabeledValueArg( const std::string& name,
168  const std::string& desc,
169  bool req,
170  T value,
171  Constraint<T>* constraint,
172  CmdLineInterface& parser,
173  bool ignoreable = false,
174  Visitor* v = NULL);
175 
184  virtual bool processArg(int* i, std::vector<std::string>& args);
185 
189  virtual std::string shortID(const std::string& val="val") const;
190 
194  virtual std::string longID(const std::string& val="val") const;
195 
199  virtual bool operator==(const Arg& a ) const;
200 
205  virtual void addToList( std::list<Arg*>& argList ) const;
206 
207 };
208 
212 template<class T>
213 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
214  const std::string& desc,
215  bool req,
216  T val,
217  const std::string& typeDesc,
218  bool ignoreable,
219  Visitor* v)
220 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
221 {
222  _ignoreable = ignoreable;
223 
225 
226 }
227 
228 template<class T>
229 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
230  const std::string& desc,
231  bool req,
232  T val,
233  const std::string& typeDesc,
234  CmdLineInterface& parser,
235  bool ignoreable,
236  Visitor* v)
237 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
238 {
239  _ignoreable = ignoreable;
241  parser.add( this );
242 }
243 
247 template<class T>
249  const std::string& desc,
250  bool req,
251  T val,
252  Constraint<T>* constraint,
253  bool ignoreable,
254  Visitor* v)
255 : ValueArg<T>("", name, desc, req, val, constraint, v)
256 {
257  _ignoreable = ignoreable;
259 }
260 
261 template<class T>
262 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
263  const std::string& desc,
264  bool req,
265  T val,
266  Constraint<T>* constraint,
268  bool ignoreable,
269  Visitor* v)
270 : ValueArg<T>("", name, desc, req, val, constraint, v)
271 {
272  _ignoreable = ignoreable;
274  parser.add( this );
275 }
276 
280 template<class T>
281 bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string>& args)
282 {
283 
284  if ( _alreadySet )
285  return false;
286 
287  if ( _hasBlanks( args[*i] ) )
288  return false;
289 
290  // never ignore an unlabeled arg
291 
292  _extractValue( args[*i] );
293  _alreadySet = true;
294  return true;
295 }
296 
300 template<class T>
301 std::string UnlabeledValueArg<T>::shortID(const std::string& val) const
302 {
303  static_cast<void>(val); // Ignore input, don't warn
304  return std::string("<") + _typeDesc + ">";
305 }
306 
310 template<class T>
311 std::string UnlabeledValueArg<T>::longID(const std::string& val) const
312 {
313  static_cast<void>(val); // Ignore input, don't warn
314 
315  // Ideally we would like to be able to use RTTI to return the name
316  // of the type required for this argument. However, g++ at least,
317  // doesn't appear to return terribly useful "names" of the types.
318  return std::string("<") + _typeDesc + ">";
319 }
320 
324 template<class T>
325 bool UnlabeledValueArg<T>::operator==(const Arg& a ) const
326 {
327  if ( _name == a.getName() || _description == a.getDescription() )
328  return true;
329  else
330  return false;
331 }
332 
333 template<class T>
334 void UnlabeledValueArg<T>::addToList( std::list<Arg*>& argList ) const
335 {
336  argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
337 }
338 
339 }
340 #endif
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:63
ValueArg.h
TCLAP::Arg::_hasBlanks
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:662
TCLAP::Arg::getName
const std::string & getName() const
Returns the argument name.
Definition: Arg.h:590
TCLAP::CmdLineInterface::add
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
TCLAP::UnlabeledValueArg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: UnlabeledValueArg.h:300
TCLAP::Arg::toString
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:620
TCLAP::Arg::_ignoreable
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:191
TCLAP::Arg::getDescription
std::string getDescription() const
Returns the argument description.
Definition: Arg.h:575
TCLAP::Arg::_name
std::string _name
A single word namd identifying the argument.
Definition: Arg.h:148
TCLAP::Arg::_description
std::string _description
Description of the argument.
Definition: Arg.h:153
TCLAP::UnlabeledValueArg::shortID
virtual std::string shortID(const std::string &val="val") const
Overrides shortID for specific behavior.
Definition: UnlabeledValueArg.h:320
TCLAP::OptionalUnlabeledTracker::check
static void check(bool req, const std::string &argName)
Definition: OptionalUnlabeledTracker.h:66
TCLAP::UnlabeledValueArg::longID
virtual std::string longID(const std::string &val="val") const
Overrides longID for specific behavior.
Definition: UnlabeledValueArg.h:330
TCLAP::UnlabeledValueArg::operator==
virtual bool operator==(const Arg &a) const
Overrides operator== for specific behavior.
Definition: UnlabeledValueArg.h:344
TCLAP::UnlabeledValueArg::addToList
virtual void addToList(std::list< Arg * > &argList) const
Instead of pushing to the front of list, push to the back.
Definition: UnlabeledValueArg.h:353
TCLAP::ValueArg
The basic labeled argument that parses a value.
Definition: ValueArg.h:63
TCLAP::Visitor
A base class that defines the interface for visitors.
Definition: Visitor.h:50
TCLAP::ValueArg::_typeDesc
std::string _typeDesc
A human readable description of the type to be parsed.
Definition: ValueArg.h:105
OptionalUnlabeledTracker.h
TCLAP::Constraint
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:57
TCLAP::Arg::_alreadySet
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:178
TCLAP
Definition: Arg.h:58
TCLAP::UnlabeledValueArg::UnlabeledValueArg
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.
Definition: UnlabeledValueArg.h:232
TCLAP::Arg
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:86
TCLAP::ValueArg::_extractValue
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: ValueArg.h:419