SwitchArg.h

Go to the documentation of this file.
00001 
00002 /****************************************************************************** 
00003  * 
00004  *  file:  SwitchArg.h
00005  * 
00006  *  Copyright (c) 2003, Michael E. Smoot .
00007  *  Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
00008  *  All rights reverved.
00009  * 
00010  *  See the file COPYING in the top directory of this distribution for
00011  *  more information.
00012  *  
00013  *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 
00014  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
00015  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
00016  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
00017  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
00018  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
00019  *  DEALINGS IN THE SOFTWARE.  
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 //BEGIN SwitchArg.cpp
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   // make sure this is actually a combined switch
00161   if ( combinedSwitches[0] != Arg::flagStartString()[0] )
00162     return false;
00163 
00164   // make sure it isn't a long name 
00165   if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) == 
00166        Arg::nameStartString() )
00167     return false;
00168 
00169   // ok, we're not specifying a ValueArg, so we know that we have
00170   // a combined switch list.  
00171   if (_flag.length() > 0)
00172     {
00173     for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
00174       if ( combinedSwitches[i] == _flag[0] ) 
00175         {
00176         // update the combined switches so this one is no longer present
00177         // this is necessary so that no unlabeled args are matched
00178         // later in the processing.
00179         //combinedSwitches.erase(i,1);
00180         combinedSwitches[i] = Arg::blankChar(); 
00181         return true;
00182         }
00183     }
00184   // none of the switches passed in the list match. 
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     // If we match on a combined switch, then we want to return false
00197     // so that other switches in the combination will also have a
00198     // chance to match.
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 //End SwitchArg.cpp
00224 
00225 } //namespace TCLAP
00226 
00227 #endif

Generated on 6 Apr 2011 for Slicer3 by  doxygen 1.6.1