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.o = s;
option.name = name; option.name = name;
option.description = description; option.description = description;
option.b = b; option.boolean_value = b;
options.push_back(option); options.push_back(option);
} }
//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, QVariant *v ) { void GetOpt::addOption(char s, const QString &name, const QString &description, QVariant *v ) {
Option option; Option option(Option::OPTION, s, name, description);
assert(!findOption(s, option));
assert(!findArg(name, option));
option.type = Option::OPTION;
option.o = s;
option.name = name;
option.description = description;
option.value = v; option.value = v;
assert(!findOption(s, option)); //TODO make this check systematic
assert(!findArg(name, option));
options.push_back(option); options.push_back(option);
} }
//add an argument //add an argument
void GetOpt::addArgument(const QString &name, const QString &description, QVariant *v) { 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; option.value = v;
options.push_back(option); 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 //add an optional agrument
void GetOpt::addOptionalArgument(const QString &name, const QString &description, QVariant *v) { void GetOpt::addOptionalArgument(const QString &name, const QString &description, QVariant *v) {
Option option; Option option;
@ -127,7 +149,7 @@ QString GetOpt::usage() {
Option &o = options[i]; Option &o = options[i];
int len = o.name.size() + 2; int len = o.name.size() + 2;
switch(o.type) { switch(o.type) {
case Option::ARGUMENT: case Option::ARGUMENT:
case Option::OPTIONAL: break; case Option::OPTIONAL: break;
case Option::SWITCH: len += 5; break; case Option::SWITCH: len += 5; break;
case Option::OPTION: len += 16; break; case Option::OPTION: len += 16; break;
@ -149,7 +171,7 @@ QString GetOpt::usage() {
QString blank = ""; QString blank = "";
blank.resize(maxlen - line.size()); blank.resize(maxlen - line.size());
blank.fill(' '); blank.fill(' ');
line += blank + formatDesc(o.description, maxlen) + "\n"; line += blank + formatDesc(o.description, maxlen) + "\n";
u += line; u += line;
} }
return u; return u;
@ -183,7 +205,7 @@ bool GetOpt::parse(QString &error) {
return false; return false;
} }
if(o.type == Option::SWITCH) { if(o.type == Option::SWITCH) {
*(o.b) = true; *(o.boolean_value) = true;
} else { //OPTION } else { //OPTION
i++; i++;
if(args.size() <= i) { if(args.size() <= i) {
@ -195,13 +217,25 @@ bool GetOpt::parse(QString &error) {
error = "Missing argument after option '" + arg + "'"; error = "Missing argument after option '" + arg + "'";
return false; 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); 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 " + error = "Error while parsing option " + o.name + ": cannot convert " +
arg + " to: " + o.value->typeName(); arg + " to: " + v.typeName();
return false; return false;
} }
*(o.value) = v; 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 //option
@ -216,7 +250,7 @@ bool GetOpt::parse(QString &error) {
return false; return false;
} }
if(o.type == Option::SWITCH) { if(o.type == Option::SWITCH) {
*(o.b) = true; *(o.boolean_value) = true;
} else { //OPTION } else { //OPTION
i++; i++;
if(args.size() <= i) { if(args.size() <= i) {
@ -239,7 +273,7 @@ bool GetOpt::parse(QString &error) {
//argument //argument
} else { } else {
arguments.push_back(arg); arguments.push_back(arg);
} }
} }
//test arguments //test arguments
for(int i = 0; i < options.size(); i++) { for(int i = 0; i < options.size(); i++) {

View File

@ -19,90 +19,104 @@
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. * * for more details. *
* * * *
****************************************************************************/ ****************************************************************************/
#ifndef GETOPT_H #ifndef GETOPT_H
#define GETOPT_H #define GETOPT_H
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QMap> #include <QMap>
#include <QVariant> #include <QVariant>
/* Example usage: /* Example usage:
QString file1, file2; QString file1, file2;
QString o_gamma = "10"; QString o_gamma = "10";
QString o_scale = "0.7"; QString o_scale = "0.7";
GetOpt opt(argc, argv); GetOpt opt(argc, argv);
opt.addArgument("img1", "first image", &file1); opt.addArgument("img1", "first image", &file1);
opt.addArgument("img2", "second image", &file2); opt.addArgument("img2", "second image", &file2);
opt.addOption('g', "gamma", "weigth to derivatives of images (default: 10)", &o_gamma); opt.addOption('g', "gamma", "weigth to derivatives of images (default: 10)", &o_gamma);
opt.addOption('s', "scale", "scale [0.5-0.99] for multiscale approach (default: 0.7)", &o_scale); opt.addOption('s', "scale", "scale [0.5-0.99] for multiscale approach (default: 0.7)", &o_scale);
opt.parse(); */ opt.parse(); */
class GetOpt { class GetOpt {
protected: protected:
struct Option { struct Option {
enum Type { SWITCH, OPTION, ARGUMENT, OPTIONAL }; enum Type { SWITCH, OPTION, ARGUMENT, OPTIONAL };
Type type; Type type;
char o; char o;
QString name; QString name;
QString description; QString description;
QVariant *value; QVariant *value;
bool *b; QString *string_value;
}; double *double_value;
bool unlimitedArgs; int *int_value;
QList<Option> options; bool *boolean_value;
public: Option(): value(NULL), string_value(NULL), double_value(NULL), int_value(NULL), boolean_value(NULL) {}
QString appname; //application name Option(Type _type, char _o, QString _name, QString _descr):
QString help; //help text type(_type), o(_o), name(_name), description(_descr),
QStringList args; //original argument vector value(NULL), string_value(NULL), double_value(NULL), int_value(NULL), boolean_value(NULL) {}
QStringList arguments; //arbitrary long list of arguments if unlimitedArgs is true };
GetOpt(): unlimitedArgs(false) {} bool unlimitedArgs;
GetOpt(int argc, char *argv[] ); QList<Option> options;
GetOpt(const QStringList &a);
public:
//add an option without a value QString appname; //application name
void addSwitch(char s, const QString &longname, const QString &description, bool *b ); QString help; //help text
QStringList args; //original argument vector
//add a valued option (v will be left untouched if the option is not given) QStringList arguments; //arbitrary long list of arguments if unlimitedArgs is true
void addOption(char s, const QString &longname, const QString &description, QVariant *v);
GetOpt(): unlimitedArgs(false) {}
//add an argument GetOpt(int argc, char *argv[] );
void addArgument(const QString &name, const QString &description, QVariant *v); GetOpt(const QStringList &a);
//add an optional agrument //add an option without a value
void addOptionalArgument(const QString &name, const QString &description, QVariant *v); void addSwitch(char s, const QString &longname, const QString &description, bool *b );
//allow an unlimited number of optional arguments //add a valued option (v will be left untouched if the option is not given)
void allowUnlimitedArguments(bool allow) { unlimitedArgs = allow; } void addOption(char s, const QString &longname, const QString &description, QVariant *v);
void addOption(char s, const QString &longname, const QString &description, QString *v);
//set help if someone uses -h or --help option void addOption(char s, const QString &longname, const QString &description, double *v);
void setHelp(QString &_help) { help = _help; } void addOption(char s, const QString &longname, const QString &description, int *v);
void addOption(char s, const QString &longname, const QString &description, bool *v);
//parses the command line and fill variables or print an error message and exits
void parse();
//add an argument
//return usage string void addArgument(const QString &name, const QString &description, QVariant *v);
QString usage();
//add an optional agrument
//return argv[0] void addOptionalArgument(const QString &name, const QString &description, QVariant *v);
QString &applicationName();
//allow an unlimited number of optional arguments
protected: void allowUnlimitedArguments(bool allow) { unlimitedArgs = allow; }
//parses and return true on success
bool parse(QString &error); //set help if someone uses -h or --help option
//return options or switch void setHelp(QString &_help) { help = _help; }
bool findOption(char c, Option &option);
//return any named argument //parses the command line and fill variables or print an error message and exits
bool findArg(const QString &name, Option &option); void parse();
//split desc into n pieces of the right length TODO: check for newlines also
QString formatDesc(QString desc, int len); //return usage string
}; QString usage();
#endif //return argv[0]
QString &applicationName();
protected:
//parses and return true on success
bool parse(QString &error);
//return options or switch
bool findOption(char c, Option &option);
//return any named argument
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);
};
#endif