209 lines
4.3 KiB
C++
209 lines
4.3 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <QString>
|
|
#include <QList>
|
|
#include <qHash>
|
|
#include <tuple>
|
|
#include <QVariant>
|
|
#include "argsparser_global.h"
|
|
|
|
namespace args_parse {
|
|
/**
|
|
* 参数类型.
|
|
*/
|
|
enum class ParamType {
|
|
IndexParam,
|
|
FloatParam,
|
|
};
|
|
|
|
/**
|
|
* @brief 命令行参数包.
|
|
*/
|
|
class IArgvPack {
|
|
public:
|
|
virtual ~IArgvPack() = default;
|
|
/**
|
|
* 参数包类型.
|
|
*
|
|
* \return
|
|
*/
|
|
virtual ParamType paramType() const = 0;
|
|
/**
|
|
* @brief 参数解释.
|
|
*
|
|
* \return 解释
|
|
*/
|
|
virtual QString means() const = 0;
|
|
virtual QString placeHolder(bool decorate = true) const = 0;
|
|
virtual QVariant value() const = 0;
|
|
/**
|
|
* @brief 匹配长度.
|
|
*
|
|
* \return 长度
|
|
*/
|
|
virtual int matchLenth() const = 0;
|
|
/**
|
|
* @brief 解析.
|
|
*
|
|
* \param argv 输入
|
|
* \param start 起始解析
|
|
* \return 匹配成功
|
|
*/
|
|
virtual bool parse(const QList<QString> args) = 0;
|
|
};
|
|
|
|
class ARGSPARSER_EXPORT __ArgvPackImpls : public IArgvPack {
|
|
private:
|
|
QString means_store;
|
|
QVariant value_store;
|
|
ParamType type_store;
|
|
|
|
public:
|
|
__ArgvPackImpls(const QString &means, ParamType t);
|
|
|
|
void setValue(const QVariant &v);
|
|
|
|
// 通过 IArgvPack 继承
|
|
ParamType paramType() const override;
|
|
QString means() const override;
|
|
QVariant value() const override;
|
|
};
|
|
|
|
class ARGSPARSER_EXPORT __FloatArgvImpls : public __ArgvPackImpls {
|
|
private:
|
|
QString key_name;
|
|
bool optional_value;
|
|
|
|
public:
|
|
explicit __FloatArgvImpls(const QString& key, const QString& means, bool optional);
|
|
|
|
QString bindKey() const;
|
|
virtual bool optional() const;
|
|
};
|
|
|
|
/**
|
|
* @brief 命令行key-value解析匹配模式.
|
|
* --key value
|
|
*/
|
|
class ARGSPARSER_EXPORT FloatKeyValue : public __FloatArgvImpls {
|
|
public:
|
|
/**
|
|
* 浮动key-value解析单元.
|
|
*
|
|
* \param key 键名
|
|
* \param means 参数解释
|
|
* \param optional 是否可选
|
|
*/
|
|
explicit FloatKeyValue(const QString& key, const QString &means, bool optional = false);
|
|
virtual ~FloatKeyValue() = default;
|
|
|
|
virtual QString placeHolder(bool decorate = true) const override;
|
|
virtual int matchLenth() const override;
|
|
bool parse(const QList<QString> args) override;
|
|
};
|
|
|
|
/**
|
|
* @brief 命令行key解析匹配模式.
|
|
* --key
|
|
*/
|
|
class ARGSPARSER_EXPORT FloatOption : public __FloatArgvImpls {
|
|
public:
|
|
/**
|
|
* 浮动option解析单元.
|
|
*
|
|
* \param key 键名
|
|
* \param means 参数解释
|
|
* \param optional 是否可选
|
|
*/
|
|
explicit FloatOption(const QString& key, const QString& means, bool optional = false);
|
|
virtual ~FloatOption() = default;
|
|
|
|
virtual QString placeHolder(bool decorate = true) const override;
|
|
virtual int matchLenth() const override;
|
|
bool parse(const QList<QString> args) override;
|
|
};
|
|
|
|
/**
|
|
* 序列索引参数.
|
|
*/
|
|
class ARGSPARSER_EXPORT IndexParam : public __ArgvPackImpls {
|
|
private:
|
|
QString _place_holder;
|
|
|
|
public:
|
|
explicit IndexParam(const QString & place_v, const QString &means);
|
|
virtual ~IndexParam() = default;
|
|
|
|
// 通过 __ArgvPackImpls 继承
|
|
virtual QString placeHolder(bool decorate = true) const override;
|
|
int matchLenth() const override;
|
|
bool parse(const QList<QString> args) override;
|
|
};
|
|
|
|
class ARGSPARSER_EXPORT MatchMode {
|
|
private:
|
|
int code_store;
|
|
QString _means_explain;
|
|
|
|
QList<std::shared_ptr<IArgvPack>> args_mode;
|
|
|
|
public:
|
|
explicit MatchMode(int mode_code, const QString &explain);
|
|
|
|
/**
|
|
* @brief 获取模式代码
|
|
* @return 模式代码
|
|
*/
|
|
int modeCode()const;
|
|
|
|
/**
|
|
* 获取用例字符串.
|
|
*
|
|
* \return
|
|
*/
|
|
QString usagestring() const;
|
|
|
|
/**
|
|
* 返回模式解析字符串.
|
|
*
|
|
* \return
|
|
*/
|
|
QString explanstring() const;
|
|
|
|
/**
|
|
* 添加参数解析单元.
|
|
*
|
|
* \param unit
|
|
* \return
|
|
*/
|
|
MatchMode& operator<<(std::shared_ptr<IArgvPack> unit);
|
|
|
|
/**
|
|
* @brief 解析匹配参数
|
|
*/
|
|
bool parse(const QList<QString>& args);
|
|
|
|
/**
|
|
* 获取解析单元.
|
|
*
|
|
* \param key
|
|
* \return
|
|
*/
|
|
std::shared_ptr<IArgvPack> getUnitViaKey(const QString& key);
|
|
std::shared_ptr<IArgvPack> getUnitViaInitIndex(int idx);
|
|
};
|
|
|
|
class ARGSPARSER_EXPORT ArgsParser {
|
|
private:
|
|
QList<std::shared_ptr<MatchMode>> match_modes;
|
|
|
|
public:
|
|
QString helperDoc() const;
|
|
|
|
ArgsParser& operator<<(std::shared_ptr<MatchMode> mode);
|
|
|
|
std::shared_ptr<MatchMode> parse(int argc, char* argv[]);
|
|
};
|
|
|
|
} |