added calls for other than variants.

This commit is contained in:
Federico Ponchio 2013-03-20 12:48:17 +00:00
parent f3337dcb7c
commit 30f67a3782
2 changed files with 151 additions and 103 deletions

View File

@ -50,21 +50,21 @@ void GetOpt::addSwitch(char s, const QString &name, const QString &description,
option.o = s;
option.name = name;
option.description = description;
option.b = b;
option.boolean_value = b;
options.push_back(option);
}
//add a valued option (v will be left untouched if the option is not given)
void GetOpt::addOption(char s, const QString &name, const QString &description, QVariant *v ) {
Option option;
assert(!findOption(s, option));
assert(!findArg(name, option));
option.type = Option::OPTION;
option.o = s;
option.name = name;
option.description = description;
Option option(Option::OPTION, s, name, description);
option.value = v;
assert(!findOption(s, option)); //TODO make this check systematic
assert(!findArg(name, option));
options.push_back(option);
}
//add an argument
void GetOpt::addArgument(const QString &name, const QString &description, QVariant *v) {
@ -76,6 +76,28 @@ void GetOpt::addArgument(const QString &name, const QString &description, QVaria
option.value = v;
options.push_back(option);
}
void GetOpt::addOption(char s, const QString &longname, const QString &description, QString *v) {
Option option(Option::OPTION, s, longname, description);
option.string_value = v;
options.push_back(option);
}
void GetOpt::addOption(char s, const QString &longname, const QString &description, double *v) {
Option option(Option::OPTION, s, longname, description);
option.double_value = v;
options.push_back(option);
}
void GetOpt::addOption(char s, const QString &longname, const QString &description, int *v) {
Option option(Option::OPTION, s, longname, description);
option.int_value = v;
options.push_back(option);
}
void GetOpt::addOption(char s, const QString &longname, const QString &description, bool *v) {
Option option(Option::OPTION, s, longname, description);
option.boolean_value = v;
options.push_back(option);
}
//add an optional agrument
void GetOpt::addOptionalArgument(const QString &name, const QString &description, QVariant *v) {
Option option;
@ -183,7 +205,7 @@ bool GetOpt::parse(QString &error) {
return false;
}
if(o.type == Option::SWITCH) {
*(o.b) = true;
*(o.boolean_value) = true;
} else { //OPTION
i++;
if(args.size() <= i) {
@ -195,13 +217,25 @@ bool GetOpt::parse(QString &error) {
error = "Missing argument after option '" + arg + "'";
return false;
}
QVariant::Type type;
if(o.value) type = o.value->type();
if(o.string_value) type = QVariant::String;
if(o.double_value) type = QVariant::Double;
if(o.int_value) type = QVariant::Int;
if(o.boolean_value) type = QVariant::Bool;
QVariant v(arg);
if(!v.canConvert(o.value->type()) || !v.convert(o.value->type())) {
if(!v.canConvert(type) || !v.convert(type)) {
error = "Error while parsing option " + o.name + ": cannot convert " +
arg + " to: " + o.value->typeName();
arg + " to: " + v.typeName();
return false;
}
if(o.value)
*(o.value) = v;
if(o.string_value) *(o.string_value) = v.toString();
if(o.double_value) *(o.double_value) = v.toDouble();
if(o.int_value) *(o.int_value) = v.toInt();
if(o.boolean_value) *(o.boolean_value) = v.toBool();
}
//option
@ -216,7 +250,7 @@ bool GetOpt::parse(QString &error) {
return false;
}
if(o.type == Option::SWITCH) {
*(o.b) = true;
*(o.boolean_value) = true;
} else { //OPTION
i++;
if(args.size() <= i) {

View File

@ -52,8 +52,17 @@ class GetOpt {
QString name;
QString description;
QVariant *value;
bool *b;
QString *string_value;
double *double_value;
int *int_value;
bool *boolean_value;
Option(): value(NULL), string_value(NULL), double_value(NULL), int_value(NULL), boolean_value(NULL) {}
Option(Type _type, char _o, QString _name, QString _descr):
type(_type), o(_o), name(_name), description(_descr),
value(NULL), string_value(NULL), double_value(NULL), int_value(NULL), boolean_value(NULL) {}
};
bool unlimitedArgs;
QList<Option> options;
@ -72,6 +81,11 @@ class GetOpt {
//add a valued option (v will be left untouched if the option is not given)
void addOption(char s, const QString &longname, const QString &description, QVariant *v);
void addOption(char s, const QString &longname, const QString &description, QString *v);
void addOption(char s, const QString &longname, const QString &description, double *v);
void addOption(char s, const QString &longname, const QString &description, int *v);
void addOption(char s, const QString &longname, const QString &description, bool *v);
//add an argument
void addArgument(const QString &name, const QString &description, QVariant *v);