QCommandLine 0.2.0
|
00001 #ifndef QCOMMAND_LINE_H 00002 # define QCOMMAND_LINE_H 00003 00004 #include <QtCore/QObject> 00005 #include <QtCore/QList> 00006 #include <QtCore/QStringList> 00007 00008 #ifndef QCOMMANDLINE_EXPORT 00009 # if defined(QCOMMANDLINE_MAKEDLL) 00010 /* We are building this library */ 00011 # define QCOMMANDLINE_EXPORT Q_DECL_EXPORT 00012 # else 00013 /* We are using this library */ 00014 # define QCOMMANDLINE_EXPORT Q_DECL_IMPORT 00015 # endif 00016 #endif 00017 00018 class QCoreApplication; 00019 00020 struct QCommandLineConfigEntry; 00021 typedef QList< QCommandLineConfigEntry > QCommandLineConfig; 00022 00023 struct QCommandLinePrivate { 00024 bool version; 00025 bool help; 00026 QStringList args; 00027 QCommandLineConfig config; 00028 }; 00029 00033 #define QCOMMANDLINE_CONFIG_ENTRY_END \ 00034 { QCommandLine::None, '\0', NULL, NULL, QCommandLine::Default } 00035 00039 class QCOMMANDLINE_EXPORT QCommandLine : public QObject 00040 { 00041 Q_OBJECT 00042 public: 00046 typedef enum { 00047 None = 0, 00048 Switch, 00049 Option, 00050 Param 00051 } Type; 00052 00056 typedef enum { 00057 Default = 0, 00058 Mandatory = 0x01, 00059 Optional = 0x02, 00060 Multiple = 0x04, 00061 MandatoryMultiple = Mandatory|Multiple, 00062 OptionalMultiple = Optional|Multiple, 00063 } Flags; 00064 00069 QCommandLine(QObject * parent = 0); 00070 00078 QCommandLine(const QCoreApplication & app, 00079 const QCommandLineConfig & config = QCommandLineConfig(), 00080 QObject * parent = 0); 00081 00091 QCommandLine(int argc, char *argv[], 00092 const QCommandLineConfig & config = QCommandLineConfig(), 00093 QObject * parent = 0); 00094 00103 QCommandLine(const QStringList args, 00104 const QCommandLineConfig & config = QCommandLineConfig(), 00105 QObject * parent = 0); 00106 00110 ~QCommandLine(); 00111 00117 void setConfig(const QCommandLineConfig & config); 00118 00124 void setConfig(const QCommandLineConfigEntry config[]); 00125 00131 QCommandLineConfig config(); 00132 00139 void setArguments(int argc, char *argv[]); 00140 00146 void setArguments(QStringList args); 00147 00153 QStringList arguments(); 00154 00160 void enableHelp(bool enable); 00161 00167 bool helpEnabled(); 00168 00174 void enableVersion(bool enable); 00175 00181 bool versionEnabled(); 00182 00189 bool parse(); 00190 00200 void addOption(const QChar & shortName, 00201 const QString & longName = QString(), 00202 const QString & descr = QString(), 00203 QCommandLine::Flags flags = QCommandLine::Optional); 00204 00214 void addSwitch(const QChar & shortName, 00215 const QString & longName = QString(), 00216 const QString & descr = QString(), 00217 QCommandLine::Flags flags = QCommandLine::Optional); 00218 00227 void addParam(const QString & name, 00228 const QString & descr = QString(), 00229 QCommandLine::Flags flags = QCommandLine::Optional); 00230 00237 void removeOption(const QString & name); 00238 00245 void removeSwitch(const QString & name); 00246 00253 void removeParam(const QString & name); 00254 00260 QString help(bool logo = true); 00261 00266 QString version(); 00267 00274 void showHelp(bool exit = true, int returnCode = 0); 00275 00282 void showVersion(bool exit = true, int returnCode = 0); 00283 00287 static const QCommandLineConfigEntry helpEntry; 00288 00292 static const QCommandLineConfigEntry versionEntry; 00293 00294 signals: 00301 void switchFound(const QString & name); 00302 00310 void optionFound(const QString & name, const QVariant & value); 00311 00319 void paramFound(const QString & name, const QVariant & value); 00320 00326 void parseError(const QString & error); 00327 private: 00328 QCommandLinePrivate *d; 00329 Q_DECLARE_PRIVATE(QCommandLine); 00330 }; 00331 00335 struct QCommandLineConfigEntry { 00339 QCommandLine::Type type; 00343 QChar shortName; 00347 QString longName; 00351 QString descr; 00355 QCommandLine::Flags flags; 00356 }; 00357 00358 #endif