QCommandLine 0.3.0
/home/iksaif/dev/qcommandline/src/qcommandline.h
00001 /* This file is part of QCommandLine
00002  *
00003  * Copyright (C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library 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 GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 #ifndef QCOMMAND_LINE_H
00021 # define QCOMMAND_LINE_H
00022 
00023 #include <QtCore/QObject>
00024 #include <QtCore/QList>
00025 #include <QtCore/QStringList>
00026 
00027 #ifndef QCOMMANDLINE_EXPORT
00028 # ifndef QCOMMANDLINE_STATIC
00029 #  if defined(QCOMMANDLINE_MAKEDLL)
00030     /* We are building this library */
00031 #   define QCOMMANDLINE_EXPORT Q_DECL_EXPORT
00032 #  else
00033     /* We are using this library */
00034 #   define QCOMMANDLINE_EXPORT Q_DECL_IMPORT
00035 #  endif
00036 # endif
00037 #endif
00038 #ifndef QCOMMANDLINE_EXPORT
00039 # define QCOMMANDLINE_EXPORT
00040 #endif
00041 
00042 class QCoreApplication;
00043 
00044 struct QCommandLineConfigEntry;
00045 typedef QList< QCommandLineConfigEntry > QCommandLineConfig;
00046 
00047 class QCommandLinePrivate {
00048 public:
00049     bool version;
00050     bool help;
00051     QStringList args;
00052     QCommandLineConfig config;
00053 };
00054 
00058 #define QCOMMANDLINE_CONFIG_ENTRY_END      \
00059     { QCommandLine::None, '\0', NULL, NULL, QCommandLine::Default }
00060 
00064 class QCOMMANDLINE_EXPORT QCommandLine : public QObject
00065 {
00066   Q_OBJECT
00067 public:
00071     typedef enum {
00072         None = 0, 
00073         Switch, 
00074         Option, 
00075         Param 
00076     } Type;
00077 
00081     typedef enum {
00082         Default = 0, 
00083         Mandatory = 0x01, 
00084         Optional = 0x02, 
00085         Multiple = 0x04, 
00086         MandatoryMultiple = Mandatory|Multiple,
00087         OptionalMultiple = Optional|Multiple,
00088     } Flags;
00089 
00094     QCommandLine(QObject * parent = 0);
00095 
00103     QCommandLine(const QCoreApplication & app,
00104                  const QCommandLineConfig & config = QCommandLineConfig(),
00105                  QObject * parent = 0);
00106 
00116     QCommandLine(int argc, char *argv[],
00117                  const QCommandLineConfig & config = QCommandLineConfig(),
00118                  QObject * parent = 0);
00119 
00128     QCommandLine(const QStringList args,
00129                  const QCommandLineConfig & config = QCommandLineConfig(),
00130                  QObject * parent = 0);
00131 
00135    ~QCommandLine();
00136 
00142     void setConfig(const QCommandLineConfig & config);
00143 
00149     void setConfig(const QCommandLineConfigEntry config[]);
00150 
00156     QCommandLineConfig config();
00157 
00164     void setArguments(int argc, char *argv[]);
00165 
00171     void setArguments(QStringList args);
00172 
00178     QStringList arguments();
00179 
00185     void enableHelp(bool enable);
00186 
00192     bool helpEnabled();
00193 
00199     void enableVersion(bool enable);
00200 
00206     bool versionEnabled();
00207 
00214     bool parse();
00215 
00225     void addOption(const QChar & shortName,
00226                    const QString & longName = QString(),
00227                    const QString & descr = QString(),
00228                    QCommandLine::Flags flags = QCommandLine::Optional);
00229 
00239     void addSwitch(const QChar & shortName,
00240                    const QString & longName = QString(),
00241                    const QString & descr = QString(),
00242                    QCommandLine::Flags flags = QCommandLine::Optional);
00243 
00252     void addParam(const QString & name,
00253                   const QString & descr = QString(),
00254                   QCommandLine::Flags flags = QCommandLine::Optional);
00255 
00262     void removeOption(const QString & name);
00263 
00270     void removeSwitch(const QString & name);
00271 
00278     void removeParam(const QString & name);
00279 
00285     QString help(bool logo = true);
00286 
00291     QString version();
00292 
00299     void showHelp(bool exit = true, int returnCode = 0);
00300 
00307     void showVersion(bool exit = true, int returnCode = 0);
00308 
00312     static const QCommandLineConfigEntry helpEntry;
00313 
00317     static const QCommandLineConfigEntry versionEntry;
00318 
00319 signals:
00326     void switchFound(const QString & name);
00327 
00335     void optionFound(const QString & name, const QVariant & value);
00336 
00344     void paramFound(const QString & name, const QVariant & value);
00345 
00351     void parseError(const QString & error);
00352 private:
00353     QCommandLinePrivate *d;
00354     Q_DECLARE_PRIVATE(QCommandLine);
00355 };
00356 
00360 struct QCommandLineConfigEntry {
00364     QCommandLine::Type type;
00368     QChar shortName;
00372     QString longName;
00376     QString descr;
00380     QCommandLine::Flags flags;
00381 };
00382 
00383 #endif