diff options
Diffstat (limited to 'Common/QOptions.h')
-rw-r--r-- | Common/QOptions.h | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/Common/QOptions.h b/Common/QOptions.h new file mode 100644 index 00000000..7e02867e --- /dev/null +++ b/Common/QOptions.h @@ -0,0 +1,113 @@ +/****************************************************************************** + QOptions: make command line options easy. https://github.com/wang-bin/qoptions + Copyright (C) 2011-2015 Wang Bin <wbsecg1@gmail.com> + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +******************************************************************************/ + +#ifndef QOPTIONS_H +#define QOPTIONS_H + +#include <QtCore/QVariant> +#include <QtCore/QMap> +#include <QtCore/QList> + +#if defined(BUILD_QOPT_LIB) +# define QOPT_EXPORT Q_DECL_EXPORT +#elif defined(BUILD_QOPT_IMPORT) +# define QOPT_EXPORT Q_DECL_IMPORT //only for vc? +#else +# define QOPT_EXPORT +#endif + + +class QOPT_EXPORT QOption { +public: + // TODO: MultiToken -name value1 -name value2 ... + enum Type { + NoToken, SingleToken, MultiToken + }; + QOption(); + explicit QOption(const char* name, const QVariant& defaultValue, Type type, const QString& description); + explicit QOption(const char* name, Type type, const QString& description); + //explicit QOption(const char* name, const QVariant& value, Type type, const QString& description); + + QString shortName() const; + QString longName() const; + QString formatName() const; + QString description() const; + QVariant value() const; + void setValue(const QVariant& value); + bool isSet() const; + bool isValid() const; + + void setType(QOption::Type type); + QOption::Type type() const; + + QString help() const; + void print() const; + bool operator <(const QOption& o) const; +private: + /*! + * \brief setName + * short/long name format: + * "--long", "-short", "short" + * "long,short", "--long,short", "--long,-short", "long,-short" + * "short,--long", "-short,long", "-short,--long" + * \param name + */ + void setName(const QString& name); + + QOption::Type mType; + QString mShortName, mLongName, mDescription; + QVariant mDefaultValue; + QVariant mValue; +}; + + +class QOPT_EXPORT QOptions { +public: + //e.g. application information, copyright etc. + QOptions(); + //QOptions(const QOptions& o); + ~QOptions(); + //QOptions& operator=(const QOptions& o); + + /*! + * \brief parse + * \param argc + * \param argv + * \return false if invalid option found + */ + bool parse(int argc, const char*const* argv); + QOptions& add(const QString& group_description); + QOptions& addDescription(const QString& description); + + QOptions& operator ()(const char* name, const QString& description = QString()); + QOptions& operator ()(const char* name, QOption::Type type, const QString& description = QString()); + QOptions& operator ()(const char* name, const QVariant& defaultValue, const QString& description); + QOptions& operator ()(const char* name, const QVariant& defaultValue, QOption::Type type, const QString& description = QString()); + //QOptions& operator ()(const char* name, QVariant* value, QOption::Type type, const QString& description = QString()); + + QOption option(const QString& name) const; + QVariant value(const QString& name) const; + QVariant operator [](const QString& name) const; + + QString help() const; + void print() const; +private: + QString mDescription, mCurrentDescription; + QList<QOption> mOptions; + QMap<QOption, QString/*group*/> mOptionGroupMap; +}; + +#endif // QOPTIONS_H |