qgetopt.h

00001 /**********************************************************************
00002  * Copyright (c) 2003, 2004, froglogic Porten & Stadlbauer GbR
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  *    * Redistributions of source code must retain the above copyright
00010  *      notice, this list of conditions and the following disclaimer.
00011  *
00012  *    * Redistributions in binary form must reproduce the above
00013  *      copyright notice, this list of conditions and the following
00014  *      disclaimer in the documentation and/or other materials
00015  *      provided with the distribution.
00016  *
00017  *    * Neither the name of the froglogic nor the names of its
00018  *      contributors may be used to endorse or promote products
00019  *      derived from this software without specific prior written
00020  *      permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00028  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00029  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00031  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00033  * OF THE POSSIBILITY OF SUCH DAMAGE.
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     // switch (no arguments)
00055     void addSwitch( const QString &lname, bool *b );
00056 
00057     // options (with arguments, sometimes optional)
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     // bare arguments
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;             // short option name (0 if none)
00098         QString lname;  // long option name  (null if none)
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 //    template class LIB_EXPORT QMap<QString, int>;
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 

Документация по Общая библиотека для работы с торговым оборудованием.. Последние изменения: Sun Jul 2 23:37:24 2006. Создано системой  doxygen 1.4.7