tclap  1.4.0
UnlabeledValueArg.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 /******************************************************************************
4  *
5  * file: UnlabeledValueArg.h
6  *
7  * Copyright (c) 2003, Michael E. Smoot .
8  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
9  * Copyright (c) 2017, Google LLC
10  * All rights reserved.
11  *
12  * See the file COPYING in the top directory of this distribution for
13  * more information.
14  *
15  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  *****************************************************************************/
24 
25 #ifndef TCLAP_UNLABELED_VALUE_ARG_H
26 #define TCLAP_UNLABELED_VALUE_ARG_H
27 
29 #include <tclap/ValueArg.h>
30 
31 #include <list>
32 #include <string>
33 #include <vector>
34 
35 namespace TCLAP {
36 
43 template <class T>
44 class UnlabeledValueArg : public ValueArg<T> {
45  // If compiler has two stage name lookup (as gcc >= 3.4 does)
46  // this is required to prevent undef. symbols
51  using ValueArg<T>::_name;
54  using ValueArg<T>::_setBy;
56 
57 public:
80  UnlabeledValueArg(const std::string &name, const std::string &desc,
81  bool req, T value, const std::string &typeDesc,
82  bool ignoreable = false, Visitor *v = NULL);
83 
107  UnlabeledValueArg(const std::string &name, const std::string &desc,
108  bool req, T value, const std::string &typeDesc,
109  CmdLineInterface &parser, bool ignoreable = false,
110  Visitor *v = NULL);
111 
132  UnlabeledValueArg(const std::string &name, const std::string &desc,
133  bool req, T value, Constraint<T> *constraint,
134  bool ignoreable = false, Visitor *v = NULL);
135 
157  UnlabeledValueArg(const std::string &name, const std::string &desc,
158  bool req, T value, Constraint<T> *constraint,
159  CmdLineInterface &parser, bool ignoreable = false,
160  Visitor *v = NULL);
161 
170  virtual bool processArg(int *i, std::vector<std::string> &args);
171 
175  virtual std::string shortID(const std::string &) const {
176  return Arg::getName();
177  }
178 
182  virtual std::string longID(const std::string &) const {
183  return Arg::getName() + " <" + _typeDesc + ">";
184  }
185 
189  virtual bool operator==(const Arg &a) const;
190 
195  virtual void addToList(std::list<Arg *> &argList) const;
196 
197  virtual bool hasLabel() const { return false; }
198 };
199 
203 template <class T>
205  const std::string &desc, bool req,
206  T val, const std::string &typeDesc,
207  bool ignoreable, Visitor *v)
208  : ValueArg<T>("", name, desc, req, val, typeDesc, v) {
209  _ignoreable = ignoreable;
210 
212 }
213 
214 template <class T>
216  const std::string &desc, bool req,
217  T val, const std::string &typeDesc,
218  CmdLineInterface &parser,
219  bool ignoreable, Visitor *v)
220  : ValueArg<T>("", name, desc, req, val, typeDesc, v) {
221  _ignoreable = ignoreable;
223  parser.add(this);
224 }
225 
229 template <class T>
231  const std::string &desc, bool req,
232  T val, Constraint<T> *constraint,
233  bool ignoreable, Visitor *v)
234  : ValueArg<T>("", name, desc, req, val, constraint, v) {
235  _ignoreable = ignoreable;
237 }
238 
239 template <class T>
241  const std::string &desc, bool req,
242  T val, Constraint<T> *constraint,
243  CmdLineInterface &parser,
244  bool ignoreable, Visitor *v)
245  : ValueArg<T>("", name, desc, req, val, constraint, v) {
246  _ignoreable = ignoreable;
248  parser.add(this);
249 }
250 
254 template <class T>
255 bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string> &args) {
256  if (_alreadySet) return false;
257 
258  if (_hasBlanks(args[*i])) return false;
259 
260  // never ignore an unlabeled arg
261 
262  _extractValue(args[*i]);
263  _alreadySet = true;
264  _setBy = args[*i];
265  return true;
266 }
267 
271 template <class T>
272 bool UnlabeledValueArg<T>::operator==(const Arg &a) const {
273  if (_name == a.getName() || _description == a.getDescription())
274  return true;
275  else
276  return false;
277 }
278 
279 template <class T>
280 void UnlabeledValueArg<T>::addToList(std::list<Arg *> &argList) const {
281  argList.push_back(const_cast<Arg *>(static_cast<const Arg *const>(this)));
282 }
283 } // namespace TCLAP
284 
285 #endif // TCLAP_UNLABELED_VALUE_ARG_H
virtual bool hasLabel() const
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:122
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: ValueArg.h:342
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:53
static void check(bool req, const std::string &argName)
The basic labeled argument that parses a value.
Definition: ValueArg.h:45
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
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.
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:41
virtual bool operator==(const Arg &a) const
Overrides operator== for specific behavior.
virtual std::string shortID(const std::string &) const
Overrides shortID for specific behavior.
A base class that defines the interface for visitors.
Definition: Visitor.h:32
const std::string & getName() const
Returns the argument name.
Definition: Arg.h:525
std::string _description
Description of the argument.
Definition: Arg.h:97
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:543
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:139
std::string _typeDesc
A human readable description of the type to be parsed.
Definition: ValueArg.h:67
std::string getDescription() const
Returns the argument description.
Definition: Arg.h:262
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:92
Definition: Arg.h:46
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:577
std::string _setBy
Indicates the value specified to set this flag (like -a or –all).
Definition: Arg.h:126
The basic unlabeled argument that parses a value.
virtual std::string longID(const std::string &) const
Overrides longID for specific behavior.