提交
This commit is contained in:
32
src/Options.cpp
Normal file
32
src/Options.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// Created by ling on 24-2-4.
|
||||
//
|
||||
|
||||
#include "Options.h"
|
||||
|
||||
namespace ling {
|
||||
Options::Options(const int id, Order *const ptr) {
|
||||
this->id = id;
|
||||
this->ptr = ptr;
|
||||
}
|
||||
|
||||
int64_t Options::getInt64() const {
|
||||
return ptr->getInt64(id);
|
||||
}
|
||||
|
||||
double Options::getDouble() const {
|
||||
return ptr->getDouble(id);
|
||||
}
|
||||
|
||||
const std::string &Options::getString() const {
|
||||
return ptr->getString(id);
|
||||
}
|
||||
|
||||
bool Options::isOption() const {
|
||||
return ptr->getOption(id);
|
||||
}
|
||||
|
||||
bool Options::isExistence() const {
|
||||
return ptr->isExistence(id);
|
||||
}
|
||||
} // ling
|
||||
140
src/Order.cpp
Normal file
140
src/Order.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
//
|
||||
// Created by ling on 24-2-4.
|
||||
//
|
||||
|
||||
#include "../include/Order.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace ling {
|
||||
Order::DataStruct::DataStruct(const std::vector<std::string> &option, const int id, const Type type, const std::string &memage) {
|
||||
this->options = option;
|
||||
this->id = id;
|
||||
this->type = type;
|
||||
this->memage = memage;
|
||||
}
|
||||
|
||||
Order::DataStruct::DataStruct() = default;
|
||||
|
||||
int64_t Order::getInt64(const int id) const {
|
||||
const auto it = opt_int.find(id);
|
||||
if (it == opt_int.end()) {
|
||||
return 0;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
double Order::getDouble(int id) const {
|
||||
const auto it = opt_double.find(id);
|
||||
if (it == opt_double.end()) {
|
||||
return 0;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
const std::string &Order::getString(const int id) const {
|
||||
const auto it = opt_str.find(id);
|
||||
if (it == opt_str.end()) {
|
||||
return errStr;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
bool Order::getOption(const int id) const {
|
||||
const auto it = options.find(id);
|
||||
if (it == options.end())
|
||||
throw std::runtime_error("无效键");
|
||||
return it->second;
|
||||
}
|
||||
|
||||
bool Order::isExistence(const int id) const {
|
||||
if (options.find(id) != options.end()) {
|
||||
return options.find(id)->second;
|
||||
}
|
||||
if (opt_int.find(id) != opt_int.end())
|
||||
return true;
|
||||
if (opt_double.find(id) != opt_double.end())
|
||||
return true;
|
||||
if (opt_str.find(id) != opt_str.end())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
Order::Order(const std::vector<std::string> &temp) {
|
||||
this->cmd = temp;
|
||||
}
|
||||
|
||||
Options Order::addOption(const std::vector<std::string> &opt, const std::string &message) {
|
||||
return addOption(opt, OPT, message);
|
||||
}
|
||||
|
||||
Options Order::addOption(const std::vector<std::string> &opt, Type type, const std::string &message) {
|
||||
const int id = nextID++;
|
||||
for (const auto &in: opt)
|
||||
data[in] = DataStruct(opt, id, type, message);
|
||||
|
||||
if (type == OPT)
|
||||
options[id] = false;
|
||||
return {id, this};
|
||||
}
|
||||
|
||||
void Order::addAnonymity(const int number) {
|
||||
this->anonymityNumber = number;
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Order::getAnonymity() const {
|
||||
return anonymity;
|
||||
}
|
||||
|
||||
void Order::analysis() {
|
||||
for (auto it = cmd.begin(); it != cmd.end(); ++it) {
|
||||
auto rules = data.find(*it);
|
||||
if (rules == data.end()) {
|
||||
//检查匿名参数数量
|
||||
if (anonymity.size() >= anonymityNumber)
|
||||
throw std::runtime_error("无法解析的参数:" + *it);
|
||||
anonymity.push_back(*it);
|
||||
continue;
|
||||
}
|
||||
if (it->at(0) != '-')
|
||||
throw std::runtime_error("无法解析的参数:" + *it);
|
||||
switch (rules->second.type) {
|
||||
case OPT:
|
||||
if (options[rules->second.id])
|
||||
throw std::runtime_error("重复提供参数:" + *it);
|
||||
options[rules->second.id] = true;
|
||||
break;
|
||||
case STRING:
|
||||
if (opt_str.find(rules->second.id) != opt_str.end())
|
||||
throw std::runtime_error("重复提供参数:" + *it);
|
||||
if (std::next(it) == cmd.end())
|
||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
||||
if (std::next(it)->at(0) == '-')
|
||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
||||
++it;
|
||||
opt_str[rules->second.id] = *it;
|
||||
break;
|
||||
case INT:
|
||||
if (opt_int.find(rules->second.id) != opt_int.end())
|
||||
throw std::runtime_error("重复提供参数:" + *it);
|
||||
if (std::next(it) == cmd.end())
|
||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
||||
if (std::next(it)->at(0) == '-')
|
||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
||||
++it;
|
||||
opt_int[rules->second.id] = std::stoll(*it);
|
||||
break;
|
||||
case DOUBLE:
|
||||
if (opt_double.find(rules->second.id) != opt_double.end())
|
||||
throw std::runtime_error("重复提供参数:" + *it);
|
||||
if (std::next(it) == cmd.end())
|
||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
||||
if (std::next(it)->at(0) == '-')
|
||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
||||
++it;
|
||||
opt_double[rules->second.id] = std::stod(*it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // ling
|
||||
Reference in New Issue
Block a user