Fixed some bug and move to QVariant.

This commit is contained in:
Federico Ponchio 2008-06-27 18:54:18 +00:00
parent e5572b4f05
commit 8494ace467
2 changed files with 27 additions and 11 deletions

View File

@ -1,5 +1,5 @@
#include <assert.h> #include <assert.h>
#include <QtCore/QVariant>
#include "qgetopt.h" #include "qgetopt.h"
#include <iostream> #include <iostream>
@ -31,7 +31,7 @@ void GetOpt::addSwitch(char s, const QString &name, const QString &description,
} }
//add a valued option (v will be left untouched if the option is not given) //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, QString *v ) { void GetOpt::addOption(char s, const QString &name, const QString &description, QVariant *v ) {
Option option; Option option;
assert(!findOption(s, option)); assert(!findOption(s, option));
assert(!findArg(name, option)); assert(!findArg(name, option));
@ -43,7 +43,7 @@ void GetOpt::addOption(char s, const QString &name, const QString &description,
options.push_back(option); options.push_back(option);
} }
//add an argument //add an argument
void GetOpt::addArgument(const QString &name, const QString &description, QString *v) { void GetOpt::addArgument(const QString &name, const QString &description, QVariant *v) {
Option option; Option option;
assert(!findArg(name, option)); assert(!findArg(name, option));
option.type = Option::ARGUMENT; option.type = Option::ARGUMENT;
@ -53,7 +53,7 @@ void GetOpt::addArgument(const QString &name, const QString &description, QStrin
options.push_back(option); options.push_back(option);
} }
//add an optional agrument //add an optional agrument
void GetOpt::addOptionalArgument(const QString &name, const QString &description, QString *v) { void GetOpt::addOptionalArgument(const QString &name, const QString &description, QVariant *v) {
Option option; Option option;
assert(!findArg(name, option)); assert(!findArg(name, option));
option.type = Option::OPTIONAL; option.type = Option::OPTIONAL;
@ -173,7 +173,13 @@ bool GetOpt::parse(QString &error) {
error = "Unknown option: '--" + arg + "'"; error = "Unknown option: '--" + arg + "'";
return false; return false;
} }
*(o.value) = val; QVariant v(val);
if(!v.canConvert(o.value->type()) || !v.convert(o.value->type())) {
error = "Error while parsing option " + o.name + ": cannot convert " +
val + " to: " + o.value->typeName();
return false;
}
*(o.value) = v;
//option //option
} else if( arg[0] == '-' ) { } else if( arg[0] == '-' ) {
@ -190,12 +196,22 @@ bool GetOpt::parse(QString &error) {
*(o.b) = true; *(o.b) = true;
} else { //OPTION } else { //OPTION
i++; i++;
if(args.size() <= i) {
error = "Missing value for option: " + arg;
return false;
}
arg = args[i]; arg = args[i];
if(i == args.size() || arg[0] == '-') { if(i == args.size() || arg[0] == '-') {
error = "Missing argument after option '" + arg + "'"; error = "Missing argument after option '" + arg + "'";
return false; return false;
} }
*(o.value) = arg; QVariant v(arg);
if(!v.canConvert(o.value->type()) || !v.convert(o.value->type())) {
error = "Error while parsing option " + o.name + ": cannot convert " +
arg + " to: " + o.value->typeName();
return false;
}
*(o.value) = v;
} }
//argument //argument
} else { } else {
@ -268,4 +284,4 @@ QString GetOpt::formatDesc(QString desc, int len) {
desc = desc.mid(pos+1); desc = desc.mid(pos+1);
} }
return output; return output;
} }

View File

@ -27,7 +27,7 @@ class GetOpt {
char o; char o;
QString name; QString name;
QString description; QString description;
QString *value; QVariant *value;
bool *b; bool *b;
}; };
bool unlimitedArgs; bool unlimitedArgs;
@ -47,13 +47,13 @@ class GetOpt {
void addSwitch(char s, const QString &longname, const QString &description, bool *b ); void addSwitch(char s, const QString &longname, const QString &description, bool *b );
//add a valued option (v will be left untouched if the option is not given) //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, QString *v ); void addOption(char s, const QString &longname, const QString &description, QVariant *v);
//add an argument //add an argument
void addArgument(const QString &name, const QString &description, QString *v); void addArgument(const QString &name, const QString &description, QVariant *v);
//add an optional agrument //add an optional agrument
void addOptionalArgument(const QString &name, const QString &description, QString *v); void addOptionalArgument(const QString &name, const QString &description, QVariant *v);
//allow an unlimited number of optional arguments //allow an unlimited number of optional arguments
void allowUnlimitedArguments(bool allow) { unlimitedArgs = allow; } void allowUnlimitedArguments(bool allow) { unlimitedArgs = allow; }