switch_arg.hpp
Go to the documentation of this file.
00001 
00011 /*****************************************************************************
00012 ** Ifdefs
00013 *****************************************************************************/
00014 
00015 #ifndef TCLAP_SWITCH_ARG_H
00016 #define TCLAP_SWITCH_ARG_H
00017 
00018 #include <string>
00019 #include <vector>
00020 
00021 #include "arg.hpp"
00022 
00023 namespace ecl {
00024 
00025 
00031 class SwitchArg : public Arg
00032 {
00033         protected:
00034 
00038                 bool _value;
00039 
00040         public:
00041 
00054                 SwitchArg(const std::string& flag,
00055                               const std::string& name,
00056                               const std::string& desc,
00057                               bool def = false,
00058                                   Visitor* v = NULL);
00059 
00060 
00074                 SwitchArg(const std::string& flag,
00075                               const std::string& name,
00076                               const std::string& desc,
00077                                   CmdLineInterface& parser,
00078                               bool def = false,
00079                                   Visitor* v = NULL);
00080 
00081 
00090                 virtual bool processArg(int* i, std::vector<std::string>& args);
00091 
00096                 bool combinedSwitchesMatch(std::string& combined);
00097 
00101                 bool getValue();
00102 
00103 };
00104 
00106 //BEGIN SwitchArg.cpp
00108 inline SwitchArg::SwitchArg(const std::string& flag,
00109                                  const std::string& name,
00110                                  const std::string& desc,
00111                          bool _default,
00112                                          Visitor* v )
00113 : Arg(flag, name, desc, false, false, v),
00114   _value( _default )
00115 { }
00116 
00117 inline SwitchArg::SwitchArg(const std::string& flag,
00118                                         const std::string& name,
00119                                         const std::string& desc,
00120                                         CmdLineInterface& parser,
00121                                         bool _default,
00122                                         Visitor* v )
00123 : Arg(flag, name, desc, false, false, v),
00124   _value( _default )
00125 {
00126         parser.add( this );
00127 }
00128 
00129 inline bool SwitchArg::getValue() { return _value; }
00130 
00131 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
00132 {
00133         // make sure this is actually a combined switch
00134         if ( combinedSwitches[0] != Arg::flagStartString()[0] )
00135                 return false;
00136 
00137         // make sure it isn't a long name
00138         if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
00139                  Arg::nameStartString() )
00140                 return false;
00141 
00142         // ok, we're not specifying a ValueArg, so we know that we have
00143         // a combined switch list.
00144         for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
00145                 if ( combinedSwitches[i] == _flag[0] )
00146                 {
00147                         // update the combined switches so this one is no longer present
00148                         // this is necessary so that no unlabeled args are matched
00149                         // later in the processing.
00150                         //combinedSwitches.erase(i,1);
00151                         combinedSwitches[i] = Arg::blankChar();
00152                         return true;
00153                 }
00154 
00155         // none of the switches passed in the list match.
00156         return false;
00157 }
00158 
00159 
00160 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
00161 {
00162         if ( _ignoreable && Arg::ignoreRest() )
00163                 return false;
00164 
00165         if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
00166         {
00167                 // If we match on a combined switch, then we want to return false
00168                 // so that other switches in the combination will also have a
00169                 // chance to match.
00170                 bool ret = false;
00171                 if ( argMatches( args[*i] ) )
00172                         ret = true;
00173 
00174                 if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) )
00175                         throw(CmdLineParseException("Argument already set!", toString()));
00176 
00177                 _alreadySet = true;
00178 
00179                 if ( _value == true )
00180                         _value = false;
00181                 else
00182                         _value = true;
00183 
00184                 _checkWithVisitor();
00185 
00186                 return ret;
00187         }
00188         else
00189                 return false;
00190 }
00191 
00193 //End SwitchArg.cpp
00195 
00196 }; // namespace ecl
00197 
00198 
00199 #endif


ecl_command_line
Author(s): Daniel Stonier
autogenerated on Wed Aug 26 2015 11:27:24