165 lines
4.1 KiB
C++
165 lines
4.1 KiB
C++
#include "argsparser.h"
|
|
|
|
using namespace args_parse;
|
|
using namespace std;
|
|
|
|
ArgvPackImpl::ArgvPackImpl(const QString& means, ParamType t) : means_store(means), type_store(t) { }
|
|
|
|
// ͨ¹ý ArgvPack ¼Ì³Ð
|
|
ParamType ArgvPackImpl::paramType() const {
|
|
return type_store;
|
|
}
|
|
|
|
QString ArgvPackImpl::means() const {
|
|
return means_store;
|
|
}
|
|
|
|
void ArgvPackImpl::setValue(const QString& v) {
|
|
this->value_store = v;
|
|
}
|
|
|
|
QString ArgvPackImpl::value() const {
|
|
return value_store;
|
|
}
|
|
|
|
FloatArgvPack::FloatArgvPack(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 FloatArgvPack::matchLenth() const {
|
|
return 2;
|
|
}
|
|
|
|
bool FloatArgvPack::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) : ArgvPackImpl(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)
|
|
: ArgvPackImpl(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<ArgvPack>> args_mode;
|
|
int code_store;
|
|
|
|
public:
|
|
explicit MatchMode(const QList<shared_ptr<ArgvPack>> mode, int mode_code)
|
|
:args_mode(mode), code_store(mode_code) { }
|
|
|
|
/**
|
|
* @brief »ñȡģʽ´úÂë
|
|
* @return ģʽ´úÂë
|
|
*/
|
|
int modeCode()const {
|
|
return code_store;
|
|
}
|
|
/**
|
|
* @brief »ñȡģʽƥÅä½á¹û
|
|
* @return ģʽƥÅä½á¹û
|
|
*/
|
|
QList<shared_ptr<ArgvPack>> 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<ArgvPack>>& args_model) {
|
|
this->match_modes.append(make_shared<MatchMode>(args_model, mode_code));
|
|
}
|
|
|
|
tuple<int, QList<shared_ptr<ArgvPack>>> 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<ArgvPack>>>(minst->modeCode(), minst->result());
|
|
}
|
|
|
|
return tuple<int, QList<shared_ptr<ArgvPack>>>(0, QList<shared_ptr<ArgvPack>>());
|
|
}
|