This commit is contained in:
codeboss 2024-10-01 17:48:20 +08:00
parent 0de21ee189
commit 435d01dde7
3 changed files with 62 additions and 29 deletions

View File

@ -33,6 +33,12 @@ bool __FloatArgvImpl::optional() const {
return optional_value; return optional_value;
} }
QString args_parse::FloatKeyValue::placeHolder() const {
if(optional())
return QString("[--%1 <%2>]").arg(bindKey(), bindKey());
return QString("--%1 <%2>").arg(bindKey(), bindKey());
}
int FloatKeyValue::matchLenth() const { int FloatKeyValue::matchLenth() const {
return 2; return 2;
} }
@ -45,7 +51,12 @@ bool FloatKeyValue::parse(const QList<QString> args, int start) {
return args_t == bindKey(); return args_t == bindKey();
} }
IndexParam::IndexParam(const QString& means) : __ArgvPackImpls(means, ParamType::IndexParam) { } IndexParam::IndexParam(const QString& place_v, const QString& means)
: __ArgvPackImpls(means, ParamType::IndexParam), _place_holder(place_v) { }
QString args_parse::IndexParam::placeHolder() const {
return QString("<%1>").arg(_place_holder);
}
int IndexParam::matchLenth() const { int IndexParam::matchLenth() const {
return 1; return 1;
@ -64,6 +75,12 @@ FloatOption::FloatOption(const QString& key, const QString& means, bool opt)
setValue(u8"0"); setValue(u8"0");
} }
QString args_parse::FloatOption::placeHolder() const {
if(optional())
return QString("[--%1]").arg(bindKey());
return QString("--%1").arg(bindKey());
}
int FloatOption::matchLenth() const { int FloatOption::matchLenth() const {
return 1; return 1;
} }
@ -108,34 +125,34 @@ namespace args_parse {
// 삿혤친駕튈토데禱 // 삿혤친駕튈토데禱
auto parse_unit = args_mode[parse_index]; auto parse_unit = args_mode[parse_index];
switch (parse_unit->paramType()) { switch (parse_unit->paramType()) {
case ParamType::IndexParam:// 固定位置索引匹配 case ParamType::IndexParam:// 固定位置索引匹配
{ {
parse_unit->parse(args, argv_index); parse_unit->parse(args, argv_index);
// 继续匹配下一个为止 // 继续匹配下一个为止
return parse(args, argv_index + parse_unit->matchLenth(), parse_index + 1); return parse(args, argv_index + parse_unit->matchLenth(), parse_index + 1);
}break; }break;
case ParamType::FloatParam:// 浮动参数匹配 case ParamType::FloatParam:// 浮动参数匹配
{ {
QList<shared_ptr<__FloatArgvImpl>> float_parsers; QList<shared_ptr<__FloatArgvImpl>> float_parsers;
for (auto& unit : args_mode) { for (auto& unit : args_mode) {
if (unit->paramType() == ParamType::FloatParam) if (unit->paramType() == ParamType::FloatParam)
float_parsers << dynamic_pointer_cast<__FloatArgvImpl>(unit); float_parsers << dynamic_pointer_cast<__FloatArgvImpl>(unit);
} }
for (auto& unit : float_parsers) { for (auto& unit : float_parsers) {
if (unit->matchLenth() + argv_index > args.size()) if (unit->matchLenth() + argv_index > args.size())
continue; continue;
auto result = unit->parse(args, argv_index); auto result = unit->parse(args, argv_index);
if (result) if (result)
if (parse(args, argv_index + unit->matchLenth(), parse_index + 1)) if (parse(args, argv_index + unit->matchLenth(), parse_index + 1))
return true;
else if (!result && unit->optional())
if (parse(args, argv_index, parse_index + 1))
return true; return true;
} else if (!result && unit->optional())
}break; if (parse(args, argv_index, parse_index + 1))
return true;
}
}break;
} }
return false; return false;

View File

@ -22,9 +22,19 @@ namespace args_parse {
class IArgvPack { class IArgvPack {
public: public:
virtual ~IArgvPack() = default; virtual ~IArgvPack() = default;
/**
* .
*
* \return
*/
virtual ParamType paramType() const = 0; virtual ParamType paramType() const = 0;
/**
* @brief .
*
* \return
*/
virtual QString means() const = 0; virtual QString means() const = 0;
virtual QString placeHolder() const = 0;
virtual QString value() const = 0; virtual QString value() const = 0;
/** /**
* @brief . * @brief .
@ -80,6 +90,7 @@ namespace args_parse {
explicit FloatKeyValue(const QString& key, const QString &means, bool optional = false); explicit FloatKeyValue(const QString& key, const QString &means, bool optional = false);
virtual ~FloatKeyValue() = default; virtual ~FloatKeyValue() = default;
virtual QString placeHolder() const override;
virtual int matchLenth() const override; virtual int matchLenth() const override;
bool parse(const QList<QString> args, int start) override; bool parse(const QList<QString> args, int start) override;
}; };
@ -93,6 +104,7 @@ namespace args_parse {
explicit FloatOption(const QString& key, const QString& means, bool optional = false); explicit FloatOption(const QString& key, const QString& means, bool optional = false);
virtual ~FloatOption() = default; virtual ~FloatOption() = default;
virtual QString placeHolder() const override;
virtual int matchLenth() const override; virtual int matchLenth() const override;
bool parse(const QList<QString> args, int start) override; bool parse(const QList<QString> args, int start) override;
}; };
@ -101,11 +113,15 @@ namespace args_parse {
* . * .
*/ */
class ARGSPARSER_EXPORT IndexParam : public __ArgvPackImpls { class ARGSPARSER_EXPORT IndexParam : public __ArgvPackImpls {
private:
QString _place_holder;
public: public:
explicit IndexParam(const QString &means); explicit IndexParam(const QString & place_v, const QString &means);
virtual ~IndexParam() = default; virtual ~IndexParam() = default;
// 通过 __ArgvPackImpls 继承 // 通过 __ArgvPackImpls 继承
virtual QString placeHolder() const override;
int matchLenth() const override; int matchLenth() const override;
bool parse(const QList<QString> args, int start) override; bool parse(const QList<QString> args, int start) override;
}; };

View File

@ -30,12 +30,12 @@ int main(int argc, char* argv[]) {
args_parse::ArgsParser args_parser; args_parse::ArgsParser args_parser;
QList<std::shared_ptr<args_parse::IArgvPack>> args_mode; QList<std::shared_ptr<args_parse::IArgvPack>> args_mode;
args_mode << std::make_shared<args_parse::IndexParam>(u8"³ÌÐòÃû³Æ"); args_mode << std::make_shared<args_parse::IndexParam>(u8"nsc", u8"³ÌÐòÃû³Æ");
args_mode << std::make_shared<args_parse::FloatOption>(u8"--help", u8"帮助"); args_mode << std::make_shared<args_parse::FloatOption>(u8"--help", u8"帮助");
args_parser.loadMode(0x000Au, args_mode); args_parser.loadMode(0x000Au, args_mode);
args_mode.clear(); args_mode.clear();
args_mode << std::make_shared<args_parse::IndexParam>(u8"³ÌÐòÃû³Æ"); args_mode << std::make_shared<args_parse::IndexParam>(u8"nsc", u8"³ÌÐòÃû³Æ");
args_mode << std::make_shared<args_parse::FloatKeyValue>(u8"--path", u8"源代码目录"); args_mode << std::make_shared<args_parse::FloatKeyValue>(u8"--path", u8"源代码目录");
args_mode << std::make_shared<args_parse::FloatKeyValue>(u8"--dest", u8"生成目录"); args_mode << std::make_shared<args_parse::FloatKeyValue>(u8"--dest", u8"生成目录");
args_mode << std::make_shared<args_parse::FloatOption>(u8"--html", u8"生成html文件格式取代AST", true); args_mode << std::make_shared<args_parse::FloatOption>(u8"--html", u8"生成html文件格式取代AST", true);