124 lines
2.7 KiB
C++
124 lines
2.7 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <QString>
|
|
#include <QList>
|
|
#include <qHash>
|
|
#include <tuple>
|
|
#include "argsparser_global.h"
|
|
|
|
namespace args_parse {
|
|
/**
|
|
* 参数类型.
|
|
*/
|
|
enum class ParamType {
|
|
IndexParam,
|
|
FloatParam,
|
|
};
|
|
|
|
/**
|
|
* @brief 命令行参数包.
|
|
*/
|
|
class ArgvPack {
|
|
public:
|
|
virtual ~ArgvPack() = default;
|
|
|
|
virtual ParamType paramType() const = 0;
|
|
virtual QString means() const = 0;
|
|
virtual QString value() const = 0;
|
|
/**
|
|
* @brief 匹配长度.
|
|
*
|
|
* \return 长度
|
|
*/
|
|
virtual int matchLenth() const = 0;
|
|
/**
|
|
* @brief 解析.
|
|
*
|
|
* \param argv 输入
|
|
* \param start 起始解析
|
|
* \return 匹配成功
|
|
*/
|
|
virtual bool parse(const QList<QString> args, int start) = 0;
|
|
};
|
|
|
|
class ARGSPARSER_EXPORT ArgvPackImpl : public ArgvPack {
|
|
private:
|
|
QString means_store;
|
|
QString value_store;
|
|
ParamType type_store;
|
|
|
|
public:
|
|
ArgvPackImpl(const QString &means, ParamType t);
|
|
|
|
void setValue(const QString &v);
|
|
|
|
// 通过 ArgvPack 继承
|
|
ParamType paramType() const override;
|
|
QString means() const override;
|
|
QString value() const override;
|
|
};
|
|
|
|
class ARGSPARSER_EXPORT FloatArgvImpl : public ArgvPackImpl {
|
|
private:
|
|
QString key_name;
|
|
bool optional_value;
|
|
|
|
public:
|
|
explicit FloatArgvImpl(const QString& key, const QString& means, bool optional);
|
|
|
|
QString bindKey() const;
|
|
virtual bool optional() const;
|
|
};
|
|
|
|
/**
|
|
* @brief 命令行key-value解析匹配模式.
|
|
* --key value
|
|
*/
|
|
class ARGSPARSER_EXPORT FloatArgvPack : public FloatArgvImpl {
|
|
public:
|
|
explicit FloatArgvPack(const QString& key, const QString &means, bool optional = false);
|
|
virtual ~FloatArgvPack() = default;
|
|
|
|
virtual int matchLenth() const override;
|
|
bool parse(const QList<QString> args, int start) override;
|
|
};
|
|
|
|
/**
|
|
* @brief 命令行key解析匹配模式.
|
|
* --key
|
|
*/
|
|
class ARGSPARSER_EXPORT FloatOption : public FloatArgvImpl {
|
|
public:
|
|
explicit FloatOption(const QString& key, const QString& means, bool optional = false);
|
|
virtual ~FloatOption() = default;
|
|
|
|
virtual int matchLenth() const override;
|
|
bool parse(const QList<QString> args, int start) override;
|
|
};
|
|
|
|
/**
|
|
* 序列索引参数.
|
|
*/
|
|
class ARGSPARSER_EXPORT IndexParam : public ArgvPackImpl {
|
|
public:
|
|
explicit IndexParam(const QString &means);
|
|
virtual ~IndexParam() = default;
|
|
|
|
// 通过 ArgvPackImpl 继承
|
|
int matchLenth() const override;
|
|
bool parse(const QList<QString> args, int start) override;
|
|
};
|
|
|
|
class MatchMode;
|
|
class ARGSPARSER_EXPORT ArgsParser {
|
|
private:
|
|
QList<std::shared_ptr<MatchMode>> match_modes;
|
|
|
|
public:
|
|
void loadMode(int mode_code, const QList<std::shared_ptr<ArgvPack>> &args_model);
|
|
|
|
std::tuple<int, QList<std::shared_ptr<ArgvPack>>> parse(int argc, char* argv[]);
|
|
};
|
|
|
|
} |