2024-06-22 10:53:51 +00:00
|
|
|
|
#include "argsparser.h"
|
|
|
|
|
|
|
|
|
|
using namespace args_parse;
|
2024-09-24 10:43:10 +00:00
|
|
|
|
using namespace std;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
ArgvPackImpl::ArgvPackImpl(const QString& means, ParamType t) : means_store(means), type_store(t) { }
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
|
|
|
|
// ͨ<><CDA8> ArgvPack <20>̳<EFBFBD>
|
2024-09-24 10:43:10 +00:00
|
|
|
|
ParamType ArgvPackImpl::paramType() const {
|
|
|
|
|
return type_store;
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
QString ArgvPackImpl::means() const {
|
|
|
|
|
return means_store;
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
|
|
|
|
void ArgvPackImpl::setValue(const QString& v) {
|
|
|
|
|
this->value_store = v;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
QString ArgvPackImpl::value() const {
|
|
|
|
|
return value_store;
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
|
|
|
|
FloatArgvPack::FloatArgvPack(const QString& key, const QString& means, bool optional) :
|
2024-09-24 10:43:10 +00:00
|
|
|
|
FloatArgvImpl(key, means, optional) { }
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
QString FloatArgvImpl::bindKey() const {
|
|
|
|
|
return key_name;
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
|
|
|
|
bool FloatArgvImpl::optional() const {
|
|
|
|
|
return optional_value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
int FloatArgvPack::matchLenth() const {
|
2024-06-22 10:53:51 +00:00
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
bool FloatArgvPack::parse(const QList<QString> args, int start) {
|
2024-06-22 10:53:51 +00:00
|
|
|
|
auto args_t = args[start];
|
|
|
|
|
auto args_v = args[start + 1];
|
2024-09-24 10:43:10 +00:00
|
|
|
|
if (args_t == bindKey())
|
2024-06-22 10:53:51 +00:00
|
|
|
|
setValue(args_v);
|
|
|
|
|
return args_t == bindKey();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
IndexParam::IndexParam(const QString& means) : ArgvPackImpl(means, ParamType::IndexParam) { }
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
int IndexParam::matchLenth() const {
|
2024-06-22 10:53:51 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
bool IndexParam::parse(const QList<QString> args, int start) {
|
2024-06-22 10:53:51 +00:00
|
|
|
|
setValue(args[start]);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FloatArgvImpl::FloatArgvImpl(const QString& key, const QString& means, bool optional)
|
2024-09-24 10:43:10 +00:00
|
|
|
|
: ArgvPackImpl(means, ParamType::FloatParam), key_name(key), optional_value(optional) { }
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
FloatOption::FloatOption(const QString& key, const QString& means, bool opt)
|
|
|
|
|
: FloatArgvImpl(key, means, opt) {
|
|
|
|
|
setValue(u8"0");
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
int FloatOption::matchLenth() const {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
|
|
|
|
bool FloatOption::parse(const QList<QString> args, int start) {
|
|
|
|
|
auto args_t = args[start];
|
|
|
|
|
setValue(QString::number(args_t == bindKey()));
|
|
|
|
|
return args_t == bindKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace args_parse {
|
|
|
|
|
class MatchMode {
|
|
|
|
|
private:
|
2024-09-24 10:43:10 +00:00
|
|
|
|
QList<shared_ptr<ArgvPack>> args_mode;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
int code_store;
|
|
|
|
|
|
|
|
|
|
public:
|
2024-09-24 10:43:10 +00:00
|
|
|
|
explicit MatchMode(const QList<shared_ptr<ArgvPack>> mode, int mode_code)
|
|
|
|
|
:args_mode(mode), code_store(mode_code) { }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>ȡģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
int modeCode()const {
|
|
|
|
|
return code_store;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>ȡģʽƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return ģʽƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
QList<shared_ptr<ArgvPack>> result() const {
|
|
|
|
|
return args_mode;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
bool parse(const QList<QString>& args, int argv_index, int parse_index) {
|
|
|
|
|
if (argv_index >= args.size())
|
2024-06-22 10:53:51 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
// <20><>ȡģʽƥ<CABD>䵥Ԫ
|
2024-06-22 10:53:51 +00:00
|
|
|
|
auto parse_unit = args_mode[parse_index];
|
|
|
|
|
switch (parse_unit->paramType()) {
|
2024-09-24 10:43:10 +00:00
|
|
|
|
case ParamType::IndexParam:// <20>̶<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
|
|
|
|
|
{
|
|
|
|
|
parse_unit->parse(args, argv_index);
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ϊֹ
|
|
|
|
|
return parse(args, argv_index + parse_unit->matchLenth(), parse_index + 1);
|
|
|
|
|
}break;
|
|
|
|
|
case ParamType::FloatParam:// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
|
|
|
|
|
{
|
|
|
|
|
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;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-09-24 10:43:10 +00:00
|
|
|
|
void ArgsParser::loadMode(int mode_code, const QList<shared_ptr<ArgvPack>>& args_model) {
|
|
|
|
|
this->match_modes.append(make_shared<MatchMode>(args_model, mode_code));
|
2024-06-22 10:53:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
tuple<int, QList<shared_ptr<ArgvPack>>> ArgsParser::parse(int argc, char* argv[]) {
|
|
|
|
|
// <20>ۺϲ<DBBA><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-06-22 10:53:51 +00:00
|
|
|
|
QList<QString> args_list;
|
|
|
|
|
for (int idx = 0; idx < argc; idx++) {
|
|
|
|
|
auto argv_str = QString::fromLocal8Bit(argv[idx]);
|
|
|
|
|
args_list.append(argv_str);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
// ö<><C3B6>ģʽƥ<CABD><C6A5>
|
2024-06-22 10:53:51 +00:00
|
|
|
|
for (auto& minst : this->match_modes) {
|
|
|
|
|
if (minst->parse(args_list, 0, 0))
|
2024-09-24 10:43:10 +00:00
|
|
|
|
return tuple<int, QList<shared_ptr<ArgvPack>>>(minst->modeCode(), minst->result());
|
2024-06-22 10:53:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 10:43:10 +00:00
|
|
|
|
return tuple<int, QList<shared_ptr<ArgvPack>>>(0, QList<shared_ptr<ArgvPack>>());
|
2024-06-22 10:53:51 +00:00
|
|
|
|
}
|