Main MRPT website > C++ reference
MRPT logo
SwitchArg.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * file: SwitchArg.h
5  *
6  * Copyright (c) 2003, Michael E. Smoot .
7  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
8  * All rights reverved.
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_SWITCH_ARG_H
25 #define TCLAP_SWITCH_ARG_H
26 
27 #include <string>
28 #include <vector>
29 
31 
32 namespace TCLAP {
33 
34 /**
35  * A simple switch argument. If the switch is set on the command line, then
36  * the getValue method will return the opposite of the default value for the
37  * switch.
38  */
39 class SwitchArg : public Arg
40 {
41  protected:
42 
43  /**
44  * The value of the switch.
45  */
46  bool _value;
47 
48  public:
49 
50  /**
51  * SwitchArg constructor.
52  * \param flag - The one character flag that identifies this
53  * argument on the command line.
54  * \param name - A one word name for the argument. Can be
55  * used as a long flag on the command line.
56  * \param desc - A description of what the argument is for or
57  * does.
58  * \param def - The default value for this Switch.
59  * \param v - An optional visitor. You probably should not
60  * use this unless you have a very good reason.
61  */
62  SwitchArg(const std::string& flag,
63  const std::string& name,
64  const std::string& desc,
65  bool def = false,
66  Visitor* v = NULL);
67 
68 
69  /**
70  * SwitchArg constructor.
71  * \param flag - The one character flag that identifies this
72  * argument on the command line.
73  * \param name - A one word name for the argument. Can be
74  * used as a long flag on the command line.
75  * \param desc - A description of what the argument is for or
76  * does.
77  * \param parser - A CmdLine parser object to add this Arg to
78  * \param def - The default value for this Switch.
79  * \param v - An optional visitor. You probably should not
80  * use this unless you have a very good reason.
81  */
82  SwitchArg(const std::string& flag,
83  const std::string& name,
84  const std::string& desc,
85  CmdLineInterface& parser,
86  bool def = false,
87  Visitor* v = NULL);
88 
89 
90  /**
91  * Handles the processing of the argument.
92  * This re-implements the Arg version of this method to set the
93  * _value of the argument appropriately.
94  * \param i - Pointer the the current argument in the list.
95  * \param args - Mutable list of strings. Passed
96  * in from main().
97  */
98  virtual bool processArg(int* i, std::vector<std::string>& args);
99 
100  /**
101  * Checks a string to see if any of the chars in the string
102  * match the flag for this Switch.
103  */
104  bool combinedSwitchesMatch(std::string& combined);
105 
106  /**
107  * Returns bool, whether or not the switch has been set.
108  */
109  bool getValue();
110 
111 };
112 
113 //////////////////////////////////////////////////////////////////////
114 //BEGIN SwitchArg.cpp
115 //////////////////////////////////////////////////////////////////////
116 inline SwitchArg::SwitchArg(const std::string& flag,
117  const std::string& name,
118  const std::string& desc,
119  bool _default,
120  Visitor* v )
121 : Arg(flag, name, desc, false, false, v),
122  _value( _default )
123 { }
124 
125 inline SwitchArg::SwitchArg(const std::string& flag,
126  const std::string& name,
127  const std::string& desc,
128  CmdLineInterface& parser,
129  bool _default,
130  Visitor* v )
131 : Arg(flag, name, desc, false, false, v),
132  _value( _default )
133 {
134  parser.add( this );
135 }
136 
137 inline bool SwitchArg::getValue() { return _value; }
138 
139 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
140 {
141  // make sure this is actually a combined switch
142  if ( combinedSwitches[0] != Arg::flagStartString()[0] )
143  return false;
144 
145  // make sure it isn't a long name
146  if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
148  return false;
149 
150  // ok, we're not specifying a ValueArg, so we know that we have
151  // a combined switch list.
152  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
153  if ( combinedSwitches[i] == _flag[0] )
154  {
155  // update the combined switches so this one is no longer present
156  // this is necessary so that no unlabeled args are matched
157  // later in the processing.
158  //combinedSwitches.erase(i,1);
159  combinedSwitches[i] = Arg::blankChar();
160  return true;
161  }
162 
163  // none of the switches passed in the list match.
164  return false;
165 }
166 
167 
168 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
169 {
170  if ( _ignoreable && Arg::ignoreRest() )
171  return false;
172 
173  if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
174  {
175  // If we match on a combined switch, then we want to return false
176  // so that other switches in the combination will also have a
177  // chance to match.
178  bool ret = false;
179  if ( argMatches( args[*i] ) )
180  ret = true;
181 
182  if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) )
183  throw(CmdLineParseException("Argument already set!", toString()));
184 
185  _alreadySet = true;
186 
187  if ( _value == true )
188  _value = false;
189  else
190  _value = true;
191 
193 
194  return ret;
195  }
196  else
197  return false;
198 }
199 
200 //////////////////////////////////////////////////////////////////////
201 //End SwitchArg.cpp
202 //////////////////////////////////////////////////////////////////////
203 
204 } //namespace TCLAP
205 
206 #endif



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