2024-06-22 10:53:51 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QList>
|
|
|
|
|
#include <qHash>
|
|
|
|
|
#include <tuple>
|
|
|
|
|
#include "argsparser_global.h"
|
|
|
|
|
|
|
|
|
|
namespace args_parse {
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
*/
|
|
|
|
|
enum class ParamType {
|
|
|
|
|
IndexParam,
|
|
|
|
|
FloatParam,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
*/
|
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
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
*
|
|
|
|
|
* \return
|
|
|
|
|
*/
|
2024-06-22 10:53:51 +00:00
|
|
|
|
virtual ParamType paramType() const = 0;
|
2024-10-01 09:48:20 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
*
|
|
|
|
|
* \return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2024-06-22 10:53:51 +00:00
|
|
|
|
virtual QString means() const = 0;
|
2024-10-01 09:48:20 +00:00
|
|
|
|
virtual QString placeHolder() const = 0;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
virtual QString value() const = 0;
|
|
|
|
|
/**
|
|
|
|
|
* @brief ƥ<EFBFBD>䳤<EFBFBD><EFBFBD>.
|
|
|
|
|
*
|
|
|
|
|
* \return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
virtual int matchLenth() const = 0;
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
*
|
|
|
|
|
* \param argv <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* \param start <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* \return ƥ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
virtual bool parse(const QList<QString> args, int start) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
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;
|
|
|
|
|
QString value_store;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
void setValue(const QString &v);
|
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
// ͨ<><CDA8> IArgvPack <20>̳<EFBFBD>
|
2024-06-22 10:53:51 +00:00
|
|
|
|
ParamType paramType() const override;
|
|
|
|
|
QString means() const override;
|
|
|
|
|
QString value() const override;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
class ARGSPARSER_EXPORT __FloatArgvImpl : public __ArgvPackImpls {
|
2024-06-22 10:53:51 +00:00
|
|
|
|
private:
|
|
|
|
|
QString key_name;
|
|
|
|
|
bool optional_value;
|
|
|
|
|
|
|
|
|
|
public:
|
2024-10-01 08:52:05 +00:00
|
|
|
|
explicit __FloatArgvImpl(const QString& key, const QString& means, bool optional);
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
|
|
|
|
QString bindKey() const;
|
|
|
|
|
virtual bool optional() const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>key-value<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD>ģʽ.
|
|
|
|
|
* --key value
|
|
|
|
|
*/
|
2024-10-01 08:52:05 +00:00
|
|
|
|
class ARGSPARSER_EXPORT FloatKeyValue : public __FloatArgvImpl {
|
2024-06-22 10:53:51 +00:00
|
|
|
|
public:
|
2024-10-01 10:17:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>key-value<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ.
|
|
|
|
|
*
|
|
|
|
|
* \param key <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* \param means <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* \param optional <EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>ѡ
|
|
|
|
|
*/
|
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-01 09:48:20 +00:00
|
|
|
|
virtual QString placeHolder() const override;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
virtual int matchLenth() const override;
|
|
|
|
|
bool parse(const QList<QString> args, int start) override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>key<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD>ģʽ.
|
|
|
|
|
* --key
|
|
|
|
|
*/
|
2024-10-01 08:52:05 +00:00
|
|
|
|
class ARGSPARSER_EXPORT FloatOption : public __FloatArgvImpl {
|
2024-06-22 10:53:51 +00:00
|
|
|
|
public:
|
2024-10-01 10:17:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>option<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ.
|
|
|
|
|
*
|
|
|
|
|
* \param key <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* \param means <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* \param optional <EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>ѡ
|
|
|
|
|
*/
|
2024-06-22 10:53:51 +00:00
|
|
|
|
explicit FloatOption(const QString& key, const QString& means, bool optional = false);
|
|
|
|
|
virtual ~FloatOption() = default;
|
|
|
|
|
|
2024-10-01 09:48:20 +00:00
|
|
|
|
virtual QString placeHolder() const override;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
virtual int matchLenth() const override;
|
|
|
|
|
bool parse(const QList<QString> args, int start) override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
*/
|
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;
|
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
// ͨ<><CDA8> __ArgvPackImpls <20>̳<EFBFBD>
|
2024-10-01 09:48:20 +00:00
|
|
|
|
virtual QString placeHolder() const override;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
int matchLenth() const override;
|
|
|
|
|
bool parse(const QList<QString> args, int start) override;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-01 10:17:40 +00:00
|
|
|
|
class ARGSPARSER_EXPORT MatchMode {
|
|
|
|
|
private:
|
|
|
|
|
QList<shared_ptr<IArgvPack>> args_mode;
|
|
|
|
|
int code_store;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
explicit MatchMode(const QList<shared_ptr<IArgvPack>> mode, int mode_code);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>ȡģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
int modeCode()const;
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>ȡģʽƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return ģʽƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
QList<shared_ptr<IArgvPack>> result() const;
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
bool parse(const QList<QString>& args, int argv_index, int parse_index);
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-22 10:53:51 +00:00
|
|
|
|
class ARGSPARSER_EXPORT ArgsParser {
|
|
|
|
|
private:
|
|
|
|
|
QList<std::shared_ptr<MatchMode>> match_modes;
|
|
|
|
|
|
|
|
|
|
public:
|
2024-10-01 08:52:05 +00:00
|
|
|
|
void loadMode(int mode_code, const QList<std::shared_ptr<IArgvPack>> &args_model);
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
std::tuple<int, QList<std::shared_ptr<IArgvPack>>> parse(int argc, char* argv[]);
|
2024-06-22 10:53:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|