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() ) ==
139  Arg::nameStartString() )
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 {
162  if ( _ignoreable && Arg::ignoreRest() )
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 
184  _checkWithVisitor();
185 
186  return ret;
187  }
188  else
189  return false;
190 }
191 
193 //End SwitchArg.cpp
195 
196 }; // namespace ecl
197 
198 
199 #endif
SwitchArg(const std::string &flag, const std::string &name, const std::string &desc, bool def=false, Visitor *v=NULL)
Definition: switch_arg.hpp:108
std::string name
Definition: xbot_ros.hpp:98
virtual bool processArg(int *i, std::vector< std::string > &args)
Definition: switch_arg.hpp:160
bool combinedSwitchesMatch(std::string &combined)
Definition: switch_arg.hpp:131
Arg(const std::string &flag, const std::string &name, const std::string &desc, bool req, bool valreq, Visitor *v=NULL)
Definition: arg.hpp:353


xbot_node
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:28:13