tclap  1.2.1
UnlabeledMultiArg.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * file: UnlabeledMultiArg.h
5  *
6  * Copyright (c) 2003, Michael E. Smoot.
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 
22 
23 #ifndef TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H
24 #define TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H
25 
26 #include <string>
27 #include <vector>
28 
29 #include <tclap/MultiArg.h>
31 
32 namespace TCLAP {
33 
39 template<class T>
40 class UnlabeledMultiArg : public MultiArg<T>
41 {
42 
43  // If compiler has two stage name lookup (as gcc >= 3.4 does)
44  // this is requried to prevent undef. symbols
49  using MultiArg<T>::_name;
53 
54  public:
55 
73  UnlabeledMultiArg( const std::string& name,
74  const std::string& desc,
75  bool req,
76  const std::string& typeDesc,
77  bool ignoreable = false,
78  Visitor* v = NULL );
97  UnlabeledMultiArg( const std::string& name,
98  const std::string& desc,
99  bool req,
100  const std::string& typeDesc,
101  CmdLineInterface& parser,
102  bool ignoreable = false,
103  Visitor* v = NULL );
104 
120  UnlabeledMultiArg( const std::string& name,
121  const std::string& desc,
122  bool req,
123  Constraint<T>* constraint,
124  bool ignoreable = false,
125  Visitor* v = NULL );
126 
143  UnlabeledMultiArg( const std::string& name,
144  const std::string& desc,
145  bool req,
146  Constraint<T>* constraint,
147  CmdLineInterface& parser,
148  bool ignoreable = false,
149  Visitor* v = NULL );
150 
159  virtual bool processArg(int* i, std::vector<std::string>& args);
160 
165  virtual std::string shortID(const std::string& val="val") const;
166 
171  virtual std::string longID(const std::string& val="val") const;
172 
177  virtual bool operator==(const Arg& a) const;
178 
183  virtual void addToList( std::list<Arg*>& argList ) const;
184 };
185 
186 template<class T>
187 UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name,
188  const std::string& desc,
189  bool req,
190  const std::string& typeDesc,
191  bool ignoreable,
192  Visitor* v)
193 : MultiArg<T>("", name, desc, req, typeDesc, v)
194 {
195  _ignoreable = ignoreable;
197 }
198 
199 template<class T>
200 UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name,
201  const std::string& desc,
202  bool req,
203  const std::string& typeDesc,
204  CmdLineInterface& parser,
205  bool ignoreable,
206  Visitor* v)
207 : MultiArg<T>("", name, desc, req, typeDesc, v)
208 {
209  _ignoreable = ignoreable;
211  parser.add( this );
212 }
213 
214 
215 template<class T>
216 UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name,
217  const std::string& desc,
218  bool req,
219  Constraint<T>* constraint,
220  bool ignoreable,
221  Visitor* v)
222 : MultiArg<T>("", name, desc, req, constraint, v)
223 {
224  _ignoreable = ignoreable;
226 }
227 
228 template<class T>
229 UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name,
230  const std::string& desc,
231  bool req,
232  Constraint<T>* constraint,
233  CmdLineInterface& parser,
234  bool ignoreable,
235  Visitor* v)
236 : MultiArg<T>("", name, desc, req, constraint, v)
237 {
238  _ignoreable = ignoreable;
240  parser.add( this );
241 }
242 
243 
244 template<class T>
245 bool UnlabeledMultiArg<T>::processArg(int *i, std::vector<std::string>& args)
246 {
247 
248  if ( _hasBlanks( args[*i] ) )
249  return false;
250 
251  // never ignore an unlabeled multi arg
252 
253 
254  // always take the first value, regardless of the start string
255  _extractValue( args[(*i)] );
256 
257  /*
258  // continue taking args until we hit the end or a start string
259  while ( (unsigned int)(*i)+1 < args.size() &&
260  args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
261  args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
262  _extractValue( args[++(*i)] );
263  */
264 
265  _alreadySet = true;
266 
267  return true;
268 }
269 
270 template<class T>
271 std::string UnlabeledMultiArg<T>::shortID(const std::string& val) const
272 {
273  static_cast<void>(val); // Ignore input, don't warn
274  return std::string("<") + _typeDesc + "> ...";
275 }
276 
277 template<class T>
278 std::string UnlabeledMultiArg<T>::longID(const std::string& val) const
279 {
280  static_cast<void>(val); // Ignore input, don't warn
281  return std::string("<") + _typeDesc + "> (accepted multiple times)";
282 }
283 
284 template<class T>
285 bool UnlabeledMultiArg<T>::operator==(const Arg& a) const
286 {
287  if ( _name == a.getName() || _description == a.getDescription() )
288  return true;
289  else
290  return false;
291 }
292 
293 template<class T>
294 void UnlabeledMultiArg<T>::addToList( std::list<Arg*>& argList ) const
295 {
296  argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
297 }
298 
299 }
300 
301 #endif
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:63
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:641
TCLAP::CmdLineInterface::add
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
TCLAP::Arg::toString
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:599
TCLAP::Arg::_ignoreable
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:150
MultiArg.h
TCLAP::UnlabeledMultiArg::longID
virtual std::string longID(const std::string &val="val") const
Returns the a long id string.
Definition: UnlabeledMultiArg.h:296
TCLAP::MultiArg
An argument that allows multiple values of type T to be specified.
Definition: MultiArg.h:59
TCLAP::Arg::_name
std::string _name
A single work namd indentifying the argument.
Definition: Arg.h:107
TCLAP::Arg::_description
std::string _description
Description of the argument.
Definition: Arg.h:112
TCLAP::OptionalUnlabeledTracker::check
static void check(bool req, const std::string &argName)
Definition: OptionalUnlabeledTracker.h:66
TCLAP::MultiArg::_typeDesc
std::string _typeDesc
The description of type T to be used in the usage.
Definition: MultiArg.h:94
TCLAP::UnlabeledMultiArg::addToList
virtual void addToList(std::list< Arg * > &argList) const
Pushes this to back of list rather than front.
Definition: UnlabeledMultiArg.h:312
TCLAP::UnlabeledMultiArg::UnlabeledMultiArg
UnlabeledMultiArg(const std::string &name, const std::string &desc, bool req, const std::string &typeDesc, bool ignoreable=false, Visitor *v=NULL)
Constructor.
Definition: UnlabeledMultiArg.h:205
TCLAP::MultiArg::_extractValue
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: MultiArg.h:417
TCLAP::Visitor
A base class that defines the interface for visitors.
Definition: Visitor.h:50
TCLAP::UnlabeledMultiArg::operator==
virtual bool operator==(const Arg &a) const
Opertor ==.
Definition: UnlabeledMultiArg.h:303
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:137
TCLAP::UnlabeledMultiArg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: UnlabeledMultiArg.h:263
TCLAP::UnlabeledMultiArg::shortID
virtual std::string shortID(const std::string &val="val") const
Returns the a short id string.
Definition: UnlabeledMultiArg.h:289
TCLAP
Definition: Arg.h:57
TCLAP::Arg
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:65