utilities/include/optionparser.h

00001 /********************************************************************************
00002  *  FARSA - Utilities                                                           *
00003  *  Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifndef OPTIONPARSER_H
00021 #define OPTIONPARSER_H
00022 
00023 #include <QString>
00024 #include <QStringList>
00025 #include <QMap>
00026 #include <QList>
00027 #include "utilitiesconfig.h"
00028 
00029 namespace farsa {
00030 
00092 class FARSA_UTIL_API OptionParser {
00093 public:
00109     OptionParser();
00110 
00127     OptionParser( int argc, char *argv[] );
00128 
00136     OptionParser( const QStringList &a );
00137 
00139     OptionParser( int offset );
00140 
00142     QString appName() const { return aname; }
00143 
00160     void addSwitch( const QString &lname, bool *b );
00161 
00168     void addOption( char s, const QString &l, QString *v );
00169 
00190     void addVarLengthOption( const QString &l, QStringList *v );
00191 
00214     void addRepeatableOption( char s, QStringList *v );
00215 
00222     void addRepeatableOption( const QString &l, QStringList *v );
00223 
00238     void addOptionalOption( const QString &l, QString *v, const QString &def );
00239 
00244     void addOptionalOption( char s, const QString &l, QString *v, const QString &def );
00245 
00272     void addArgument( const QString &name, QString *v );
00273 
00279     void addOptionalArgument( const QString &name, QString *v );
00280 
00282     bool parse( bool untilFirstSwitchOnly );
00283 
00294     bool parse() { return parse( false ); }
00295 
00305     bool isSet( const QString &name ) const;
00306 
00308     int currentArgument() const { return currArg; }
00309 
00310 private:
00311     enum OptionType { OUnknown, OEnd, OSwitch, OArg1, OOpt, ORepeat, OVarLen };
00312 
00313     struct Option;
00314     friend struct Option;
00315 
00316     struct Option {
00317         Option( OptionType t = OUnknown,
00318                 char s = 0, const QString &l = QString::null )
00319             : type( t ),
00320               sname( s ),
00321               lname( l ),
00322               boolValue( 0 ) { }
00323 
00324         OptionType type;
00325         char sname;     // short option name (0 if none)
00326         QString lname;  // long option name  (null if none)
00327         union {
00328             bool *boolValue;
00329             QString *stringValue;
00330             QStringList *listValue;
00331         };
00332         QString def;
00333     };
00334 
00335     QList<Option> options;
00336     typedef QList<Option>::const_iterator OptionConstIterator;
00337     QMap<QString, int> setOptions;
00338 
00339     void init( int argc, char *argv[], int offset = 1 );
00340     void addOption( Option o );
00341     void setSwitch( const Option &o );
00342 
00343     QStringList args;
00344     QString aname;
00345 
00346     int numReqArgs;
00347     int numOptArgs;
00348     Option reqArg;
00349     Option optArg;
00350 
00351     int currArg;
00352 };
00353 
00354 } // end namespace farsa
00355 
00356 #endif
00357