00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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;
00326 QString lname;
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 }
00355
00356 #endif
00357