00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef QGETOPT_H
00038 #define QGETOPT_H
00039
00040 #include <qstring.h>
00041 #include <qstringlist.h>
00042 #include <qmap.h>
00043 #include "teglobal.h"
00044
00045 class LIB_EXPORT GetOpt {
00046 public:
00047 GetOpt();
00048 GetOpt( int offset );
00049 GetOpt( int argc, char *argv[] );
00050 GetOpt( const QStringList &a );
00051
00052 QString appName() const { return aname; }
00053
00054
00055 void addSwitch( const QString &lname, bool *b );
00056
00057
00058 void addOption( char s, const QString &l, QString *v );
00059 void addVarLengthOption( const QString &l, QStringList *v );
00060 void addRepeatableOption( char s, QStringList *v );
00061 void addRepeatableOption( const QString &l, QStringList *v );
00062 void addOptionalOption( const QString &l, QString *v,
00063 const QString &def );
00064 void addOptionalOption( char s, const QString &l,
00065 QString *v, const QString &def );
00066
00067
00068 void addArgument( const QString &name, QString *v );
00069 void addOptionalArgument( const QString &name, QString *v );
00070
00071 bool parse( bool untilFirstSwitchOnly );
00072 bool parse() { return parse( false ); }
00073
00074 bool isSet( const QString &name ) const;
00075
00076 int currentArgument() const { return currArg; }
00077
00078 private:
00079 enum OptionType { OUnknown, OEnd, OSwitch, OArg1, OOpt, ORepeat, OVarLen };
00080
00081 struct LIB_EXPORT Option;
00082 friend struct Option;
00083
00084 struct Option {
00085 Option( OptionType t = OUnknown,
00086 char s = 0, const QString &l = QString::null )
00087 : type( t ),
00088 sname( s ),
00089 lname( l ),
00090 boolValue( 0 ) { }
00091 bool operator==(const Option & opt) const
00092 {
00093 return (type==opt.type) && (sname==opt.sname) && (lname==opt.lname);
00094 }
00095
00096 OptionType type;
00097 char sname;
00098 QString lname;
00099 union {
00100 bool *boolValue;
00101 QString *stringValue;
00102 QStringList *listValue;
00103 };
00104 QString def;
00105 };
00106
00107 #include "templexports.h"
00108
00109 QValueList<Option> options;
00110 typedef QValueList<Option>::const_iterator OptionConstIterator;
00111
00112 QMap<QString, int> setOptions;
00113
00114 void init( int argc, char *argv[], int offset = 1 );
00115 void addOption( Option o );
00116 void setSwitch( const Option &o );
00117
00118 QStringList args;
00119 QString aname;
00120
00121 int numReqArgs;
00122 int numOptArgs;
00123 Option reqArg;
00124 Option optArg;
00125
00126 int currArg;
00127 };
00128
00129 template class LIB_EXPORT QValueList<GetOpt::Option>;
00130 template class LIB_EXPORT QValueListIterator<GetOpt::Option>;
00131 template class LIB_EXPORT QValueListConstIterator<GetOpt::Option>;
00132
00133 #endif
00134