optionparser.h
1 /********************************************************************************
2  * FARSA - Utilities *
3  * Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef OPTIONPARSER_H
21 #define OPTIONPARSER_H
22 
23 #include <QString>
24 #include <QStringList>
25 #include <QMap>
26 #include <QList>
27 #include "utilitiesconfig.h"
28 
29 namespace farsa {
30 
92 class FARSA_UTIL_API OptionParser {
93 public:
109  OptionParser();
110 
127  OptionParser( int argc, char *argv[] );
128 
136  OptionParser( const QStringList &a );
137 
139  OptionParser( int offset );
140 
142  QString appName() const { return aname; }
143 
160  void addSwitch( const QString &lname, bool *b );
161 
168  void addOption( char s, const QString &l, QString *v );
169 
190  void addVarLengthOption( const QString &l, QStringList *v );
191 
214  void addRepeatableOption( char s, QStringList *v );
215 
222  void addRepeatableOption( const QString &l, QStringList *v );
223 
238  void addOptionalOption( const QString &l, QString *v, const QString &def );
239 
244  void addOptionalOption( char s, const QString &l, QString *v, const QString &def );
245 
272  void addArgument( const QString &name, QString *v );
273 
279  void addOptionalArgument( const QString &name, QString *v );
280 
282  bool parse( bool untilFirstSwitchOnly );
283 
294  bool parse() { return parse( false ); }
295 
305  bool isSet( const QString &name ) const;
306 
308  int currentArgument() const { return currArg; }
309 
310 private:
311  enum OptionType { OUnknown, OEnd, OSwitch, OArg1, OOpt, ORepeat, OVarLen };
312 
313  struct Option;
314  friend struct Option;
315 
316  struct Option {
317  Option( OptionType t = OUnknown,
318  char s = 0, const QString &l = QString::null )
319  : type( t ),
320  sname( s ),
321  lname( l ),
322  boolValue( 0 ) { }
323 
324  OptionType type;
325  char sname; // short option name (0 if none)
326  QString lname; // long option name (null if none)
327  union {
328  bool *boolValue;
329  QString *stringValue;
330  QStringList *listValue;
331  };
332  QString def;
333  };
334 
335  QList<Option> options;
336  typedef QList<Option>::const_iterator OptionConstIterator;
337  QMap<QString, int> setOptions;
338 
339  void init( int argc, char *argv[], int offset = 1 );
340  void addOption( Option o );
341  void setSwitch( const Option &o );
342 
343  QStringList args;
344  QString aname;
345 
346  int numReqArgs;
347  int numOptArgs;
348  Option reqArg;
349  Option optArg;
350 
351  int currArg;
352 };
353 
354 } // end namespace farsa
355 
356 #endif
357