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

@@ -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