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-10-01 08:52:05 +00:00
|
|
|
|
__ArgvPackImpls::__ArgvPackImpls(const QString& means, ParamType t) : means_store(means), type_store(t) { }
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
// ͨ<><CDA8> IArgvPack <20>̳<EFBFBD>
|
|
|
|
|
ParamType __ArgvPackImpls::paramType() const {
|
2024-09-24 10:43:10 +00:00
|
|
|
|
return type_store;
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
QString __ArgvPackImpls::means() const {
|
2024-09-24 10:43:10 +00:00
|
|
|
|
return means_store;
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
void __ArgvPackImpls::setValue(const QString& v) {
|
2024-06-22 10:53:51 +00:00
|
|
|
|
this->value_store = v;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
QString __ArgvPackImpls::value() const {
|
2024-09-24 10:43:10 +00:00
|
|
|
|
return value_store;
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
FloatKeyValue::FloatKeyValue(const QString& key, const QString& means, bool optional) :
|
2024-10-01 15:23:54 +00:00
|
|
|
|
__FloatArgvImpls(key, means, optional) { }
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-10-01 15:23:54 +00:00
|
|
|
|
QString __FloatArgvImpls::bindKey() const {
|
2024-09-24 10:43:10 +00:00
|
|
|
|
return key_name;
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-10-01 15:23:54 +00:00
|
|
|
|
bool __FloatArgvImpls::optional() const {
|
2024-06-22 10:53:51 +00:00
|
|
|
|
return optional_value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 09:48:20 +00:00
|
|
|
|
QString args_parse::FloatKeyValue::placeHolder() const {
|
2024-10-01 10:17:40 +00:00
|
|
|
|
if (optional())
|
2024-10-01 09:48:20 +00:00
|
|
|
|
return QString("[--%1 <%2>]").arg(bindKey(), bindKey());
|
|
|
|
|
return QString("--%1 <%2>").arg(bindKey(), bindKey());
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
int FloatKeyValue::matchLenth() const {
|
2024-06-22 10:53:51 +00:00
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 08:52:05 +00:00
|
|
|
|
bool FloatKeyValue::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-10-01 15:23:54 +00:00
|
|
|
|
if (args_t == u8"--" + bindKey()){
|
2024-06-22 10:53:51 +00:00
|
|
|
|
setValue(args_v);
|
2024-10-01 15:23:54 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 09:48:20 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 15:23:54 +00:00
|
|
|
|
__FloatArgvImpls::__FloatArgvImpls(const QString& key, const QString& means, bool optional)
|
2024-10-01 08:52:05 +00:00
|
|
|
|
: __ArgvPackImpls(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)
|
2024-10-01 15:23:54 +00:00
|
|
|
|
: __FloatArgvImpls(key, means, opt) {
|
2024-09-24 10:43:10 +00:00
|
|
|
|
setValue(u8"0");
|
|
|
|
|
}
|
2024-06-22 10:53:51 +00:00
|
|
|
|
|
2024-10-01 09:48:20 +00:00
|
|
|
|
QString args_parse::FloatOption::placeHolder() const {
|
2024-10-01 10:17:40 +00:00
|
|
|
|
if (optional())
|
2024-10-01 09:48:20 +00:00
|
|
|
|
return QString("[--%1]").arg(bindKey());
|
|
|
|
|
return QString("--%1").arg(bindKey());
|
|
|
|
|
}
|
|
|
|
|
|
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()));
|
2024-10-01 15:23:54 +00:00
|
|
|
|
return args_t == u8"--" + bindKey();
|
2024-06-22 10:53:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 15:23:54 +00:00
|
|
|
|
MatchMode::MatchMode(int mode_code, const QString& mode)
|
|
|
|
|
:_means_explain(mode), code_store(mode_code) { }
|
2024-10-01 10:17:40 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>ȡģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
|
2024-10-01 15:23:54 +00:00
|
|
|
|
MatchMode& args_parse::MatchMode::operator<<(std::shared_ptr<IArgvPack> unit) {
|
|
|
|
|
args_mode << unit;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:17:40 +00:00
|
|
|
|
int MatchMode::modeCode() const {
|
|
|
|
|
return code_store;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 15:23:54 +00:00
|
|
|
|
QString args_parse::MatchMode::usageString() const {
|
|
|
|
|
QString usage_string;
|
|
|
|
|
for (auto& item : args_mode)
|
|
|
|
|
usage_string += item->placeHolder() + u8" ";
|
2024-10-01 10:17:40 +00:00
|
|
|
|
|
2024-10-01 15:23:54 +00:00
|
|
|
|
return usage_string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString args_parse::MatchMode::explanString() const {
|
|
|
|
|
QStringList sections;
|
|
|
|
|
sections << u8" " + _means_explain;
|
|
|
|
|
sections << QString(" Switch:");
|
|
|
|
|
|
|
|
|
|
for (auto& item : args_mode) {
|
|
|
|
|
sections << " " + item->placeHolder();
|
|
|
|
|
sections << " " + item->means();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sections.join("\n");
|
2024-10-01 10:17:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
bool MatchMode::parse(const QList<QString>& args, int argv_index, int parse_index) {
|
|
|
|
|
if (argv_index >= args.size())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// <20><>ȡģʽƥ<CABD>䵥Ԫ
|
|
|
|
|
auto parse_unit = args_mode[parse_index];
|
|
|
|
|
switch (parse_unit->paramType()) {
|
|
|
|
|
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>
|
|
|
|
|
{
|
2024-10-01 15:23:54 +00:00
|
|
|
|
QList<shared_ptr<__FloatArgvImpls>> float_parsers;
|
2024-10-01 10:17:40 +00:00
|
|
|
|
|
|
|
|
|
for (auto& unit : args_mode) {
|
|
|
|
|
if (unit->paramType() == ParamType::FloatParam)
|
2024-10-01 15:23:54 +00:00
|
|
|
|
float_parsers << dynamic_pointer_cast<__FloatArgvImpls>(unit);
|
2024-10-01 10:17:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
}
|
2024-10-01 10:17:40 +00:00
|
|
|
|
|
2024-10-01 15:23:54 +00:00
|
|
|
|
std::shared_ptr<IArgvPack> args_parse::MatchMode::getUnitViaKey(const QString& key) {
|
|
|
|
|
for (auto& u : args_mode) {
|
|
|
|
|
if (u->paramType() == ParamType::FloatParam) {
|
|
|
|
|
auto conv = std::dynamic_pointer_cast<__FloatArgvImpls>(u);
|
|
|
|
|
if (conv->bindKey() == key)
|
|
|
|
|
return u;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::shared_ptr<IArgvPack>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<IArgvPack> args_parse::MatchMode::getUnitViaPos(int pos) {
|
|
|
|
|
return args_mode[pos];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString args_parse::ArgsParser::helpDoc() const {
|
|
|
|
|
QString help_string;
|
|
|
|
|
for (auto& mode : this->match_modes) {
|
|
|
|
|
help_string += "Usage:" + mode->usageString() + "\n";
|
|
|
|
|
help_string += mode->explanString() + "\n\n";
|
|
|
|
|
}
|
|
|
|
|
return help_string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArgsParser& args_parse::ArgsParser::operator<<(std::shared_ptr<MatchMode> mode) {
|
|
|
|
|
this->match_modes.append(mode);
|
|
|
|
|
return *this;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 15:23:54 +00:00
|
|
|
|
std::shared_ptr<MatchMode> ArgsParser::parse(int argc, char* argv[]) {
|
2024-09-24 10:43:10 +00:00
|
|
|
|
// <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-10-01 15:23:54 +00:00
|
|
|
|
return minst;
|
2024-06-22 10:53:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 15:23:54 +00:00
|
|
|
|
return std::shared_ptr<MatchMode>();
|
2024-06-22 10:53:51 +00:00
|
|
|
|
}
|