This commit is contained in:
parent
435d01dde7
commit
cae32e3215
|
@ -34,7 +34,7 @@ bool __FloatArgvImpl::optional() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
QString args_parse::FloatKeyValue::placeHolder() const {
|
QString args_parse::FloatKeyValue::placeHolder() const {
|
||||||
if(optional())
|
if (optional())
|
||||||
return QString("[--%1 <%2>]").arg(bindKey(), bindKey());
|
return QString("[--%1 <%2>]").arg(bindKey(), bindKey());
|
||||||
return QString("--%1 <%2>").arg(bindKey(), bindKey());
|
return QString("--%1 <%2>").arg(bindKey(), bindKey());
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ FloatOption::FloatOption(const QString& key, const QString& means, bool opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
QString args_parse::FloatOption::placeHolder() const {
|
QString args_parse::FloatOption::placeHolder() const {
|
||||||
if(optional())
|
if (optional())
|
||||||
return QString("[--%1]").arg(bindKey());
|
return QString("[--%1]").arg(bindKey());
|
||||||
return QString("--%1").arg(bindKey());
|
return QString("--%1").arg(bindKey());
|
||||||
}
|
}
|
||||||
|
@ -91,74 +91,71 @@ bool FloatOption::parse(const QList<QString> args, int start) {
|
||||||
return args_t == bindKey();
|
return args_t == bindKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace args_parse {
|
MatchMode::MatchMode(const QList<shared_ptr<IArgvPack>> mode, int mode_code)
|
||||||
class MatchMode {
|
:args_mode(mode), code_store(mode_code) { }
|
||||||
private:
|
|
||||||
QList<shared_ptr<IArgvPack>> args_mode;
|
|
||||||
int code_store;
|
|
||||||
|
|
||||||
public:
|
/**
|
||||||
explicit MatchMode(const QList<shared_ptr<IArgvPack>> mode, int mode_code)
|
* @brief 获取模式代码
|
||||||
:args_mode(mode), code_store(mode_code) { }
|
* @return 模式代码
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
int MatchMode::modeCode() const {
|
||||||
* @brief 获取模式代码
|
return code_store;
|
||||||
* @return 模式代码
|
|
||||||
*/
|
|
||||||
int modeCode()const {
|
|
||||||
return code_store;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief 获取模式匹配结果
|
|
||||||
* @return 模式匹配结果
|
|
||||||
*/
|
|
||||||
QList<shared_ptr<IArgvPack>> result() const {
|
|
||||||
return args_mode;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief 解析匹配参数
|
|
||||||
*/
|
|
||||||
bool parse(const QList<QString>& args, int argv_index, int parse_index) {
|
|
||||||
if (argv_index >= args.size())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// 获取模式匹配单元
|
|
||||||
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;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
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))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 获取模式匹配结果
|
||||||
|
* @return 模式匹配结果
|
||||||
|
*/
|
||||||
|
|
||||||
|
QList<shared_ptr<IArgvPack>> MatchMode::result() const {
|
||||||
|
return args_mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 解析匹配参数
|
||||||
|
*/
|
||||||
|
|
||||||
|
bool MatchMode::parse(const QList<QString>& args, int argv_index, int parse_index) {
|
||||||
|
if (argv_index >= args.size())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// 获取模式匹配单元
|
||||||
|
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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void ArgsParser::loadMode(int mode_code, const QList<shared_ptr<IArgvPack>>& args_model) {
|
void ArgsParser::loadMode(int mode_code, const QList<shared_ptr<IArgvPack>>& args_model) {
|
||||||
this->match_modes.append(make_shared<MatchMode>(args_model, mode_code));
|
this->match_modes.append(make_shared<MatchMode>(args_model, mode_code));
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,6 +87,13 @@ namespace args_parse {
|
||||||
*/
|
*/
|
||||||
class ARGSPARSER_EXPORT FloatKeyValue : public __FloatArgvImpl {
|
class ARGSPARSER_EXPORT FloatKeyValue : public __FloatArgvImpl {
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* 浮动key-value解析单元.
|
||||||
|
*
|
||||||
|
* \param key 键名
|
||||||
|
* \param means 参数解释
|
||||||
|
* \param optional 是否可选
|
||||||
|
*/
|
||||||
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;
|
||||||
|
|
||||||
|
@ -101,6 +108,13 @@ namespace args_parse {
|
||||||
*/
|
*/
|
||||||
class ARGSPARSER_EXPORT FloatOption : public __FloatArgvImpl {
|
class ARGSPARSER_EXPORT FloatOption : public __FloatArgvImpl {
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* 浮动option解析单元.
|
||||||
|
*
|
||||||
|
* \param key 键名
|
||||||
|
* \param means 参数解释
|
||||||
|
* \param optional 是否可选
|
||||||
|
*/
|
||||||
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;
|
||||||
|
|
||||||
|
@ -126,7 +140,30 @@ namespace args_parse {
|
||||||
bool parse(const QList<QString> args, int start) override;
|
bool parse(const QList<QString> args, int start) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class MatchMode;
|
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 获取模式代码
|
||||||
|
* @return 模式代码
|
||||||
|
*/
|
||||||
|
int modeCode()const;
|
||||||
|
/**
|
||||||
|
* @brief 获取模式匹配结果
|
||||||
|
* @return 模式匹配结果
|
||||||
|
*/
|
||||||
|
QList<shared_ptr<IArgvPack>> result() const;
|
||||||
|
/**
|
||||||
|
* @brief 解析匹配参数
|
||||||
|
*/
|
||||||
|
bool parse(const QList<QString>& args, int argv_index, int parse_index);
|
||||||
|
};
|
||||||
|
|
||||||
class ARGSPARSER_EXPORT ArgsParser {
|
class ARGSPARSER_EXPORT ArgsParser {
|
||||||
private:
|
private:
|
||||||
QList<std::shared_ptr<MatchMode>> match_modes;
|
QList<std::shared_ptr<MatchMode>> match_modes;
|
||||||
|
|
Loading…
Reference in New Issue