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 
00049      public:
00050 
00063           SwitchArg(const std::string& flag, 
00064                      const std::string& name, 
00065                      const std::string& desc,
00066                      bool def,
00067                       Visitor* v = NULL);
00068 
00069                       
00083           SwitchArg(const std::string& flag, 
00084                      const std::string& name, 
00085                      const std::string& desc,
00086                      bool def,
00087                       CmdLineInterface& parser,
00088                       Visitor* v = NULL);
00089                       
00090                       
00099           virtual bool processArg(int* i, std::vector<std::string>& args); 
00100 
00105           bool combinedSwitchesMatch(std::string& combined);
00106 
00110           bool getValue();
00111 };
00112 
00114 //BEGIN SwitchArg.cpp
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                          bool _default,
00129                          CmdLineInterface& parser,
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      // make sure this is actually a combined switch
00142      if ( combinedSwitches[0] != Arg::flagStartString()[0] )
00143           return false;
00144 
00145      // make sure it isn't a long name 
00146      if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) == 
00147            Arg::nameStartString() )
00148           return false;
00149 
00150      // ok, we're not specifying a ValueArg, so we know that we have
00151      // a combined switch list.  
00152      for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
00153           if ( combinedSwitches[i] == _flag[0] ) 
00154           {
00155                // update the combined switches so this one is no longer present
00156                // this is necessary so that no unlabeled args are matched
00157                // later in the processing.
00158                //combinedSwitches.erase(i,1);
00159                combinedSwitches[i] = Arg::blankChar(); 
00160                return true;
00161           }
00162 
00163      // none of the switches passed in the list match. 
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           // If we match on a combined switch, then we want to return false
00176           // so that other switches in the combination will also have a
00177           // chance to match.
00178           bool ret = false;
00179           if ( argMatches( args[*i] ) )
00180                ret = true;
00181 
00182           if ( _alreadySet )
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 //End SwitchArg.cpp
00203 
00204 } //namespace TCLAP
00205 
00206 #endif

Generated on Sun Mar 11 15:33:24 2007 for MetalWarriors by  doxygen 1.4.7