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
00052 bool _default;
00053
00054 public:
00055
00068 SwitchArg(const std::string& flag,
00069 const std::string& name,
00070 const std::string& desc,
00071 bool def = false,
00072 Visitor* v = NULL);
00073
00074
00088 SwitchArg(const std::string& flag,
00089 const std::string& name,
00090 const std::string& desc,
00091 CmdLineInterface& parser,
00092 bool def = false,
00093 Visitor* v = NULL);
00094
00095
00104 virtual bool processArg(int* i, std::vector<std::string>& args);
00105
00110 bool combinedSwitchesMatch(std::string& combined);
00111
00115 bool getValue();
00116
00117 virtual void reset();
00118
00119 };
00120
00122
00124 inline SwitchArg::SwitchArg(const std::string& flag,
00125 const std::string& name,
00126 const std::string& desc,
00127 bool default_val,
00128 Visitor* v )
00129 : Arg(flag, name, desc, false, false, v),
00130 _value( default_val ),
00131 _default( default_val )
00132 { }
00133
00134 inline SwitchArg::SwitchArg(const std::string& flag,
00135 const std::string& name,
00136 const std::string& desc,
00137 CmdLineInterface& parser,
00138 bool default_val,
00139 Visitor* v )
00140 : Arg(flag, name, desc, false, false, v),
00141 _value( default_val ),
00142 _default(default_val)
00143 {
00144 parser.add( this );
00145 }
00146
00147 inline bool SwitchArg::getValue() { return _value; }
00148
00149 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
00150 {
00151
00152 if ( combinedSwitches.length() > 0 &&
00153 combinedSwitches[0] != Arg::flagStartString()[0] )
00154 return false;
00155
00156
00157 if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
00158 Arg::nameStartString() )
00159 return false;
00160
00161
00162 if ( combinedSwitches.find_first_of( Arg::delimiter() ) != std::string::npos )
00163 return false;
00164
00165
00166
00167 for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
00168 if ( _flag.length() > 0 &&
00169 combinedSwitches[i] == _flag[0] &&
00170 _flag[0] != Arg::flagStartString()[0] )
00171 {
00172
00173
00174
00175
00176 combinedSwitches[i] = Arg::blankChar();
00177 return true;
00178 }
00179
00180
00181 return false;
00182 }
00183
00184
00185 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
00186 {
00187 if ( _ignoreable && Arg::ignoreRest() )
00188 return false;
00189
00190 if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
00191 {
00192
00193
00194
00195 bool ret = false;
00196 if ( argMatches( args[*i] ) )
00197 ret = true;
00198
00199 if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) )
00200 throw(CmdLineParseException("Argument already set!", toString()));
00201
00202 _alreadySet = true;
00203
00204 if ( _value == true )
00205 _value = false;
00206 else
00207 _value = true;
00208
00209 _checkWithVisitor();
00210
00211 return ret;
00212 }
00213 else
00214 return false;
00215 }
00216
00217 inline void SwitchArg::reset()
00218 {
00219 Arg::reset();
00220 _value = _default;
00221 }
00223
00225
00226 }
00227
00228 #endif