qgetopt.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * MeshLab                                                           o o     *
00003 * An extendible mesh processor                                    o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2005, 2009                                          \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 #ifndef GETOPT_H
00025 #define GETOPT_H
00026 
00027 #include <QString>
00028 #include <QStringList>
00029 #include <QMap>
00030 #include <QVariant>
00031 
00032 /* Example usage:
00033 
00034   QString file1, file2;
00035   QString o_gamma = "10";
00036   QString o_scale = "0.7";
00037 
00038   GetOpt opt(argc, argv);
00039   opt.addArgument("img1", "first image", &file1);
00040   opt.addArgument("img2", "second image", &file2);
00041   opt.addOption('g', "gamma", "weigth to derivatives of images (default: 10)", &o_gamma);
00042   opt.addOption('s', "scale", "scale [0.5-0.99] for multiscale approach (default: 0.7)", &o_scale);
00043 
00044   opt.parse();          */
00045 
00046 class GetOpt {
00047  protected:
00048   struct Option {
00049     enum Type { SWITCH, OPTION, ARGUMENT, OPTIONAL };
00050     Type type;
00051     char o;
00052     QString name;
00053     QString description;
00054     QVariant *value;
00055     QString *string_value;
00056         float *float_value;
00057     double *double_value;
00058     int *int_value;
00059     bool *boolean_value;
00060 
00061         Option(): value(NULL), string_value(NULL), float_value(NULL), double_value(NULL), int_value(NULL), boolean_value(NULL) {}
00062     Option(Type _type, char _o, QString _name, QString _descr):
00063         type(_type), o(_o), name(_name), description(_descr),
00064                 value(NULL), string_value(NULL), float_value(NULL), double_value(NULL), int_value(NULL), boolean_value(NULL) {}
00065   };
00066 
00067   bool unlimitedArgs;
00068   QList<Option> options;
00069 
00070  public:
00071   QString appname;          //application name
00072   QString help;             //help text
00073   QStringList args;         //original argument vector
00074   QStringList arguments;    //arbitrary long list of arguments if unlimitedArgs is true
00075 
00076   GetOpt(): unlimitedArgs(false) {}
00077   GetOpt(int argc, char *argv[] );
00078   GetOpt(const QStringList &a);
00079 
00080   //add an option without a value
00081   void addSwitch(char s, const QString &longname, const QString &description, bool *b );
00082 
00083   //add a valued option (v will be left untouched if the option is not given)
00084   void addOption(char s, const QString &longname, const QString &description, QVariant *v);
00085   void addOption(char s, const QString &longname, const QString &description, QString *v);
00086   void addOption(char s, const QString &longname, const QString &description, float *v);
00087   void addOption(char s, const QString &longname, const QString &description, double *v);
00088   void addOption(char s, const QString &longname, const QString &description, int *v);
00089   void addOption(char s, const QString &longname, const QString &description, bool *v);
00090 
00091 
00092   //add an argument
00093   void addArgument(const QString &name, const QString &description, QVariant *v);
00094   void addArgument(const QString &name, const QString &description, QString *v);
00095   void addArgument(const QString &name, const QString &description, float *v);
00096   void addArgument(const QString &name, const QString &description, double *v);
00097   void addArgument(const QString &name, const QString &description, int *v);
00098   void addArgument(const QString &name, const QString &description, bool *v);
00099   void addArgument(const QString &name, const QString &description, Option option);
00100 
00101   //add an optional agrument
00102   void addOptionalArgument(const QString &name, const QString &description, QVariant *v);
00103 
00104   //allow an unlimited number of optional arguments
00105   void allowUnlimitedArguments(bool allow) { unlimitedArgs = allow; }
00106 
00107   //set help if someone uses -h or --help option
00108   void setHelp(QString &_help) { help = _help; }
00109 
00110   //parses the command line and fill variables or print an error message and exits
00111   void parse();
00112 
00113   //return usage string
00114   QString usage();
00115 
00116   //return argv[0]
00117   QString &applicationName();
00118 
00119 protected:
00120   //parses and return true on success
00121   bool parse(QString &error);
00122   //return options or switch
00123   bool findOption(char c, Option &option);
00124   //return any named argument
00125   bool findArg(const QString &name, Option &option);
00126   //split desc into n pieces of the right length TODO: check for newlines also
00127   QString formatDesc(QString desc, int len);
00128   //manage conversion from string to option value
00129   bool assignOption(Option &option, QString arg, QString &error);
00130 
00131 };
00132 
00133 #endif
00134 


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:34:44