165 lines
4.1 KiB
C++
165 lines
4.1 KiB
C++
#include "argsparser.h"
|
|
|
|
using namespace args_parse;
|
|
using namespace std;
|
|
|
|
__ArgvPackImpls::__ArgvPackImpls(const QString& means, ParamType t) : means_store(means), type_store(t) { }
|
|
|
|
// ͨ¹ý IArgvPack ¼Ì³Ð
|
|
ParamType __ArgvPackImpls::paramType() const {
|
|
return type_store;
|
|
}
|
|
|
|
QString __ArgvPackImpls::means() const {
|
|
return means_store;
|
|
}
|
|
|
|
void __ArgvPackImpls::setValue(const QString& v) {
|
|
this->value_store = v;
|
|
}
|
|
|
|
QString __ArgvPackImpls::value() const {
|
|
return value_store;
|
|
}
|
|
|
|
FloatKeyValue::FloatKeyValue(const QString& key, const QString& means, bool optional) :
|
|
__FloatArgvImpl(key, means, optional) { }
|
|
|
|
QString __FloatArgvImpl::bindKey() const {
|
|
return key_name;
|
|
}
|
|
|
|
bool __FloatArgvImpl::optional() const {
|
|
return optional_value;
|
|
}
|
|
|
|
int FloatKeyValue::matchLenth() const {
|
|
return 2;
|
|
}
|
|
|
|
bool FloatKeyValue::parse(const QList<QString> args, int start) {
|
|
auto args_t = args[start];
|
|
auto args_v = args[start + 1];
|
|
if (args_t == bindKey())
|
|
setValue(args_v);
|
|
return args_t == bindKey();
|
|
}
|
|
|
|
IndexParam::IndexParam(const QString& means) : __ArgvPackImpls(means, ParamType::IndexParam) { }
|
|
|
|
int IndexParam::matchLenth() const {
|
|
return 1;
|
|
}
|
|
|
|
bool IndexParam::parse(const QList<QString> args, int start) {
|
|
setValue(args[start]);
|
|
return true;
|
|
}
|
|
|
|
__FloatArgvImpl::__FloatArgvImpl(const QString& key, const QString& means, bool optional)
|
|
: __ArgvPackImpls(means, ParamType::FloatParam), key_name(key), optional_value(optional) { }
|
|
|
|
FloatOption::FloatOption(const QString& key, const QString& means, bool opt)
|
|
: __FloatArgvImpl(key, means, opt) {
|
|
setValue(u8"0");
|
|
}
|
|
|
|
int FloatOption::matchLenth() const {
|
|
return 1;
|
|
}
|
|
|
|
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:
|
|
QList<shared_ptr<IArgvPack>> args_mode;
|
|
int code_store;
|
|
|
|
public:
|
|
explicit MatchMode(const QList<shared_ptr<IArgvPack>> mode, int mode_code)
|
|
:args_mode(mode), code_store(mode_code) { }
|
|
|
|
/**
|
|
* @brief »ñȡģʽ´úÂë
|
|
* @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;
|
|
}
|
|
};
|
|
}
|
|
void ArgsParser::loadMode(int mode_code, const QList<shared_ptr<IArgvPack>>& args_model) {
|
|
this->match_modes.append(make_shared<MatchMode>(args_model, mode_code));
|
|
}
|
|
|
|
tuple<int, QList<shared_ptr<IArgvPack>>> ArgsParser::parse(int argc, char* argv[]) {
|
|
// ¾ÛºÏ²ÎÊýÊý×é
|
|
QList<QString> args_list;
|
|
for (int idx = 0; idx < argc; idx++) {
|
|
auto argv_str = QString::fromLocal8Bit(argv[idx]);
|
|
args_list.append(argv_str);
|
|
}
|
|
|
|
// ö¾ÙģʽƥÅä
|
|
for (auto& minst : this->match_modes) {
|
|
if (minst->parse(args_list, 0, 0))
|
|
return tuple<int, QList<shared_ptr<IArgvPack>>>(minst->modeCode(), minst->result());
|
|
}
|
|
|
|
return tuple<int, QList<shared_ptr<IArgvPack>>>(0, QList<shared_ptr<IArgvPack>>());
|
|
}
|