Main MRPT website > C++ reference
MRPT logo
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 
31 
32 namespace TCLAP {
33 
34 /**
35  * Just like a MultiArg, except that the arguments are unlabeled. Basically,
36  * this Arg will slurp up everything that hasn't been matched to another
37  * Arg.
38  */
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 
56  /**
57  * Constructor.
58  * \param name - The name of the Arg. Note that this is used for
59  * identification, not as a long flag.
60  * \param desc - A description of what the argument is for or
61  * does.
62  * \param req - Whether the argument is required on the command
63  * line.
64  * \param typeDesc - A short, human readable description of the
65  * type that this object expects. This is used in the generation
66  * of the USAGE statement. The goal is to be helpful to the end user
67  * of the program.
68  * \param ignoreable - Whether or not this argument can be ignored
69  * using the "--" flag.
70  * \param v - An optional visitor. You probably should not
71  * use this unless you have a very good reason.
72  */
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 );
79  /**
80  * Constructor.
81  * \param name - The name of the Arg. Note that this is used for
82  * identification, not as a long flag.
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 typeDesc - A short, human readable description of the
88  * type that this object expects. This is used in the generation
89  * of the USAGE statement. The goal is to be helpful to the end user
90  * of the program.
91  * \param parser - A CmdLine parser object to add this Arg to
92  * \param ignoreable - Whether or not this argument can be ignored
93  * using the "--" flag.
94  * \param v - An optional visitor. You probably should not
95  * use this unless you have a very good reason.
96  */
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 
105  /**
106  * Constructor.
107  * \param name - The name of the Arg. Note that this is used for
108  * identification, not as a long flag.
109  * \param desc - A description of what the argument is for or
110  * does.
111  * \param req - Whether the argument is required on the command
112  * line.
113  * \param constraint - A pointer to a Constraint object used
114  * to constrain this Arg.
115  * \param ignoreable - Whether or not this argument can be ignored
116  * using the "--" flag.
117  * \param v - An optional visitor. You probably should not
118  * use this unless you have a very good reason.
119  */
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 
127  /**
128  * Constructor.
129  * \param name - The name of the Arg. Note that this is used for
130  * identification, not as a long flag.
131  * \param desc - A description of what the argument is for or
132  * does.
133  * \param req - Whether the argument is required on the command
134  * line.
135  * \param constraint - A pointer to a Constraint object used
136  * to constrain this Arg.
137  * \param parser - A CmdLine parser object to add this Arg to
138  * \param ignoreable - Whether or not this argument can be ignored
139  * using the "--" flag.
140  * \param v - An optional visitor. You probably should not
141  * use this unless you have a very good reason.
142  */
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 
151  /**
152  * Handles the processing of the argument.
153  * This re-implements the Arg version of this method to set the
154  * _value of the argument appropriately. It knows the difference
155  * between labeled and unlabeled.
156  * \param i - Pointer the the current argument in the list.
157  * \param args - Mutable list of strings. Passed from main().
158  */
159  virtual bool processArg(int* i, std::vector<std::string>& args);
160 
161  /**
162  * Returns the a short id string. Used in the usage.
163  * \param val - value to be used.
164  */
165  virtual std::string shortID(const std::string& val="val") const;
166 
167  /**
168  * Returns the a long id string. Used in the usage.
169  * \param val - value to be used.
170  */
171  virtual std::string longID(const std::string& val="val") const;
172 
173  /**
174  * Opertor ==.
175  * \param a - The Arg to be compared to this.
176  */
177  virtual bool operator==(const Arg& a) const;
178 
179  /**
180  * Pushes this to back of list rather than front.
181  * \param argList - The list this should be added to.
182  */
183  virtual void addToList( std::list<Arg*>& argList ) const;
184 };
185 
186 template<class T>
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>
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>
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>
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  std::string id = "<" + _typeDesc + "> ...";
274 
275  return id;
276 }
277 
278 template<class T>
279 std::string UnlabeledMultiArg<T>::longID(const std::string& val) const
280 {
281  std::string id = "<" + _typeDesc + "> (accepted multiple times)";
282 
283  return id;
284 }
285 
286 template<class T>
288 {
289  if ( _name == a.getName() || _description == a.getDescription() )
290  return true;
291  else
292  return false;
293 }
294 
295 template<class T>
296 void UnlabeledMultiArg<T>::addToList( std::list<Arg*>& argList ) const
297 {
298  argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
299 }
300 
301 }
302 
303 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013