VR-Link API Documentation for DIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmdSwitchArg.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 DtCmdLine_SWITCH_ARG_H
25 #define DtCmdLine_SWITCH_ARG_H
26 
30 
31 #include <string>
32 #include <vector>
33 
34 #include "cmdArg.h"
35 
36 #ifdef DtUSE_UTILITIES_NAMESPACE
37 namespace DtUSE_UTILITIES_NAMESPACE
38 {
39 #endif
40 
41  namespace DtCmdLine {
42 
48  class SwitchArg : public Arg
50  {
51  protected:
52 
56  bool _value;
57 
61  bool myDefault;
62 
63  public:
64 
77  SwitchArg(const std::string& flag,
78  const std::string& name,
79  const std::string& desc,
80  bool def,
81  Visitor* v = NULL);
82 
83 
97  SwitchArg(const std::string& flag,
98  const std::string& name,
99  const std::string& desc,
100  bool def,
101  CmdLineInterface& parser,
102  Visitor* v = NULL);
103 
104 
113  virtual bool processArg(int* i, std::vector<std::string>& args);
114 
119  bool combinedSwitchesMatch(std::string& combined);
120 
124  bool getValue() const;
125 
129  bool getDefault() const;
130 
133  operator bool();
134 
135  };
136 
138  //BEGIN SwitchArg.cpp
140  inline SwitchArg::SwitchArg(const std::string& flag,
141  const std::string& name,
142  const std::string& desc,
143  bool _default,
144  Visitor* v )
145  : Arg(flag, name, desc, false, false, v)
146  , _value( _default )
147  , myDefault( _default )
148  { }
149 
150  inline SwitchArg::SwitchArg(const std::string& flag,
151  const std::string& name,
152  const std::string& desc,
153  bool _default,
154  CmdLineInterface& parser,
155  Visitor* v )
156  : Arg(flag, name, desc, false, false, v)
157  , _value( _default )
158  , myDefault( _default )
159  {
160  parser.add( this );
161  }
162 
163  inline bool SwitchArg::getValue() const { return _value; }
164 
165  inline bool SwitchArg::getDefault() const { return myDefault; }
166 
167  inline SwitchArg::operator bool() { return _value; }
168 
169  inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
170  {
172  if ( combinedSwitches[0] != Arg::flagStartString()[0] )
173  return false;
174 
176  if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
178  return false;
179 
182  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
183  if ( !_flag.empty() && combinedSwitches[i] == _flag[0] )
184  {
188  //combinedSwitches.erase(i,1);
189  combinedSwitches[i] = Arg::blankChar();
190  return true;
191  }
192 
194  return false;
195  }
196 
197 
198  inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
199  {
200  if ( _ignoreable && Arg::ignoreRest() )
201  return false;
202 
203  if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
204  {
208  bool ret = false;
209  if ( argMatches( args[*i] ) )
210  ret = true;
211 
212  if ( _alreadySet )
213  throw(CmdLineParseException("Argument already set!", toString()));
214 
215  _alreadySet = true;
216 
217  if ( myDefault == true )
218  _value = false;
219  else
220  _value = true;
221 
223 
224  return ret;
225  }
226  else
227  return false;
228  }
229 
231  //End SwitchArg.cpp
233 
234  } //namespace DtCmdLine
235 
236 #ifdef DtUSE_UTILITIES_NAMESPACE
237 }
238 #endif
239 
240 #endif
A base class that defines the interface for visitors.
Definition: cmdVisitor.h:41
static const char blankChar()
The char used as a place holder when SwitchArgs are combined.
Definition: cmdArg.h:191
A simple switch argument.
Definition: cmdSwitchArg.h:49
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: cmdSwitchArg.h:169
bool getDefault() const
Returns bool, the default value.
Definition: cmdSwitchArg.h:165
This file declares classes for VR-Link command line parsing utility.
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: cmdArg.h:125
bool _value
The value of the switch.
Definition: cmdSwitchArg.h:56
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: cmdSwitchArg.h:198
SwitchArg(const std::string &flag, const std::string &name, const std::string &desc, bool def, Visitor *v=NULL)
SwitchArg constructor.
Definition: cmdSwitchArg.h:140
bool myDefault
The default value of the switch.
Definition: cmdSwitchArg.h:61
bool _alreadySet
Indicates whether the argument has been set.
Definition: cmdArg.h:112
static const std::string flagStartString()
The sting that indicates the beginning of a flag.
Definition: cmdArg.h:202
Thrown from CmdLine when the arguments on the command line are not properly specified, e.g.
Definition: cmdArgException.h:153
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: cmdArg.h:503
static const std::string nameStartString()
The sting that indicates the beginning of a name.
Definition: cmdArg.h:208
static bool ignoreRest()
Whether to ignore the rest.
Definition: cmdArg.h:178
A virtual base class that defines the essential data for all arguments.
Definition: cmdArg.h:48
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
bool getValue() const
Returns bool, whether or not the switch has been set.
Definition: cmdSwitchArg.h:163
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: cmdLineInterface.h:52
std::string _flag
The single char flag used to identify the argument.
Definition: cmdArg.h:73
void _checkWithVisitor() const
Performs the special handling described by the Vistitor.
Definition: cmdArg.h:515
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: cmdArg.h:494

Document ID: Generated on Mon Apr 25 03:39:01 EDT 2022 from SVN revision 242502
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)