SwitchArg.h
Go to the documentation of this file.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
00114 virtual std::string getValueAsString()const;
00115
00116 };
00117
00119
00121 inline SwitchArg::SwitchArg(const std::string& flag,
00122 const std::string& name,
00123 const std::string& desc,
00124 bool _default,
00125 Visitor* v )
00126 : Arg(flag, name, desc, false, false, v),
00127 _value( _default )
00128 { }
00129
00130 inline SwitchArg::SwitchArg(const std::string& flag,
00131 const std::string& name,
00132 const std::string& desc,
00133 CmdLineInterface& parser,
00134 bool _default,
00135 Visitor* v )
00136 : Arg(flag, name, desc, false, false, v),
00137 _value( _default )
00138 {
00139 parser.add( this );
00140 }
00141
00142 inline bool SwitchArg::getValue() { return _value; }
00143
00144 inline std::string SwitchArg::getValueAsString()const
00145 {
00146 std::string res;
00147 if( _value )
00148 {
00149 res = "1";
00150 }
00151 else
00152 {
00153 res = "0";
00154 }
00155 return res;
00156 }
00157
00158 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
00159 {
00160
00161 if ( combinedSwitches[0] != Arg::flagStartString()[0] )
00162 return false;
00163
00164
00165 if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
00166 Arg::nameStartString() )
00167 return false;
00168
00169
00170
00171 if (_flag.length() > 0)
00172 {
00173 for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
00174 if ( combinedSwitches[i] == _flag[0] )
00175 {
00176
00177
00178
00179
00180 combinedSwitches[i] = Arg::blankChar();
00181 return true;
00182 }
00183 }
00184
00185 return false;
00186 }
00187
00188
00189 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
00190 {
00191 if ( _ignoreable && Arg::ignoreRest() )
00192 return false;
00193
00194 if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
00195 {
00196
00197
00198
00199 bool ret = false;
00200 if ( argMatches( args[*i] ) )
00201 ret = true;
00202
00203 if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) )
00204 throw(CmdLineParseException("Argument already set!", toString()));
00205
00206 _alreadySet = true;
00207
00208 if ( _value == true )
00209 _value = false;
00210 else
00211 _value = true;
00212
00213 _checkWithVisitor();
00214
00215 return ret;
00216 }
00217 else
00218 return false;
00219 }
00220
00222
00224
00225 }
00226
00227 #endif