Main MRPT website > C++ reference for MRPT 1.3.2
SwitchArg.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2015, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 /******************************************************************************
11  *
12  * file: SwitchArg.h
13  *
14  * Copyright (c) 2003, Michael E. Smoot .
15  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
16  * All rights reverved.
17  *
18  * See the file COPYING in the top directory of this distribution for
19  * more information.
20  *
21  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  *
29  *****************************************************************************/
30 
31 
32 #ifndef TCLAP_SWITCH_ARG_H
33 #define TCLAP_SWITCH_ARG_H
34 
35 #include <string>
36 #include <vector>
37 
39 
40 namespace TCLAP {
41 
42 /**
43  * A simple switch argument. If the switch is set on the command line, then
44  * the getValue method will return the opposite of the default value for the
45  * switch.
46  */
47 class SwitchArg : public Arg
48 {
49  protected:
50 
51  /**
52  * The value of the switch.
53  */
54  bool _value;
55 
56  public:
57 
58  /**
59  * SwitchArg constructor.
60  * \param flag - The one character flag that identifies this
61  * argument on the command line.
62  * \param name - A one word name for the argument. Can be
63  * used as a long flag on the command line.
64  * \param desc - A description of what the argument is for or
65  * does.
66  * \param def - The default value for this Switch.
67  * \param v - An optional visitor. You probably should not
68  * use this unless you have a very good reason.
69  */
70  SwitchArg(const std::string& flag,
71  const std::string& name,
72  const std::string& desc,
73  bool def = false,
74  Visitor* v = NULL);
75 
76 
77  /**
78  * SwitchArg constructor.
79  * \param flag - The one character flag that identifies this
80  * argument on the command line.
81  * \param name - A one word name for the argument. Can be
82  * used as a long flag on the command line.
83  * \param desc - A description of what the argument is for or
84  * does.
85  * \param parser - A CmdLine parser object to add this Arg to
86  * \param def - The default value for this Switch.
87  * \param v - An optional visitor. You probably should not
88  * use this unless you have a very good reason.
89  */
90  SwitchArg(const std::string& flag,
91  const std::string& name,
92  const std::string& desc,
93  CmdLineInterface& parser,
94  bool def = false,
95  Visitor* v = NULL);
96 
97 
98  /**
99  * Handles the processing of the argument.
100  * This re-implements the Arg version of this method to set the
101  * _value of the argument appropriately.
102  * \param i - Pointer the the current argument in the list.
103  * \param args - Mutable list of strings. Passed
104  * in from main().
105  */
106  virtual bool processArg(int* i, std::vector<std::string>& args);
107 
108  /**
109  * Checks a string to see if any of the chars in the string
110  * match the flag for this Switch.
111  */
112  bool combinedSwitchesMatch(std::string& combined);
113 
114  /**
115  * Returns bool, whether or not the switch has been set.
116  */
117  bool getValue();
118 
119 };
120 
121 //////////////////////////////////////////////////////////////////////
122 //BEGIN SwitchArg.cpp
123 //////////////////////////////////////////////////////////////////////
124 inline SwitchArg::SwitchArg(const std::string& flag,
125  const std::string& name,
126  const std::string& desc,
127  bool _default,
128  Visitor* v )
129 : Arg(flag, name, desc, false, false, v),
130  _value( _default )
131 { }
132 
133 inline SwitchArg::SwitchArg(const std::string& flag,
134  const std::string& name,
135  const std::string& desc,
136  CmdLineInterface& parser,
137  bool _default,
138  Visitor* v )
139 : Arg(flag, name, desc, false, false, v),
140  _value( _default )
141 {
142  parser.add( this );
143 }
144 
145 inline bool SwitchArg::getValue() { return _value; }
146 
147 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
148 {
149  // make sure this is actually a combined switch
150  if ( combinedSwitches[0] != Arg::flagStartString()[0] )
151  return false;
152 
153  // make sure it isn't a long name
154  if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
156  return false;
157 
158  // ok, we're not specifying a ValueArg, so we know that we have
159  // a combined switch list.
160  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
161  if ( combinedSwitches[i] == _flag[0] )
162  {
163  // update the combined switches so this one is no longer present
164  // this is necessary so that no unlabeled args are matched
165  // later in the processing.
166  //combinedSwitches.erase(i,1);
167  combinedSwitches[i] = Arg::blankChar();
168  return true;
169  }
170 
171  // none of the switches passed in the list match.
172  return false;
173 }
174 
175 
176 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
177 {
178  if ( _ignoreable && Arg::ignoreRest() )
179  return false;
180 
181  if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
182  {
183  // If we match on a combined switch, then we want to return false
184  // so that other switches in the combination will also have a
185  // chance to match.
186  bool ret = false;
187  if ( argMatches( args[*i] ) )
188  ret = true;
189 
190  if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) )
191  throw(CmdLineParseException("Argument already set!", toString()));
192 
193  _alreadySet = true;
194 
195  if ( _value == true )
196  _value = false;
197  else
198  _value = true;
199 
201 
202  return ret;
203  }
204  else
205  return false;
206 }
207 
208 //////////////////////////////////////////////////////////////////////
209 //End SwitchArg.cpp
210 //////////////////////////////////////////////////////////////////////
211 
212 } //namespace TCLAP
213 
214 #endif
bool _value
The value of the switch.
Definition: SwitchArg.h:54
void _checkWithVisitor() const
Performs the special handling described by the Vistitor.
Definition: Arg.h:519
Definition: Arg.h:44
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:498
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:51
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
A simple switch argument.
Definition: SwitchArg.h:47
static const std::string nameStartString()
The sting that indicates the beginning of a name.
Definition: Arg.h:213
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:115
static const std::string flagStartString()
The sting that indicates the beginning of a flag.
Definition: Arg.h:207
Thrown from CmdLine when the arguments on the command line are not properly specified, e.g.
Definition: ArgException.h:151
A base class that defines the interface for visitors.
Definition: Visitor.h:39
std::string _flag
The single char flag used to identify the argument.
Definition: Arg.h:76
bool combinedSwitchesMatch(std::string &combined)
Checks a string to see if any of the chars in the string match the flag for this Switch.
Definition: SwitchArg.h:147
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:128
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:507
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:183
bool getValue()
Returns bool, whether or not the switch has been set.
Definition: SwitchArg.h:145
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: SwitchArg.h:176
SwitchArg(const std::string &flag, const std::string &name, const std::string &desc, bool def=false, Visitor *v=NULL)
SwitchArg constructor.
Definition: SwitchArg.h:124
static char blankChar()
The char used as a place holder when SwitchArgs are combined.
Definition: Arg.h:196
The base class that manages the command line definition and passes along the parsing to the appropria...



Page generated by Doxygen 1.8.12 for MRPT 1.3.2 SVN: at Thu Nov 10 13:46:27 UTC 2016