为了规避NDK的Bug,停用异常处理
This commit is contained in:
@@ -50,7 +50,8 @@ namespace ling {
|
|||||||
//匿名参数数量
|
//匿名参数数量
|
||||||
int anonymityNumber = 0;
|
int anonymityNumber = 0;
|
||||||
int nextID = 1;
|
int nextID = 1;
|
||||||
std::string errStr;
|
std::string nullstr;
|
||||||
|
std::string err;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int64_t getInt64(int id) const;
|
int64_t getInt64(int id) const;
|
||||||
@@ -59,7 +60,7 @@ namespace ling {
|
|||||||
|
|
||||||
const std::string &getString(int id) const;
|
const std::string &getString(int id) const;
|
||||||
|
|
||||||
bool getOption(int id) const;
|
bool getOption(int id);
|
||||||
|
|
||||||
bool isExistence(int id) const;
|
bool isExistence(int id) const;
|
||||||
|
|
||||||
@@ -77,7 +78,9 @@ namespace ling {
|
|||||||
const std::vector<std::string> &getAnonymity() const;
|
const std::vector<std::string> &getAnonymity() const;
|
||||||
|
|
||||||
/// 解析
|
/// 解析
|
||||||
void analysis();
|
bool analysis();
|
||||||
|
|
||||||
|
const std::string &getError() const;
|
||||||
|
|
||||||
friend class Options;
|
friend class Options;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#define THROW(errStr) do{ this->err = errStr; return false; } while(false)
|
||||||
|
|
||||||
namespace ling {
|
namespace ling {
|
||||||
Order::DataStruct::DataStruct(const std::vector<std::string> &option, const int id, const Type type, const std::string &memage) {
|
Order::DataStruct::DataStruct(const std::vector<std::string> &option, const int id, const Type type, const std::string &memage) {
|
||||||
this->options = option;
|
this->options = option;
|
||||||
@@ -35,15 +37,15 @@ namespace ling {
|
|||||||
const std::string &Order::getString(const int id) const {
|
const std::string &Order::getString(const int id) const {
|
||||||
const auto it = opt_str.find(id);
|
const auto it = opt_str.find(id);
|
||||||
if (it == opt_str.end()) {
|
if (it == opt_str.end()) {
|
||||||
return errStr;
|
return nullstr;
|
||||||
}
|
}
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Order::getOption(const int id) const {
|
bool Order::getOption(const int id) {
|
||||||
const auto it = options.find(id);
|
const auto it = options.find(id);
|
||||||
if (it == options.end())
|
if (it == options.end())
|
||||||
throw std::runtime_error("无效键");
|
THROW("无效键");
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,55 +88,60 @@ namespace ling {
|
|||||||
return anonymity;
|
return anonymity;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Order::analysis() {
|
bool Order::analysis() {
|
||||||
for (auto it = cmd.begin(); it != cmd.end(); ++it) {
|
for (auto it = cmd.begin(); it != cmd.end(); ++it) {
|
||||||
auto rules = data.find(*it);
|
auto rules = data.find(*it);
|
||||||
if (rules == data.end()) {
|
if (rules == data.end()) {
|
||||||
//检查匿名参数数量
|
//检查匿名参数数量
|
||||||
if (anonymity.size() >= anonymityNumber)
|
if (anonymity.size() >= anonymityNumber)
|
||||||
throw std::runtime_error("无法解析的参数:" + *it);
|
THROW("无法解析的参数:" + *it);
|
||||||
anonymity.push_back(*it);
|
anonymity.push_back(*it);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (it->at(0) != '-')
|
if (it->at(0) != '-')
|
||||||
throw std::runtime_error("无法解析的参数:" + *it);
|
THROW("无法解析的参数:" + *it);
|
||||||
switch (rules->second.type) {
|
switch (rules->second.type) {
|
||||||
case OPT:
|
case OPT:
|
||||||
if (options[rules->second.id])
|
if (options[rules->second.id])
|
||||||
throw std::runtime_error("重复提供参数:" + *it);
|
THROW("重复提供参数:" + *it);
|
||||||
options[rules->second.id] = true;
|
options[rules->second.id] = true;
|
||||||
break;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
if (opt_str.find(rules->second.id) != opt_str.end())
|
if (opt_str.find(rules->second.id) != opt_str.end())
|
||||||
throw std::runtime_error("重复提供参数:" + *it);
|
THROW("重复提供参数:" + *it);
|
||||||
if (std::next(it) == cmd.end())
|
if (std::next(it) == cmd.end())
|
||||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
THROW("参数不足!" + *it + " 需要提供参数");
|
||||||
if (std::next(it)->at(0) == '-')
|
if (std::next(it)->at(0) == '-')
|
||||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
THROW("参数不足!" + *it + " 需要提供参数");
|
||||||
++it;
|
++it;
|
||||||
opt_str[rules->second.id] = *it;
|
opt_str[rules->second.id] = *it;
|
||||||
break;
|
break;
|
||||||
case INT:
|
case INT:
|
||||||
if (opt_int.find(rules->second.id) != opt_int.end())
|
if (opt_int.find(rules->second.id) != opt_int.end())
|
||||||
throw std::runtime_error("重复提供参数:" + *it);
|
THROW("重复提供参数:" + *it);
|
||||||
if (std::next(it) == cmd.end())
|
if (std::next(it) == cmd.end())
|
||||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
THROW("参数不足!" + *it + " 需要提供参数");
|
||||||
if (std::next(it)->at(0) == '-')
|
if (std::next(it)->at(0) == '-')
|
||||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
THROW("参数不足!" + *it + " 需要提供参数");
|
||||||
++it;
|
++it;
|
||||||
opt_int[rules->second.id] = std::stoll(*it);
|
opt_int[rules->second.id] = std::stoll(*it);
|
||||||
break;
|
break;
|
||||||
case DOUBLE:
|
case DOUBLE:
|
||||||
if (opt_double.find(rules->second.id) != opt_double.end())
|
if (opt_double.find(rules->second.id) != opt_double.end())
|
||||||
throw std::runtime_error("重复提供参数:" + *it);
|
THROW("重复提供参数:" + *it);
|
||||||
if (std::next(it) == cmd.end())
|
if (std::next(it) == cmd.end())
|
||||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
THROW("参数不足!" + *it + " 需要提供参数");
|
||||||
if (std::next(it)->at(0) == '-')
|
if (std::next(it)->at(0) == '-')
|
||||||
throw std::runtime_error("参数不足!" + *it + " 需要提供参数");
|
THROW("参数不足!" + *it + " 需要提供参数");
|
||||||
++it;
|
++it;
|
||||||
opt_double[rules->second.id] = std::stod(*it);
|
opt_double[rules->second.id] = std::stod(*it);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string &Order::getError() const {
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
} // ling
|
} // ling
|
||||||
|
|||||||
@@ -6,85 +6,78 @@
|
|||||||
#include <Order.h>
|
#include <Order.h>
|
||||||
|
|
||||||
TEST(Option, 选项测试) {
|
TEST(Option, 选项测试) {
|
||||||
try {
|
|
||||||
ling::Order order({"-pts", "--all", "--ok"});
|
ling::Order order({"-pts", "--all", "--ok"});
|
||||||
const auto pts = order.addOption({"-pts", "--pts"});
|
const auto pts = order.addOption({"-pts", "--pts"});
|
||||||
const auto all = order.addOption({"-all", "--all"});
|
const auto all = order.addOption({"-all", "--all"});
|
||||||
const auto ok = order.addOption({"--ok", "-ok"});
|
const auto ok = order.addOption({"--ok", "-ok"});
|
||||||
const auto cancel = order.addOption({"--cancel"});
|
const auto cancel = order.addOption({"--cancel"});
|
||||||
order.analysis();
|
if (!order.analysis()) {
|
||||||
|
std::cout << order.getError() << std::endl;
|
||||||
|
ASSERT_FALSE("解析出错!");
|
||||||
|
}
|
||||||
ASSERT_TRUE(pts.isOption());
|
ASSERT_TRUE(pts.isOption());
|
||||||
ASSERT_TRUE(all.isOption());
|
ASSERT_TRUE(all.isOption());
|
||||||
ASSERT_TRUE(ok.isOption());
|
ASSERT_TRUE(ok.isOption());
|
||||||
ASSERT_FALSE(cancel.isOption());
|
ASSERT_FALSE(cancel.isOption());
|
||||||
} catch (const std::runtime_error &e) {
|
|
||||||
std::cout << e.what() << std::endl;
|
|
||||||
ASSERT_FALSE("解析出错!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Option, 参数测试) {
|
TEST(Option, 参数测试) {
|
||||||
try {
|
|
||||||
ling::Order order({"-pts", "hello world!", "--all", "--ok"});
|
ling::Order order({"-pts", "hello world!", "--all", "--ok"});
|
||||||
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
||||||
const auto all = order.addOption({"-all", "--all"});
|
const auto all = order.addOption({"-all", "--all"});
|
||||||
const auto ok = order.addOption({"--ok", "-ok"});
|
const auto ok = order.addOption({"--ok", "-ok"});
|
||||||
const auto cancel = order.addOption({"--cancel"});
|
const auto cancel = order.addOption({"--cancel"});
|
||||||
order.analysis();
|
if (!order.analysis()) {
|
||||||
|
std::cout << order.getError() << std::endl;
|
||||||
|
ASSERT_FALSE("解析出错!");
|
||||||
|
}
|
||||||
ASSERT_TRUE(all.isOption());
|
ASSERT_TRUE(all.isOption());
|
||||||
ASSERT_TRUE(ok.isOption());
|
ASSERT_TRUE(ok.isOption());
|
||||||
ASSERT_FALSE(cancel.isOption());
|
ASSERT_FALSE(cancel.isOption());
|
||||||
ASSERT_TRUE(pts.getString() == "hello world!");
|
ASSERT_TRUE(pts.getString() == "hello world!");
|
||||||
} catch (const std::runtime_error &e) {
|
|
||||||
std::cout << e.what() << std::endl;
|
|
||||||
ASSERT_FALSE("解析出错!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST(Option, 异常参数测试) {
|
TEST(Option, 异常参数测试) {
|
||||||
try {
|
|
||||||
ling::Order order({"-pts", "--all", "--ok"});
|
ling::Order order({"-pts", "--all", "--ok"});
|
||||||
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
||||||
const auto all = order.addOption({"-all", "--all"});
|
const auto all = order.addOption({"-all", "--all"});
|
||||||
const auto ok = order.addOption({"--ok", "-ok"});
|
const auto ok = order.addOption({"--ok", "-ok"});
|
||||||
const auto cancel = order.addOption({"--cancel"});
|
const auto cancel = order.addOption({"--cancel"});
|
||||||
order.analysis();
|
ASSERT_FALSE(order.analysis());
|
||||||
ASSERT_FALSE("纠正出错!");
|
|
||||||
} catch (const std::runtime_error &e) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Option, 匿名参数测试) {
|
TEST(Option, 匿名参数测试) {
|
||||||
try {
|
|
||||||
ling::Order order({"-pts", "hello world!", "hello!", "--all", "--ok"});
|
ling::Order order({"-pts", "hello world!", "hello!", "--all", "--ok"});
|
||||||
order.addAnonymity(1);
|
order.addAnonymity(1);
|
||||||
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
||||||
const auto all = order.addOption({"-all", "--all"});
|
const auto all = order.addOption({"-all", "--all"});
|
||||||
const auto ok = order.addOption({"--ok", "-ok"});
|
const auto ok = order.addOption({"--ok", "-ok"});
|
||||||
const auto cancel = order.addOption({"--cancel"});
|
const auto cancel = order.addOption({"--cancel"});
|
||||||
order.analysis();
|
if (!order.analysis()) {
|
||||||
|
std::cout << order.getError() << std::endl;
|
||||||
|
ASSERT_FALSE("解析出错!");
|
||||||
|
}
|
||||||
ASSERT_TRUE(all.isOption());
|
ASSERT_TRUE(all.isOption());
|
||||||
ASSERT_TRUE(ok.isOption());
|
ASSERT_TRUE(ok.isOption());
|
||||||
ASSERT_FALSE(cancel.isOption());
|
ASSERT_FALSE(cancel.isOption());
|
||||||
ASSERT_TRUE(pts.getString() == "hello world!");
|
ASSERT_TRUE(pts.getString() == "hello world!");
|
||||||
ASSERT_TRUE(order.getAnonymity().size() == 1);
|
ASSERT_TRUE(order.getAnonymity().size() == 1);
|
||||||
ASSERT_TRUE(order.getAnonymity()[0] == "hello!");
|
ASSERT_TRUE(order.getAnonymity()[0] == "hello!");
|
||||||
} catch (const std::runtime_error &e) {
|
|
||||||
std::cout << e.what() << std::endl;
|
|
||||||
ASSERT_FALSE("解析出错!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Option, 匿名参数测试2) {
|
TEST(Option, 匿名参数测试2) {
|
||||||
try {
|
|
||||||
ling::Order order({"-pts", "hello world!", "hello!", "--all", "--ok", "world"});
|
ling::Order order({"-pts", "hello world!", "hello!", "--all", "--ok", "world"});
|
||||||
order.addAnonymity(2);
|
order.addAnonymity(2);
|
||||||
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
||||||
const auto all = order.addOption({"-all", "--all"});
|
const auto all = order.addOption({"-all", "--all"});
|
||||||
const auto ok = order.addOption({"--ok", "-ok"});
|
const auto ok = order.addOption({"--ok", "-ok"});
|
||||||
const auto cancel = order.addOption({"--cancel"});
|
const auto cancel = order.addOption({"--cancel"});
|
||||||
order.analysis();
|
if (!order.analysis()) {
|
||||||
|
std::cout << order.getError() << std::endl;
|
||||||
|
ASSERT_FALSE("解析出错!");
|
||||||
|
}
|
||||||
|
|
||||||
ASSERT_TRUE(all.isOption());
|
ASSERT_TRUE(all.isOption());
|
||||||
ASSERT_TRUE(ok.isOption());
|
ASSERT_TRUE(ok.isOption());
|
||||||
ASSERT_FALSE(cancel.isOption());
|
ASSERT_FALSE(cancel.isOption());
|
||||||
@@ -92,22 +85,15 @@ TEST(Option, 匿名参数测试2) {
|
|||||||
ASSERT_TRUE(order.getAnonymity().size() == 2);
|
ASSERT_TRUE(order.getAnonymity().size() == 2);
|
||||||
ASSERT_TRUE(order.getAnonymity()[0] == "hello!");
|
ASSERT_TRUE(order.getAnonymity()[0] == "hello!");
|
||||||
ASSERT_TRUE(order.getAnonymity()[1] == "world");
|
ASSERT_TRUE(order.getAnonymity()[1] == "world");
|
||||||
} catch (const std::runtime_error &e) {
|
|
||||||
std::cout << e.what() << std::endl;
|
|
||||||
ASSERT_FALSE("解析出错!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Option, 匿名参数测试3) {
|
TEST(Option, 匿名参数测试3) {
|
||||||
try {
|
|
||||||
ling::Order order({"-pts", "hello world!", "hello!", "--all", "--ok", "world"});
|
ling::Order order({"-pts", "hello world!", "hello!", "--all", "--ok", "world"});
|
||||||
order.addAnonymity(1);
|
order.addAnonymity(1);
|
||||||
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
||||||
const auto all = order.addOption({"-all", "--all"});
|
const auto all = order.addOption({"-all", "--all"});
|
||||||
const auto ok = order.addOption({"--ok", "-ok"});
|
const auto ok = order.addOption({"--ok", "-ok"});
|
||||||
const auto cancel = order.addOption({"--cancel"});
|
const auto cancel = order.addOption({"--cancel"});
|
||||||
order.analysis();
|
if (order.analysis())
|
||||||
ASSERT_FALSE("匿名参数数量超出限制,但是没有抛出异常!");
|
ASSERT_FALSE("匿名参数数量超出限制,但是没有抛出异常!");
|
||||||
} catch (const std::runtime_error &e) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user