2
/******************************************************************************
6
* Copyright (c) 2003, Michael E. Smoot .
7
* Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
10
* See the file COPYING in the top directory of this distribution for
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.
21
*****************************************************************************/
24
#ifndef TCLAP_SWITCH_ARG_H
25
#define TCLAP_SWITCH_ARG_H
30
#include <tclap/Arg.h>
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
39
class SwitchArg : public Arg
44
* The value of the switch.
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
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.
62
SwitchArg(const std::string& flag,
63
const std::string& name,
64
const std::string& desc,
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
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.
82
SwitchArg(const std::string& flag,
83
const std::string& name,
84
const std::string& desc,
85
CmdLineInterface& parser,
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
98
virtual bool processArg(int* i, std::vector<std::string>& args);
101
* Checks a string to see if any of the chars in the string
102
* match the flag for this Switch.
104
bool combinedSwitchesMatch(std::string& combined);
107
* Returns bool, whether or not the switch has been set.
113
//////////////////////////////////////////////////////////////////////
114
//BEGIN SwitchArg.cpp
115
//////////////////////////////////////////////////////////////////////
116
inline SwitchArg::SwitchArg(const std::string& flag,
117
const std::string& name,
118
const std::string& desc,
121
: Arg(flag, name, desc, false, false, v),
125
inline SwitchArg::SwitchArg(const std::string& flag,
126
const std::string& name,
127
const std::string& desc,
128
CmdLineInterface& parser,
131
: Arg(flag, name, desc, false, false, v),
137
inline bool SwitchArg::getValue() { return _value; }
139
inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
141
// make sure this is actually a combined switch
142
if ( combinedSwitches[0] != Arg::flagStartString()[0] )
145
// make sure it isn't a long name
146
if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
147
Arg::nameStartString() )
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] )
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();
163
// none of the switches passed in the list match.
168
inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
170
if ( _ignoreable && Arg::ignoreRest() )
173
if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
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
179
if ( argMatches( args[*i] ) )
182
if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) )
183
throw(CmdLineParseException("Argument already set!", toString()));
187
if ( _value == true )
200
//////////////////////////////////////////////////////////////////////
202
//////////////////////////////////////////////////////////////////////