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 bool *b; 00056 }; 00057 bool unlimitedArgs; 00058 QList<Option> options; 00059 00060 public: 00061 QString appname; //application name 00062 QString help; //help text 00063 QStringList args; //original argument vector 00064 QStringList arguments; //arbitrary long list of arguments if unlimitedArgs is true 00065 00066 GetOpt(): unlimitedArgs(false) {} 00067 GetOpt(int argc, char *argv[] ); 00068 GetOpt(const QStringList &a); 00069 00070 //add an option without a value 00071 void addSwitch(char s, const QString &longname, const QString &description, bool *b ); 00072 00073 //add a valued option (v will be left untouched if the option is not given) 00074 void addOption(char s, const QString &longname, const QString &description, QVariant *v); 00075 00076 //add an argument 00077 void addArgument(const QString &name, const QString &description, QVariant *v); 00078 00079 //add an optional agrument 00080 void addOptionalArgument(const QString &name, const QString &description, QVariant *v); 00081 00082 //allow an unlimited number of optional arguments 00083 void allowUnlimitedArguments(bool allow) { unlimitedArgs = allow; } 00084 00085 //set help if someone uses -h or --help option 00086 void setHelp(QString &_help) { help = _help; } 00087 00088 //parses the command line and fill variables or print an error message and exits 00089 void parse(); 00090 00091 //return usage string 00092 QString usage(); 00093 00094 //return argv[0] 00095 QString &applicationName(); 00096 00097 protected: 00098 //parses and return true on success 00099 bool parse(QString &error); 00100 //return options or switch 00101 bool findOption(char c, Option &option); 00102 //return any named argument 00103 bool findArg(const QString &name, Option &option); 00104 //split desc into n pieces of the right length TODO: check for newlines also 00105 QString formatDesc(QString desc, int len); 00106 }; 00107 00108 #endif