WsParser_VS/ArgsParser/argsparser.h

209 lines
4.3 KiB
C
Raw Normal View History

2024-06-22 10:53:51 +00:00
#pragma once
#include <memory>
#include <QString>
#include <QList>
#include <qHash>
#include <tuple>
2024-10-01 16:03:59 +00:00
#include <QVariant>
2024-06-22 10:53:51 +00:00
#include "argsparser_global.h"
namespace args_parse {
/**
2025-02-15 15:47:42 +00:00
* .
2024-06-22 10:53:51 +00:00
*/
enum class ParamType {
IndexParam,
FloatParam,
};
/**
2025-02-15 15:47:42 +00:00
* @brief .
2024-06-22 10:53:51 +00:00
*/
2024-10-01 08:52:05 +00:00
class IArgvPack {
2024-06-22 10:53:51 +00:00
public:
2024-10-01 08:52:05 +00:00
virtual ~IArgvPack() = default;
2024-10-01 09:48:20 +00:00
/**
2025-02-15 15:47:42 +00:00
* .
2024-10-01 09:48:20 +00:00
*
* \return
*/
2024-06-22 10:53:51 +00:00
virtual ParamType paramType() const = 0;
2024-10-01 09:48:20 +00:00
/**
2025-02-15 15:47:42 +00:00
* @brief .
2024-10-01 09:48:20 +00:00
*
2025-02-15 15:47:42 +00:00
* \return
2024-10-01 09:48:20 +00:00
*/
2024-06-22 10:53:51 +00:00
virtual QString means() const = 0;
2024-10-02 02:03:19 +00:00
virtual QString placeHolder(bool decorate = true) const = 0;
2024-10-01 16:03:59 +00:00
virtual QVariant value() const = 0;
2024-06-22 10:53:51 +00:00
/**
2025-02-15 15:47:42 +00:00
* @brief .
2024-06-22 10:53:51 +00:00
*
2025-02-15 15:47:42 +00:00
* \return
2024-06-22 10:53:51 +00:00
*/
virtual int matchLenth() const = 0;
/**
2025-02-15 15:47:42 +00:00
* @brief .
2024-06-22 10:53:51 +00:00
*
2025-02-15 15:47:42 +00:00
* \param argv
* \param start
* \return
2024-06-22 10:53:51 +00:00
*/
2024-10-03 13:45:30 +00:00
virtual bool parse(const QList<QString> args) = 0;
2024-06-22 10:53:51 +00:00
};
2024-10-01 08:52:05 +00:00
class ARGSPARSER_EXPORT __ArgvPackImpls : public IArgvPack {
2024-06-22 10:53:51 +00:00
private:
QString means_store;
2024-10-01 16:03:59 +00:00
QVariant value_store;
2024-06-22 10:53:51 +00:00
ParamType type_store;
public:
2024-10-01 08:52:05 +00:00
__ArgvPackImpls(const QString &means, ParamType t);
2024-06-22 10:53:51 +00:00
2024-10-01 16:03:59 +00:00
void setValue(const QVariant &v);
2024-06-22 10:53:51 +00:00
2025-02-15 15:47:42 +00:00
// 通过 IArgvPack 继承
2024-06-22 10:53:51 +00:00
ParamType paramType() const override;
QString means() const override;
2024-10-01 16:03:59 +00:00
QVariant value() const override;
2024-06-22 10:53:51 +00:00
};
2024-10-01 15:23:54 +00:00
class ARGSPARSER_EXPORT __FloatArgvImpls : public __ArgvPackImpls {
2024-06-22 10:53:51 +00:00
private:
QString key_name;
bool optional_value;
public:
2024-10-01 15:23:54 +00:00
explicit __FloatArgvImpls(const QString& key, const QString& means, bool optional);
2024-06-22 10:53:51 +00:00
QString bindKey() const;
virtual bool optional() const;
};
/**
2025-02-15 15:47:42 +00:00
* @brief key-value解析匹配模式.
2024-06-22 10:53:51 +00:00
* --key value
*/
2024-10-01 15:23:54 +00:00
class ARGSPARSER_EXPORT FloatKeyValue : public __FloatArgvImpls {
2024-06-22 10:53:51 +00:00
public:
2024-10-01 10:17:40 +00:00
/**
2025-02-15 15:47:42 +00:00
* key-value解析单元.
2024-10-01 10:17:40 +00:00
*
2025-02-15 15:47:42 +00:00
* \param key
* \param means
* \param optional
2024-10-01 10:17:40 +00:00
*/
2024-10-01 08:52:05 +00:00
explicit FloatKeyValue(const QString& key, const QString &means, bool optional = false);
virtual ~FloatKeyValue() = default;
2024-06-22 10:53:51 +00:00
2024-10-02 02:03:19 +00:00
virtual QString placeHolder(bool decorate = true) const override;
2024-06-22 10:53:51 +00:00
virtual int matchLenth() const override;
2024-10-03 13:45:30 +00:00
bool parse(const QList<QString> args) override;
2024-06-22 10:53:51 +00:00
};
/**
2025-02-15 15:47:42 +00:00
* @brief key解析匹配模式.
2024-06-22 10:53:51 +00:00
* --key
*/
2024-10-01 15:23:54 +00:00
class ARGSPARSER_EXPORT FloatOption : public __FloatArgvImpls {
2024-06-22 10:53:51 +00:00
public:
2024-10-01 10:17:40 +00:00
/**
2025-02-15 15:47:42 +00:00
* option解析单元.
2024-10-01 10:17:40 +00:00
*
2025-02-15 15:47:42 +00:00
* \param key
* \param means
* \param optional
2024-10-01 10:17:40 +00:00
*/
2024-06-22 10:53:51 +00:00
explicit FloatOption(const QString& key, const QString& means, bool optional = false);
virtual ~FloatOption() = default;
2024-10-02 02:03:19 +00:00
virtual QString placeHolder(bool decorate = true) const override;
2024-06-22 10:53:51 +00:00
virtual int matchLenth() const override;
2024-10-03 13:45:30 +00:00
bool parse(const QList<QString> args) override;
2024-06-22 10:53:51 +00:00
};
/**
2025-02-15 15:47:42 +00:00
* .
2024-06-22 10:53:51 +00:00
*/
2024-10-01 08:52:05 +00:00
class ARGSPARSER_EXPORT IndexParam : public __ArgvPackImpls {
2024-10-01 09:48:20 +00:00
private:
QString _place_holder;
2024-06-22 10:53:51 +00:00
public:
2024-10-01 09:48:20 +00:00
explicit IndexParam(const QString & place_v, const QString &means);
2024-06-22 10:53:51 +00:00
virtual ~IndexParam() = default;
2025-02-15 15:47:42 +00:00
// 通过 __ArgvPackImpls 继承
2024-10-02 02:03:19 +00:00
virtual QString placeHolder(bool decorate = true) const override;
2024-06-22 10:53:51 +00:00
int matchLenth() const override;
2024-10-03 13:45:30 +00:00
bool parse(const QList<QString> args) override;
2024-06-22 10:53:51 +00:00
};
2024-10-01 10:17:40 +00:00
class ARGSPARSER_EXPORT MatchMode {
private:
int code_store;
2024-10-01 15:23:54 +00:00
QString _means_explain;
QList<std::shared_ptr<IArgvPack>> args_mode;
2024-10-01 10:17:40 +00:00
public:
2024-10-01 15:23:54 +00:00
explicit MatchMode(int mode_code, const QString &explain);
2024-10-01 10:17:40 +00:00
/**
2025-02-15 15:47:42 +00:00
* @brief
* @return
2024-10-01 10:17:40 +00:00
*/
int modeCode()const;
2024-10-01 15:23:54 +00:00
2024-10-01 10:17:40 +00:00
/**
2025-02-15 15:47:42 +00:00
* .
2024-10-01 15:23:54 +00:00
*
* \return
*/
QString usageString() const;
/**
2025-02-15 15:47:42 +00:00
* .
2024-10-01 15:23:54 +00:00
*
* \return
*/
QString explanString() const;
/**
2025-02-15 15:47:42 +00:00
* .
2024-10-01 15:23:54 +00:00
*
* \param unit
* \return
*/
MatchMode& operator<<(std::shared_ptr<IArgvPack> unit);
2024-10-01 10:17:40 +00:00
/**
2025-02-15 15:47:42 +00:00
* @brief
2024-10-01 10:17:40 +00:00
*/
2024-10-03 13:45:30 +00:00
bool parse(const QList<QString>& args);
2024-10-01 15:23:54 +00:00
/**
2025-02-15 15:47:42 +00:00
* .
2024-10-01 15:23:54 +00:00
*
* \param key
* \return
*/
std::shared_ptr<IArgvPack> getUnitViaKey(const QString& key);
2024-10-03 13:45:30 +00:00
std::shared_ptr<IArgvPack> getUnitViaInitIndex(int idx);
2024-10-01 10:17:40 +00:00
};
2024-06-22 10:53:51 +00:00
class ARGSPARSER_EXPORT ArgsParser {
private:
QList<std::shared_ptr<MatchMode>> match_modes;
public:
2024-10-04 05:12:16 +00:00
QString helperDoc() const;
2024-10-01 15:23:54 +00:00
ArgsParser& operator<<(std::shared_ptr<MatchMode> mode);
2024-06-22 10:53:51 +00:00
2024-10-01 15:23:54 +00:00
std::shared_ptr<MatchMode> parse(int argc, char* argv[]);
2024-06-22 10:53:51 +00:00
};
}