switch_arg.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12 ** Ifdefs
13 *****************************************************************************/
14 
15 #ifndef TCLAP_SWITCH_ARG_H
16 #define TCLAP_SWITCH_ARG_H
17 
18 #include <string>
19 #include <vector>
20 
21 #include "arg.hpp"
22 
23 namespace ecl {
24 
25 
31 class SwitchArg : public Arg
32 {
33  protected:
34 
38  bool _value;
39 
40  public:
41 
54  SwitchArg(const std::string& flag,
55  const std::string& name,
56  const std::string& desc,
57  bool def = false,
58  Visitor* v = NULL);
59 
60 
74  SwitchArg(const std::string& flag,
75  const std::string& name,
76  const std::string& desc,
77  CmdLineInterface& parser,
78  bool def = false,
79  Visitor* v = NULL);
80 
81 
90  virtual bool processArg(int* i, std::vector<std::string>& args);
91 
96  bool combinedSwitchesMatch(std::string& combined);
97 
101  bool getValue();
102 
103 };
104 
106 //BEGIN SwitchArg.cpp
108 inline SwitchArg::SwitchArg(const std::string& flag,
109  const std::string& name,
110  const std::string& desc,
111  bool _default,
112  Visitor* v )
113 : Arg(flag, name, desc, false, false, v),
114  _value( _default )
115 { }
116 
117 inline SwitchArg::SwitchArg(const std::string& flag,
118  const std::string& name,
119  const std::string& desc,
120  CmdLineInterface& parser,
121  bool _default,
122  Visitor* v )
123 : Arg(flag, name, desc, false, false, v),
124  _value( _default )
125 {
126  parser.add( this );
127 }
128 
129 inline bool SwitchArg::getValue() { return _value; }
130 
131 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
132 {
133  // make sure this is actually a combined switch
134  if ( combinedSwitches[0] != Arg::flagStartString()[0] )
135  return false;
136 
137  // make sure it isn't a long name
138  if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
140  return false;
141 
142  // ok, we're not specifying a ValueArg, so we know that we have
143  // a combined switch list.
144  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
145  if ( combinedSwitches[i] == _flag[0] )
146  {
147  // update the combined switches so this one is no longer present
148  // this is necessary so that no unlabeled args are matched
149  // later in the processing.
150  //combinedSwitches.erase(i,1);
151  combinedSwitches[i] = Arg::blankChar();
152  return true;
153  }
154 
155  // none of the switches passed in the list match.
156  return false;
157 }
158 
159 
160 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
161 {
163  return false;
164 
165  if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
166  {
167  // If we match on a combined switch, then we want to return false
168  // so that other switches in the combination will also have a
169  // chance to match.
170  bool ret = false;
171  if ( argMatches( args[*i] ) )
172  ret = true;
173 
174  if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) )
175  throw(CmdLineParseException("Argument already set!", toString()));
176 
177  _alreadySet = true;
178 
179  if ( _value == true )
180  _value = false;
181  else
182  _value = true;
183 
185 
186  return ret;
187  }
188  else
189  return false;
190 }
191 
193 //End SwitchArg.cpp
195 
196 }; // namespace ecl
197 
198 
199 #endif
arg.hpp
TCLAP command line argument parser classes.
ecl::SwitchArg::_value
bool _value
Definition: switch_arg.hpp:42
ecl::Arg::_checkWithVisitor
void _checkWithVisitor() const
Definition: arg.hpp:507
ecl::Arg::_flag
std::string _flag
Definition: arg.hpp:66
ecl::SwitchArg::SwitchArg
SwitchArg(const std::string &flag, const std::string &name, const std::string &desc, bool def=false, Visitor *v=NULL)
Definition: switch_arg.hpp:110
ecl::SwitchArg::getValue
bool getValue()
Definition: switch_arg.hpp:131
ecl::CmdLineInterface::add
virtual void add(Arg &a)=0
ecl::Arg
Virtual parent for all the different argument classes.
Definition: arg.hpp:39
ecl::Arg::argMatches
virtual bool argMatches(const std::string &s) const
Definition: arg.hpp:486
ecl::Arg::blankChar
static char blankChar()
Definition: arg.hpp:186
ecl::Arg::_alreadySet
bool _alreadySet
Definition: arg.hpp:105
ecl::Visitor
TClap class indirectly used to define the interface for visitors.
Definition: visitor.hpp:27
ecl::CmdLineInterface
Managing interface for The base class that manages the command line definition and passes along the p...
Definition: cmd_line_interface.hpp:38
ecl::Arg::nameStartString
static std::string nameStartString()
Definition: arg.hpp:203
ecl::Arg::ignoreRest
static bool ignoreRest()
Definition: arg.hpp:173
ecl::Arg::toString
virtual std::string toString() const
Definition: arg.hpp:495
ecl::SwitchArg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)
Definition: switch_arg.hpp:162
ecl::Arg::flagStartString
static std::string flagStartString()
Definition: arg.hpp:197
ecl::Arg::_ignoreable
bool _ignoreable
Definition: arg.hpp:118
ecl::CmdLineParseException
Defines the exception when an argument is improperly specified.
Definition: arg_exception.hpp:143
ecl
ecl::SwitchArg::combinedSwitchesMatch
bool combinedSwitchesMatch(std::string &combined)
Definition: switch_arg.hpp:133


ecl_command_line
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:13