2013-08-11 21:49:11 +02:00
|
|
|
/* (C) 2011 Viktor Lofgren
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
#include "getoptpp.h"
|
|
|
|
#include <stdexcept>
|
2013-08-11 13:13:19 +02:00
|
|
|
#include <cassert>
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace vlofgren {
|
|
|
|
|
|
|
|
/*
|
2013-08-11 21:49:11 +02:00
|
|
|
*
|
|
|
|
* Class OptionsParser
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
OptionsParser::OptionsParser(const char* programDesc) : fprogramDesc(programDesc) {}
|
|
|
|
OptionsParser::~OptionsParser() {}
|
|
|
|
|
|
|
|
ParameterSet& OptionsParser::getParameters() {
|
2013-08-11 21:49:11 +02:00
|
|
|
return parameters;
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OptionsParser::parse(int argc, const char* argv[]) throw(runtime_error)
|
|
|
|
{
|
2013-08-11 21:49:11 +02:00
|
|
|
argv0 = argv[0];
|
|
|
|
|
|
|
|
if(argc == 1) return;
|
|
|
|
|
|
|
|
vector<string> v(&argv[1], &argv[argc]);
|
|
|
|
|
2013-12-19 22:02:02 +01:00
|
|
|
ParserState state(/* *this,*/ v);
|
2013-08-11 21:49:11 +02:00
|
|
|
|
|
|
|
for(; !state.end(); state.advance()) {
|
|
|
|
|
2013-10-21 20:29:30 +02:00
|
|
|
std::list<Parameter*>::iterator i;
|
|
|
|
|
|
|
|
for(i = parameters.parameters.begin();
|
|
|
|
i != parameters.parameters.end(); i++)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
n = (*i)->receive(state);
|
|
|
|
}
|
|
|
|
catch(Parameter::ExpectedArgument &)
|
|
|
|
{
|
|
|
|
throw Parameter::ExpectedArgument(state.get() + ": expected an argument");
|
|
|
|
}
|
|
|
|
catch(Parameter::UnexpectedArgument &)
|
|
|
|
{
|
|
|
|
throw Parameter::UnexpectedArgument(state.get() + ": did not expect an argument");
|
|
|
|
}
|
|
|
|
catch(Switchable::SwitchingError &)
|
|
|
|
{
|
|
|
|
throw Parameter::ParameterRejected(state.get() + ": parameter already set");
|
|
|
|
}
|
|
|
|
catch(Parameter::ParameterRejected & pr) {
|
|
|
|
std::string what = pr.what();
|
|
|
|
if(what.length())
|
|
|
|
{
|
|
|
|
throw Parameter::ParameterRejected(state.get() + ": " + what);
|
|
|
|
}
|
|
|
|
throw Parameter::ParameterRejected(state.get() + " (unspecified error)");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 1; j < n; ++j)
|
|
|
|
{
|
|
|
|
state.advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(n != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(i == parameters.parameters.end()) {
|
|
|
|
std::string file = state.get();
|
|
|
|
if(file == "--") {
|
|
|
|
state.advance();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(file.at(0) == '-')
|
|
|
|
throw Parameter::ParameterRejected(string("Bad parameter: ") + file);
|
|
|
|
else files.push_back(state.get());
|
|
|
|
}
|
2013-08-11 21:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!state.end()) for(; !state.end(); state.advance()) {
|
2013-10-21 20:29:30 +02:00
|
|
|
files.push_back(state.get());
|
2013-08-11 21:49:11 +02:00
|
|
|
}
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionsParser::usage() const {
|
2013-08-11 21:49:11 +02:00
|
|
|
cerr << fprogramDesc << endl;
|
2013-10-21 20:29:30 +02:00
|
|
|
cerr << "Build time: " << __DATE__ << " " << __TIME__ << endl << endl;
|
2013-08-11 21:49:11 +02:00
|
|
|
cerr << "Usage: " << programName() << " [OPTIONS]" << endl << endl;
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
cerr << "Parameters: " << endl;
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
std::list<Parameter*>::const_iterator i;
|
|
|
|
for(i = parameters.parameters.begin();
|
2013-10-21 20:29:30 +02:00
|
|
|
i != parameters.parameters.end(); i++)
|
2013-08-11 21:49:11 +02:00
|
|
|
{
|
2014-01-26 15:51:02 +01:00
|
|
|
cerr.width(33);
|
2013-10-21 20:29:30 +02:00
|
|
|
cerr << std::left << " " + (*i)->usageLine();
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-10-21 20:29:30 +02:00
|
|
|
cerr.width(40);
|
|
|
|
cerr << std::left << (*i)->description() << endl;
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
}
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const vector<string>& OptionsParser::getFiles() const {
|
2013-08-11 21:49:11 +02:00
|
|
|
return files;
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const string& OptionsParser::programName() const {
|
2013-08-11 21:49:11 +02:00
|
|
|
return argv0;
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-08-11 21:49:11 +02:00
|
|
|
* Parameter set
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
ParameterSet::ParameterSet(const ParameterSet& ps) {
|
2013-08-11 21:49:11 +02:00
|
|
|
throw new runtime_error("ParameterSet not copyable");
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ParameterSet::~ParameterSet() {
|
2013-08-11 21:49:11 +02:00
|
|
|
for(std::list<Parameter*>::iterator i = parameters.begin();
|
2013-10-21 20:29:30 +02:00
|
|
|
i != parameters.end(); i++)
|
2013-08-11 21:49:11 +02:00
|
|
|
{
|
2013-10-21 20:29:30 +02:00
|
|
|
delete *i;
|
2013-08-11 21:49:11 +02:00
|
|
|
}
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The typical use case for command line arguments makes linear searching completely
|
2013-08-11 21:49:11 +02:00
|
|
|
* acceptable here.
|
|
|
|
*/
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
Parameter& ParameterSet::operator[](char c) const {
|
2013-08-11 21:49:11 +02:00
|
|
|
for(std::list<Parameter*>::const_iterator i = parameters.begin(); i!= parameters.end(); i++) {
|
2013-10-21 20:29:30 +02:00
|
|
|
if((*i)->shortOption() == c) return *(*i);
|
2013-08-11 21:49:11 +02:00
|
|
|
}
|
2013-12-19 22:02:02 +01:00
|
|
|
throw out_of_range("ParameterSet["+string(&c)+string("]"));
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Parameter& ParameterSet::operator[](const string& param) const {
|
2013-08-11 21:49:11 +02:00
|
|
|
for(std::list<Parameter*>::const_iterator i = parameters.begin(); i!= parameters.end(); i++) {
|
2013-10-21 20:29:30 +02:00
|
|
|
if((*i)->longOption() == param) return *(*i);
|
2013-08-11 21:49:11 +02:00
|
|
|
}
|
|
|
|
throw out_of_range("ParameterSet["+param+"]");
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-08-11 21:49:11 +02:00
|
|
|
*
|
|
|
|
* Class ParserState
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
|
2013-12-19 22:02:02 +01:00
|
|
|
ParserState::ParserState(/*OptionsParser &opts, */vector<string>& args) :
|
|
|
|
/*opts(opts),*/ arguments(args), iterator(args.begin())
|
2013-08-11 11:54:16 +02:00
|
|
|
{
|
2013-08-11 21:49:11 +02:00
|
|
|
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const string ParserState::peek() const {
|
2013-08-11 21:49:11 +02:00
|
|
|
vector<string>::const_iterator next = iterator+1;
|
|
|
|
if(next != arguments.end()) return *next;
|
|
|
|
else return "";
|
|
|
|
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const string ParserState::get() const {
|
2013-08-11 21:49:11 +02:00
|
|
|
if(!end()) return *iterator;
|
|
|
|
else return "";
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParserState::advance() {
|
2013-08-11 21:49:11 +02:00
|
|
|
iterator++;
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ParserState::end() const {
|
2013-08-11 21:49:11 +02:00
|
|
|
return iterator == arguments.end();
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-08-11 21:49:11 +02:00
|
|
|
*
|
|
|
|
* Class Parameter
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parameter::Parameter(char shortOption, const std::string & longOption, const std::string & description) :
|
2013-08-11 21:49:11 +02:00
|
|
|
fshortOption(shortOption), flongOption(longOption), fdescription(description)
|
2013-08-11 11:54:16 +02:00
|
|
|
{
|
2013-08-11 21:49:11 +02:00
|
|
|
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Parameter::~Parameter() {}
|
|
|
|
|
|
|
|
const string& Parameter::description() const { return fdescription; }
|
|
|
|
const string& Parameter::longOption() const { return flongOption; }
|
2013-08-11 13:13:19 +02:00
|
|
|
bool Parameter::hasShortOption() const { return fshortOption != 0x0; }
|
|
|
|
char Parameter::shortOption() const { assert(hasShortOption()); return fshortOption; }
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
/*
|
2013-08-11 21:49:11 +02:00
|
|
|
*
|
|
|
|
* Class Switchable
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
bool Switchable::isSet() const { return fset; }
|
|
|
|
Switchable::~Switchable() {};
|
|
|
|
Switchable::Switchable() : fset(false) {}
|
|
|
|
|
|
|
|
void MultiSwitchable::set() throw (Switchable::SwitchingError) { fset = true; }
|
|
|
|
MultiSwitchable::~MultiSwitchable() {}
|
|
|
|
|
|
|
|
|
|
|
|
void UniquelySwitchable::set() throw (Switchable::SwitchingError) {
|
2013-08-11 21:49:11 +02:00
|
|
|
if(UniquelySwitchable::isSet()) throw Switchable::SwitchingError();
|
|
|
|
fset = true;
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
UniquelySwitchable::~UniquelySwitchable() {}
|
|
|
|
|
|
|
|
|
|
|
|
PresettableUniquelySwitchable::~PresettableUniquelySwitchable() {}
|
|
|
|
bool PresettableUniquelySwitchable::isSet() const {
|
2013-08-11 21:49:11 +02:00
|
|
|
return UniquelySwitchable::isSet() || fpreset.isSet();
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
void PresettableUniquelySwitchable::set() throw (Switchable::SwitchingError)
|
|
|
|
{
|
2013-08-11 21:49:11 +02:00
|
|
|
UniquelySwitchable::set();
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
void PresettableUniquelySwitchable::preset() {
|
2013-08-11 21:49:11 +02:00
|
|
|
fpreset.set();
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-08-11 21:49:11 +02:00
|
|
|
*
|
|
|
|
* PODParameter specializations
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2013-08-11 11:54:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
PODParameter<string>::PODParameter(char shortOption, const char *longOption,
|
2013-10-21 20:29:30 +02:00
|
|
|
const char* description) : CommonParameter<PresettableUniquelySwitchable>(shortOption, longOption, description) {
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
int PODParameter<int>::validate(const string &s) throw(Parameter::ParameterRejected)
|
|
|
|
{
|
2013-08-11 21:49:11 +02:00
|
|
|
// This is sadly necessary for strto*-functions to operate on
|
|
|
|
// const char*. The function doesn't write to the memory, though,
|
|
|
|
// so it's quite safe.
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
char* cstr = const_cast<char*>(s.c_str());
|
|
|
|
if(*cstr == '\0') throw ParameterRejected("No argument given");
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
long l = strtol(cstr, &cstr, 10);
|
|
|
|
if(*cstr != '\0') throw ParameterRejected("Expected int");
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
if(l > INT_MAX || l < INT_MIN) {
|
2013-10-21 20:29:30 +02:00
|
|
|
throw ParameterRejected("Expected int");
|
2013-08-11 21:49:11 +02:00
|
|
|
}
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
return l;
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
long PODParameter<long>::validate(const string &s) throw(Parameter::ParameterRejected)
|
|
|
|
{
|
2013-08-11 21:49:11 +02:00
|
|
|
char* cstr = const_cast<char*>(s.c_str());
|
|
|
|
if(*cstr == '\0') throw ParameterRejected("No argument given");
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
long l = strtol(cstr, &cstr, 10);
|
|
|
|
if(*cstr != '\0') throw ParameterRejected("Expected long");
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
return l;
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
double PODParameter<double>::validate(const string &s) throw(Parameter::ParameterRejected)
|
|
|
|
{
|
2013-08-11 21:49:11 +02:00
|
|
|
char* cstr = const_cast<char*>(s.c_str());
|
|
|
|
if(*cstr == '\0') throw ParameterRejected("No argument given");
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
double d = strtod(cstr, &cstr);
|
|
|
|
if(*cstr != '\0') throw ParameterRejected("Expected double");
|
2013-08-11 11:54:16 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
return d;
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
string PODParameter<string>::validate(const string &s) throw(Parameter::ParameterRejected)
|
|
|
|
{
|
2013-08-11 21:49:11 +02:00
|
|
|
return s;
|
2013-08-11 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} //namespace
|