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;
@ -127,7 +149,7 @@ QString GetOpt::usage() {
Option &o = options[i];
int len = o.name.size() + 2;
switch(o.type) {
case Option::ARGUMENT:
case Option::ARGUMENT:
case Option::OPTIONAL: break;
case Option::SWITCH: len += 5; break;
case Option::OPTION: len += 16; break;
@ -149,7 +171,7 @@ QString GetOpt::usage() {
QString blank = "";
blank.resize(maxlen - line.size());
blank.fill(' ');
line += blank + formatDesc(o.description, maxlen) + "\n";
line += blank + formatDesc(o.description, maxlen) + "\n";
u += line;
}
return u;
@ -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;
}
*(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
@ -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) {
@ -239,7 +273,7 @@ bool GetOpt::parse(QString &error) {
//argument
} else {
arguments.push_back(arg);
}
}
}
//test arguments
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) *
* for more details. *
* *
****************************************************************************/
#ifndef GETOPT_H
#define GETOPT_H
#include <QString>
#include <QStringList>
#include <QMap>
#include <QVariant>
/* Example usage:
QString file1, file2;
QString o_gamma = "10";
QString o_scale = "0.7";
GetOpt opt(argc, argv);
opt.addArgument("img1", "first image", &file1);
opt.addArgument("img2", "second image", &file2);
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.parse(); */
class GetOpt {
protected:
struct Option {
enum Type { SWITCH, OPTION, ARGUMENT, OPTIONAL };
Type type;
char o;
QString name;
QString description;
QVariant *value;
bool *b;
};
bool unlimitedArgs;
QList<Option> options;
public:
QString appname; //application name
QString help; //help text
QStringList args; //original argument vector
QStringList arguments; //arbitrary long list of arguments if unlimitedArgs is true
GetOpt(): unlimitedArgs(false) {}
GetOpt(int argc, char *argv[] );
GetOpt(const QStringList &a);
//add an option without a value
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)
void addOption(char s, const QString &longname, const QString &description, QVariant *v);
//add an argument
void addArgument(const QString &name, const QString &description, QVariant *v);
//add an optional agrument
void addOptionalArgument(const QString &name, const QString &description, QVariant *v);
//allow an unlimited number of optional arguments
void allowUnlimitedArguments(bool allow) { unlimitedArgs = allow; }
//set help if someone uses -h or --help option
void setHelp(QString &_help) { help = _help; }
//parses the command line and fill variables or print an error message and exits
void parse();
//return usage string
QString usage();
//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
****************************************************************************/
#ifndef GETOPT_H
#define GETOPT_H
#include <QString>
#include <QStringList>
#include <QMap>
#include <QVariant>
/* Example usage:
QString file1, file2;
QString o_gamma = "10";
QString o_scale = "0.7";
GetOpt opt(argc, argv);
opt.addArgument("img1", "first image", &file1);
opt.addArgument("img2", "second image", &file2);
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.parse(); */
class GetOpt {
protected:
struct Option {
enum Type { SWITCH, OPTION, ARGUMENT, OPTIONAL };
Type type;
char o;
QString name;
QString description;
QVariant *value;
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;
public:
QString appname; //application name
QString help; //help text
QStringList args; //original argument vector
QStringList arguments; //arbitrary long list of arguments if unlimitedArgs is true
GetOpt(): unlimitedArgs(false) {}
GetOpt(int argc, char *argv[] );
GetOpt(const QStringList &a);
//add an option without a value
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)
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);
//add an optional agrument
void addOptionalArgument(const QString &name, const QString &description, QVariant *v);
//allow an unlimited number of optional arguments
void allowUnlimitedArguments(bool allow) { unlimitedArgs = allow; }
//set help if someone uses -h or --help option
void setHelp(QString &_help) { help = _help; }
//parses the command line and fill variables or print an error message and exits
void parse();
//return usage string
QString usage();
//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