增加16进制支持
This commit is contained in:
@@ -91,6 +91,15 @@ namespace ling {
|
|||||||
return anonymity;
|
return anonymity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 自动处理16进制
|
||||||
|
static inline int64_t toInt64(const std::string &str) {
|
||||||
|
if (str.find("0x") == std::string::npos)
|
||||||
|
return std::stoll(str);
|
||||||
|
int64_t value;
|
||||||
|
::sscanf(str.c_str(), "0x%lxx", &value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
void Order::analysis() {
|
void 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);
|
||||||
@@ -127,7 +136,7 @@ namespace ling {
|
|||||||
if (std::next(it)->at(0) == '-')
|
if (std::next(it)->at(0) == '-')
|
||||||
throw OptionsException("参数不足!" + *it + " 需要提供参数");
|
throw OptionsException("参数不足!" + *it + " 需要提供参数");
|
||||||
++it;
|
++it;
|
||||||
opt_int[rules->second.id] = std::stoll(*it);
|
opt_int[rules->second.id] = toInt64(*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())
|
||||||
|
|||||||
@@ -4,37 +4,57 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <Options.h>
|
#include <Options.h>
|
||||||
#include <Order.h>
|
#include <Order.h>
|
||||||
|
#include "OptionsException.h"
|
||||||
|
|
||||||
|
TEST(Option, 16进制测试) {
|
||||||
|
try {
|
||||||
|
ling::Order order({"--hex", "0x1F", "--hex2", "0x1f"});
|
||||||
|
const auto hex = order.addOption({"--hex"}, ling::Order::INT);
|
||||||
|
const auto hex2 = order.addOption({"--hex2"}, ling::Order::INT);
|
||||||
|
order.analysis();
|
||||||
|
ASSERT_TRUE(hex.isExistence());
|
||||||
|
ASSERT_TRUE(hex2.isExistence());
|
||||||
|
ASSERT_EQ(hex.getInt64(), 0x1f);
|
||||||
|
ASSERT_EQ(hex2.getInt64(), 0x1f);
|
||||||
|
} catch (const ling::OptionsException &e) {
|
||||||
|
ASSERT_FALSE(e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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"});
|
||||||
if (!order.analysis()) {
|
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 ling::OptionsException &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"});
|
||||||
if (!order.analysis()) {
|
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 ling::OptionsException &e) {
|
||||||
|
std::cout << e.what() << std::endl;
|
||||||
|
ASSERT_FALSE("解析出错!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -44,39 +64,43 @@ TEST(Option, 异常参数测试) {
|
|||||||
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"});
|
||||||
ASSERT_FALSE(order.analysis());
|
try {
|
||||||
|
order.analysis();
|
||||||
|
ASSERT_FALSE("异常处理失败");
|
||||||
|
} catch (const ling::OptionsException &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"});
|
||||||
if (!order.analysis()) {
|
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 ling::OptionsException &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"});
|
||||||
if (!order.analysis()) {
|
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());
|
||||||
@@ -85,6 +109,10 @@ 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 ling::OptionsException &e) {
|
||||||
|
std::cout << e.what() << std::endl;
|
||||||
|
ASSERT_FALSE("解析出错!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Option, 匿名参数测试3) {
|
TEST(Option, 匿名参数测试3) {
|
||||||
@@ -94,8 +122,11 @@ TEST(Option, 匿名参数测试3) {
|
|||||||
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"});
|
||||||
if (order.analysis())
|
try {
|
||||||
|
order.analysis();
|
||||||
ASSERT_FALSE("匿名参数数量超出限制,但是没有抛出异常!");
|
ASSERT_FALSE("匿名参数数量超出限制,但是没有抛出异常!");
|
||||||
|
} catch (const ling::OptionsException &e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user