This commit is contained in:
parent
0de21ee189
commit
435d01dde7
|
@ -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<QString> 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<shared_ptr<__FloatArgvImpl>> 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<shared_ptr<__FloatArgvImpl>> 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;
|
||||
|
|
|
@ -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<QString> 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<QString> 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<QString> args, int start) override;
|
||||
};
|
||||
|
|
|
@ -30,12 +30,12 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
args_parse::ArgsParser args_parser;
|
||||
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_parser.loadMode(0x000Au, args_mode);
|
||||
|
||||
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"--dest", u8"生成目录");
|
||||
args_mode << std::make_shared<args_parse::FloatOption>(u8"--html", u8"生成html文件格式取代AST", true);
|
||||
|
|
Loading…
Reference in New Issue