vcglib/wrap/system/qgetopt.h

109 lines
4.1 KiB
C
Raw Normal View History

2009-05-18 15:59:31 +02:00
/****************************************************************************
* MeshLab o o *
* An extendible mesh processor o o *
* _ O _ *
* Copyright(C) 2005, 2009 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
2008-05-29 14:50:41 +02:00
#ifndef GETOPT_H
#define GETOPT_H
#include <QString>
#include <QStringList>
#include <QMap>
#include <QVariant>
2008-05-29 14:50:41 +02:00
/* 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;
2008-06-27 20:54:18 +02:00
QVariant *value;
2008-05-29 14:50:41 +02:00
bool *b;
};
bool unlimitedArgs;
QList<Option> options;
public:
QString appname; //application name
QString help; //help text
QStringList args; //original argument vector
2008-05-30 12:45:33 +02:00
QStringList arguments; //arbitrary long list of arguments if unlimitedArgs is true
2008-05-29 14:50:41 +02:00
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)
2008-06-27 20:54:18 +02:00
void addOption(char s, const QString &longname, const QString &description, QVariant *v);
2008-05-29 14:50:41 +02:00
//add an argument
2008-06-27 20:54:18 +02:00
void addArgument(const QString &name, const QString &description, QVariant *v);
2008-05-29 14:50:41 +02:00
//add an optional agrument
2008-06-27 20:54:18 +02:00
void addOptionalArgument(const QString &name, const QString &description, QVariant *v);
2008-05-29 14:50:41 +02:00
//allow an unlimited number of optional arguments
2008-05-30 12:45:33 +02:00
void allowUnlimitedArguments(bool allow) { unlimitedArgs = allow; }
2008-05-29 14:50:41 +02:00
//set help if someone uses -h or --help option
void setHelp(QString &_help) { help = _help; }
2008-05-30 12:45:33 +02:00
//parses the command line and fill variables or print an error message and exits
2008-05-29 14:50:41 +02:00
void parse();
//return usage string
QString usage();
2008-05-30 12:45:33 +02:00
2008-05-29 14:50:41 +02:00
//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);
2008-05-30 12:45:33 +02:00
//split desc into n pieces of the right length TODO: check for newlines also
2008-05-29 14:50:41 +02:00
QString formatDesc(QString desc, int len);
};
#endif