diff --git a/ArgsParser/argsparser.cpp b/ArgsParser/argsparser.cpp index ec3d8a6..7fb3af6 100644 --- a/ArgsParser/argsparser.cpp +++ b/ArgsParser/argsparser.cpp @@ -33,6 +33,12 @@ bool __FloatArgvImpl::optional() const { 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 { return 2; } @@ -45,7 +51,12 @@ bool FloatKeyValue::parse(const QList args, int start) { 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 { return 1; @@ -64,6 +75,12 @@ FloatOption::FloatOption(const QString& key, const QString& means, bool opt) 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 { return 1; } @@ -108,34 +125,34 @@ namespace args_parse { // 获取模式匹配单元 auto parse_unit = args_mode[parse_index]; switch (parse_unit->paramType()) { - case ParamType::IndexParam:// 固定位置索引匹配 - { - parse_unit->parse(args, argv_index); - // 继续匹配下一个为止 - return parse(args, argv_index + parse_unit->matchLenth(), parse_index + 1); - }break; - case ParamType::FloatParam:// 浮动参数匹配 - { - QList> float_parsers; + case ParamType::IndexParam:// 固定位置索引匹配 + { + parse_unit->parse(args, argv_index); + // 继续匹配下一个为止 + return parse(args, argv_index + parse_unit->matchLenth(), parse_index + 1); + }break; + case ParamType::FloatParam:// 浮动参数匹配 + { + QList> float_parsers; - for (auto& unit : args_mode) { - if (unit->paramType() == ParamType::FloatParam) - float_parsers << dynamic_pointer_cast<__FloatArgvImpl>(unit); - } + for (auto& unit : args_mode) { + if (unit->paramType() == ParamType::FloatParam) + float_parsers << dynamic_pointer_cast<__FloatArgvImpl>(unit); + } - for (auto& unit : float_parsers) { - if (unit->matchLenth() + argv_index > args.size()) - continue; + for (auto& unit : float_parsers) { + if (unit->matchLenth() + argv_index > args.size()) + continue; - auto result = unit->parse(args, argv_index); - if (result) - 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)) + auto result = unit->parse(args, argv_index); + if (result) + if (parse(args, argv_index + unit->matchLenth(), parse_index + 1)) return true; - } - }break; + else if (!result && unit->optional()) + if (parse(args, argv_index, parse_index + 1)) + return true; + } + }break; } return false; diff --git a/ArgsParser/argsparser.h b/ArgsParser/argsparser.h index 9b3aa16..d2a32eb 100644 --- a/ArgsParser/argsparser.h +++ b/ArgsParser/argsparser.h @@ -22,9 +22,19 @@ namespace args_parse { class IArgvPack { public: virtual ~IArgvPack() = default; - + /** + * 参数包类型. + * + * \return + */ virtual ParamType paramType() const = 0; + /** + * @brief 参数解释. + * + * \return 解释 + */ virtual QString means() const = 0; + virtual QString placeHolder() const = 0; virtual QString value() const = 0; /** * @brief 匹配长度. @@ -80,6 +90,7 @@ namespace args_parse { explicit FloatKeyValue(const QString& key, const QString &means, bool optional = false); virtual ~FloatKeyValue() = default; + virtual QString placeHolder() const override; virtual int matchLenth() const override; bool parse(const QList args, int start) override; }; @@ -93,6 +104,7 @@ namespace args_parse { explicit FloatOption(const QString& key, const QString& means, bool optional = false); virtual ~FloatOption() = default; + virtual QString placeHolder() const override; virtual int matchLenth() const override; bool parse(const QList args, int start) override; }; @@ -101,11 +113,15 @@ namespace args_parse { * 序列索引参数. */ class ARGSPARSER_EXPORT IndexParam : public __ArgvPackImpls { + private: + QString _place_holder; + public: - explicit IndexParam(const QString &means); + explicit IndexParam(const QString & place_v, const QString &means); virtual ~IndexParam() = default; // 通过 __ArgvPackImpls 继承 + virtual QString placeHolder() const override; int matchLenth() const override; bool parse(const QList args, int start) override; }; diff --git a/WsNovelParser/main.cpp b/WsNovelParser/main.cpp index 64520e2..ef18cbb 100644 --- a/WsNovelParser/main.cpp +++ b/WsNovelParser/main.cpp @@ -30,12 +30,12 @@ int main(int argc, char* argv[]) { args_parse::ArgsParser args_parser; QList> args_mode; - args_mode << std::make_shared(u8"程序名称"); + args_mode << std::make_shared(u8"nsc", u8"程序名称"); args_mode << std::make_shared(u8"--help", u8"帮助"); args_parser.loadMode(0x000Au, args_mode); args_mode.clear(); - args_mode << std::make_shared(u8"程序名称"); + args_mode << std::make_shared(u8"nsc", u8"程序名称"); args_mode << std::make_shared(u8"--path", u8"源代码目录"); args_mode << std::make_shared(u8"--dest", u8"生成目录"); args_mode << std::make_shared(u8"--html", u8"生成html文件格式取代AST", true);