Special command line option classes added

This commit is contained in:
johan
2013-08-11 21:49:11 +02:00
parent 29f6e41923
commit cc3baec022
8 changed files with 559 additions and 306 deletions

View File

@@ -203,7 +203,7 @@ protected:
* iterator that technically allows for more complex grammar than what is
* presently used.
*/
virtual bool receive(ParserState& state) throw(ParameterRejected) = 0;
virtual int receive(ParserState& state) throw(ParameterRejected) = 0;
friend class OptionsParser;
@@ -250,20 +250,9 @@ protected:
* receiveSwitch() or receiveArgument() accordingly.
*
* @param state The current argument being parsed.
* @return The number of parameters taken from the input
*/
virtual bool receive(ParserState& state) throw(ParameterRejected);
/**
* Called when a parameter does not have an argument, e.g.
* either -f or --foo
*/
virtual void receiveSwitch() throw (ParameterRejected) = 0;
/**
* Called when a parameter does have an argument, .e.g
* -fbar or --foo=bar
*/
virtual void receiveArgument(const std::string& argument) throw (ParameterRejected) = 0;
virtual int receive(ParserState& state) throw(ParameterRejected) = 0;
};
/** This class (used as a mixin) defines how a parameter
@@ -344,15 +333,15 @@ private:
/* Parameter that does not take an argument, and throws an exception
* if an argument is given */
class SwitchParameter : public CommonParameter<MultiSwitchable> {
template<typename SwitchingBehavior=MultiSwitchable>
class SwitchParameter : public CommonParameter<SwitchingBehavior> {
public:
SwitchParameter(char shortOption, const char *longOption,
const char* description);
virtual ~SwitchParameter();
protected:
virtual void receiveSwitch() throw (Parameter::ParameterRejected);
virtual void receiveArgument(const std::string& argument) throw (Parameter::ParameterRejected);
virtual int receive(ParserState& state) throw(Parameter::ParameterRejected);
};
/** Plain-Old-Data parameter. Performs input validation.
@@ -363,8 +352,8 @@ protected:
* Specifically, you need to specialize validate().
*/
template<typename T>
class PODParameter : public CommonParameter<PresettableUniquelySwitchable> {
template<typename T, typename SwitchingBehavior=PresettableUniquelySwitchable>
class PODParameter : public CommonParameter<SwitchingBehavior> {
public:
PODParameter(char shortOption, const char *longOption,
const char* description);
@@ -383,14 +372,14 @@ public:
std::string usageLine() const;
protected:
virtual int receive(ParserState& state) throw(Parameter::ParameterRejected);
/** Validation function for the data type.
*
* @throw ParameterRejected if the argument does not conform to this data type.
* @return the value corresponding to the argument.
*/
virtual T validate(const std::string& s) throw (ParameterRejected);
virtual void receiveArgument(const std::string &argument) throw(ParameterRejected);
virtual void receiveSwitch() throw (Parameter::ParameterRejected);
virtual T validate(const std::string& s) throw (Parameter::ParameterRejected);
T value;
};

View File

@@ -53,155 +53,125 @@ template<typename SwitchingBehavior>
std::string CommonParameter<SwitchingBehavior>::usageLine() const {
std::stringstream strstr;
strstr.width(10);
if (hasShortOption())
{
strstr << std::left<< std::string("-") + shortOption();
strstr << "-" << shortOption() << ", ";
}
else
{
strstr << " ";
strstr << " ";
}
strstr.width(20);
strstr << std::left << "--" + longOption();
strstr.width(20);
strstr << std::left << "--" + longOption();
return strstr.str();
return strstr.str();
}
/*
*
* Class SwitchParameter
*
*
*/
template<typename SwitchingBehavior>
bool CommonParameter<SwitchingBehavior>::receive(ParserState& state) throw(Parameter::ParameterRejected) {
SwitchParameter<SwitchingBehavior>::SwitchParameter(char shortOption, const char *longOption,
const char* description) : CommonParameter<SwitchingBehavior>(shortOption, longOption, description) {}
template<typename SwitchingBehavior>
SwitchParameter<SwitchingBehavior>::~SwitchParameter() {}
template<typename SwitchingBehavior>
int SwitchParameter<SwitchingBehavior>::receive(ParserState& state) throw(Parameter::ParameterRejected) {
const std::string arg = state.get();
try {
if(arg.at(0) != '-') return false;
if(arg.at(0) != '-') return false;
if(arg.at(1) == '-') { /* Long form parameter */
try {
unsigned int eq = arg.find_first_of("=");
if ((arg.at(1) == '-' && arg.substr(2) == this->longOption()) ||
(this->hasShortOption() && arg.at(1) == this->shortOption() && arg.length() == 2))
{
this->set();
return 1;
}
if(eq == std::string::npos) {
if(arg.substr(2) != longOption())
return false;
this->receiveSwitch();
} else {
if(arg.substr(2, eq-2) != longOption())
return false;
this->receiveArgument(arg.substr(eq+1));
}
return true;
} catch(Parameter::ExpectedArgument &ea) {
throw ExpectedArgument("--" + longOption() + ": expected an argument");
} catch(Parameter::UnexpectedArgument &ua) {
throw UnexpectedArgument("--" + longOption() + ": did not expect an argument");
} catch(Switchable::SwitchingError &e) {
throw ParameterRejected("--" + longOption() + ": parameter already set");
} catch(Parameter::ParameterRejected &pr) {
std::string what = pr.what();
if(what.length())
throw Parameter::ParameterRejected("--" + longOption() + ": " + what);
throw Parameter::ParameterRejected("--" + longOption() + " (unspecified error)");
}
}
else if (hasShortOption())
{
try {
if(arg.at(1) == shortOption()) {
/* Matched argument on the form -f or -fsomething */
if(arg.length() == 2) { /* -f */
this->receiveSwitch();
return true;
} else { /* -fsomething */
this->receiveArgument(arg.substr(2));
return true;
}
}
} catch(Parameter::ExpectedArgument &ea) {
throw ExpectedArgument(std::string("-") + shortOption() + ": expected an argument");
} catch(Parameter::UnexpectedArgument &ua) {
throw UnexpectedArgument(std::string("-") + shortOption() + ": did not expect an argument");
} catch(Switchable::SwitchingError &e) {
throw ParameterRejected(std::string("-") + shortOption() + ": parameter already set");
}
}
} catch(std::out_of_range& o) {
return false;
}
return false;
return 0;
}
/*
* PODParameter stuff
*
*/
template<typename T>
PODParameter<T>::PODParameter(char shortOption, const char *longOption,
template<typename T, typename SwitchingBehavior>
PODParameter<T, SwitchingBehavior>::PODParameter(char shortOption, const char *longOption,
const char* description) : CommonParameter<PresettableUniquelySwitchable>(shortOption, longOption, description) {}
template<typename T>
PODParameter<T>::~PODParameter() {}
template<typename T, typename SwitchingBehavior>
PODParameter<T, SwitchingBehavior>::~PODParameter() {}
template<typename T>
PODParameter<T>::operator T() const { return getValue(); }
template<typename T, typename SwitchingBehavior>
PODParameter<T, SwitchingBehavior>::operator T() const { return getValue(); }
template<typename T>
void PODParameter<T>::setDefault(T value) {
template<typename T, typename SwitchingBehavior>
void PODParameter<T, SwitchingBehavior>::setDefault(T value) {
PresettableUniquelySwitchable::preset();
this->value = value;
}
template<typename T>
T PODParameter<T>::getValue() const {
if(!isSet()) {
throw runtime_error(
std::string("Attempting to retreive the argument of parameter") + longOption() + " but it hasn't been set!");
template<typename T, typename SwitchingBehavior>
T PODParameter<T, SwitchingBehavior>::getValue() const {
if(!this->isSet()) {
throw std::runtime_error(
std::string("Attempting to retreive the argument of parameter") + this->longOption() + " but it hasn't been set!");
}
return value;
}
template<typename T>
std::string PODParameter<T>::usageLine() const {
template<typename T, typename SwitchingBehavior>
std::string PODParameter<T, SwitchingBehavior>::usageLine() const {
std::stringstream strstr;
strstr.width(10);
if (hasShortOption())
if (this->hasShortOption())
{
strstr << std::left << std::string("-") + shortOption() +"arg";
strstr << "-" << this->shortOption() << ", ";
}
else
{
strstr << "";
strstr << " ";
}
strstr.width(20);
strstr << std::left << "--" + longOption() + "=arg";
strstr << std::left << "--" + this->longOption() + " <arg>";
return strstr.str();
}
template<typename T>
void PODParameter<T>::receiveSwitch() throw (Parameter::ParameterRejected) {
throw Parameter::ExpectedArgument();
}
template<typename T, typename SwitchingBehavior>
int PODParameter<T, SwitchingBehavior>::receive(ParserState& state) throw(Parameter::ParameterRejected) {
template<typename T>
void PODParameter<T>::receiveArgument(const std::string &argument) throw(Parameter::ParameterRejected) {
set();
value = this->validate(argument);
}
const std::string arg = state.get();
if(arg.at(0) != '-') return false;
if((arg.at(1) == '-' && arg.substr(2) == this->longOption()) ||
(this->hasShortOption() && arg.at(1) == this->shortOption() && arg.length() == 2))
{
// retrieve the argument
std::string arg1 = state.peek();
if (arg1.length() == 0)
{
throw Parameter::ExpectedArgument(arg + ": expected an argument");
return 1;
}
this->set();
value = this->validate(arg1);
return 2;
}
return 0;
}
#endif