00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef TCLAP_SWITCH_ARG_H
00025 #define TCLAP_SWITCH_ARG_H
00026
00027 #include <string>
00028 #include <vector>
00029
00030 #include <tclap/Arg.h>
00031
00032 namespace TCLAP {
00033
00039 class SwitchArg : public Arg
00040 {
00041 protected:
00042
00046 bool _value;
00047
00048 public:
00049
00062 SwitchArg(const std::string& flag,
00063 const std::string& name,
00064 const std::string& desc,
00065 bool def = false,
00066 Visitor* v = NULL);
00067
00068
00082 SwitchArg(const std::string& flag,
00083 const std::string& name,
00084 const std::string& desc,
00085 CmdLineInterface& parser,
00086 bool def = false,
00087 Visitor* v = NULL);
00088
00089
00098 virtual bool processArg(int* i, std::vector<std::string>& args);
00099
00104 bool combinedSwitchesMatch(std::string& combined);
00105
00109 bool getValue();
00110
00111 };
00112
00114
00116 inline SwitchArg::SwitchArg(const std::string& flag,
00117 const std::string& name,
00118 const std::string& desc,
00119 bool _default,
00120 Visitor* v )
00121 : Arg(flag, name, desc, false, false, v),
00122 _value( _default )
00123 { }
00124
00125 inline SwitchArg::SwitchArg(const std::string& flag,
00126 const std::string& name,
00127 const std::string& desc,
00128 CmdLineInterface& parser,
00129 bool _default,
00130 Visitor* v )
00131 : Arg(flag, name, desc, false, false, v),
00132 _value( _default )
00133 {
00134 parser.add( this );
00135 }
00136
00137 inline bool SwitchArg::getValue() { return _value; }
00138
00139 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
00140 {
00141
00142 if ( combinedSwitches[0] != Arg::flagStartString()[0] )
00143 return false;
00144
00145
00146 if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
00147 Arg::nameStartString() )
00148 return false;
00149
00150
00151
00152 for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
00153 if ( combinedSwitches[i] == _flag[0] )
00154 {
00155
00156
00157
00158
00159 combinedSwitches[i] = Arg::blankChar();
00160 return true;
00161 }
00162
00163
00164 return false;
00165 }
00166
00167
00168 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
00169 {
00170 if ( _ignoreable && Arg::ignoreRest() )
00171 return false;
00172
00173 if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
00174 {
00175
00176
00177
00178 bool ret = false;
00179 if ( argMatches( args[*i] ) )
00180 ret = true;
00181
00182 if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) )
00183 throw(CmdLineParseException("Argument already set!", toString()));
00184
00185 _alreadySet = true;
00186
00187 if ( _value == true )
00188 _value = false;
00189 else
00190 _value = true;
00191
00192 _checkWithVisitor();
00193
00194 return ret;
00195 }
00196 else
00197 return false;
00198 }
00199
00201
00203
00204 }
00205
00206 #endif