cleanup and bug fixing

This commit is contained in:
Federico Ponchio 2014-02-13 10:44:39 +00:00
parent d45132e06b
commit d86bbfae4e
2 changed files with 33 additions and 22 deletions

View File

@ -245,25 +245,8 @@ 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(type) || !v.convert(type)) {
error = "Error while parsing option " + o.name + ": cannot convert " +
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();
if(!parseOption(o, arg))
return false;
}
//option
@ -311,7 +294,8 @@ bool GetOpt::parse(QString &error) {
error = "Too few arguments, could not parse argument '" + o.name + "'";
return false;
}
*(o.value) = arguments.front();
if(!parseOption(o, arguments.front()))
return false;
arguments.pop_front();
}
//test arguments
@ -319,7 +303,8 @@ bool GetOpt::parse(QString &error) {
Option &o = options[i];
if(o.type != Option::OPTIONAL) continue;
if(arguments.isEmpty()) break;
*(o.value) = arguments.front();
if(!parseOption(o, arguments.front()))
return false;
arguments.pop_front();
}
//test arguments
@ -327,7 +312,8 @@ bool GetOpt::parse(QString &error) {
Option &o = options[i];
if(o.type != Option::ARGUMENT) continue;
if(arguments.isEmpty()) break;
*(o.value) = arguments.front();
if(!parseOption(o, arguments.front()))
return false;
arguments.pop_front();
}
if(!arguments.isEmpty() && !unlimitedArgs) {
@ -378,3 +364,26 @@ QString GetOpt::formatDesc(QString desc, int len) {
}
return output;
}
bool GetOpt::parseOption(GetOpt::Option &o, const QString &arg) {
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(type) || !v.convert(type)) {
cerr << "Error while parsing option " << qPrintable(o.name) << ": cannot convert " <<
qPrintable(arg) << " to: " << qPrintable(v.typeName()) << endl;
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();
return true;
}

View File

@ -122,6 +122,8 @@ class GetOpt {
bool findArg(const QString &name, Option &option);
//split desc into n pieces of the right length TODO: check for newlines also
QString formatDesc(QString desc, int len);
bool parseOption(Option &option, const QString &arg);
};
#endif