MultiSwitchArg.h
Go to the documentation of this file.
// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
/******************************************************************************
*
* file: MultiSwitchArg.h
*
* Copyright (c) 2003, Michael E. Smoot .
* Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
* Copyright (c) 2005, Michael E. Smoot, Daniel Aarno, Erik Zeek.
* Copyright (c) 2017, Google LLC
* All rights reserved.
*
* See the file COPYING in the top directory of this distribution for
* more information.
*
* THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#ifndef TCLAP_MULTI_SWITCH_ARG_H
#define TCLAP_MULTI_SWITCH_ARG_H
#include <string>
#include <vector>
#include <tclap/SwitchArg.h>
namespace TCLAP {
class MultiSwitchArg : public SwitchArg
{
protected:
int _value;
int _default;
public:
MultiSwitchArg(const std::string& flag,
const std::string& name,
const std::string& desc,
int init = 0,
Visitor* v = NULL);
MultiSwitchArg(const std::string& flag,
const std::string& name,
const std::string& desc,
CmdLineInterface& parser,
int init = 0,
Visitor* v = NULL);
virtual bool processArg(int* i, std::vector<std::string>& args);
int getValue() const { return _value; }
std::string shortID(const std::string& val) const;
std::string longID(const std::string& val) const;
void reset();
};
//BEGIN MultiSwitchArg.cpp
inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
const std::string& name,
const std::string& desc,
int init,
Visitor* v )
: SwitchArg(flag, name, desc, false, v),
_value( init ),
_default( init )
{ }
inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
const std::string& name,
const std::string& desc,
CmdLineInterface& parser,
int init,
Visitor* v )
: SwitchArg(flag, name, desc, false, v),
_value( init ),
_default( init )
{
parser.add( this );
}
inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string>& args)
{
if ( _ignoreable && Arg::ignoreRest() )
return false;
if ( argMatches( args[*i] ))
{
// so the isSet() method will work
_alreadySet = true;
// Matched argument: increment value.
++_value;
_checkWithVisitor();
return true;
}
else if ( combinedSwitchesMatch( args[*i] ) )
{
// so the isSet() method will work
_alreadySet = true;
// Matched argument: increment value.
++_value;
// Check for more in argument and increment value.
while ( combinedSwitchesMatch( args[*i] ) )
++_value;
_checkWithVisitor();
return false;
}
else
return false;
}
inline std::string
MultiSwitchArg::shortID(const std::string& val) const
{
return Arg::shortID(val) + " ...";
}
inline std::string
MultiSwitchArg::longID(const std::string& val) const
{
return Arg::longID(val) + " (accepted multiple times)";
}
inline void
MultiSwitchArg::reset()
{
MultiSwitchArg::_value = MultiSwitchArg::_default;
}
//END MultiSwitchArg.cpp
} //namespace TCLAP
#endif
SwitchArg.h
TCLAP::Arg::_checkWithVisitor
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition: Arg.h:602
TCLAP::Arg::longID
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:514
TCLAP::Arg::ignoreRest
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:196
TCLAP::Arg::_alreadySet
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:128
TCLAP::Arg::_ignoreable
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:141
TCLAP::Arg::argMatches
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:581
TCLAP::Arg::shortID
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:496
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:47
TCLAP::CmdLineInterface::add
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
TCLAP::MultiSwitchArg
A multiple switch argument.
Definition: MultiSwitchArg.h:43
TCLAP::MultiSwitchArg::MultiSwitchArg
MultiSwitchArg(const std::string &flag, const std::string &name, const std::string &desc, int init=0, Visitor *v=NULL)
MultiSwitchArg constructor.
Definition: MultiSwitchArg.h:133
TCLAP::MultiSwitchArg::getValue
int getValue() const
Returns int, the number of times the switch has been set.
Definition: MultiSwitchArg.h:114
TCLAP::MultiSwitchArg::shortID
std::string shortID(const std::string &val) const
Returns the shortID for this Arg.
Definition: MultiSwitchArg.h:194
TCLAP::MultiSwitchArg::longID
std::string longID(const std::string &val) const
Returns the longID for this Arg.
Definition: MultiSwitchArg.h:200
TCLAP::MultiSwitchArg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: MultiSwitchArg.h:156
TCLAP::MultiSwitchArg::_value
int _value
The value of the switch.
Definition: MultiSwitchArg.h:49
TCLAP::MultiSwitchArg::reset
void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: MultiSwitchArg.h:206
TCLAP::MultiSwitchArg::_default
int _default
Used to support the reset() method so that ValueArg can be reset to their constructed value.
Definition: MultiSwitchArg.h:55
TCLAP::SwitchArg
A simple switch argument.
Definition: SwitchArg.h:42
TCLAP::SwitchArg::combinedSwitchesMatch
bool combinedSwitchesMatch(std::string &combined)
Checks a string to see if any of the chars in the string match the flag for this Switch.
Definition: SwitchArg.h:176
TCLAP::Visitor
A base class that defines the interface for visitors.
Definition: Visitor.h:35
TCLAP
Definition: Arg.h:48