支持使用负数

This commit is contained in:
2024-06-30 15:23:29 +08:00
parent 5599e5d024
commit 68f377ceda
2 changed files with 17 additions and 7 deletions

View File

@@ -123,8 +123,6 @@ namespace ling {
throw OptionsException("重复提供参数:" + *it); throw OptionsException("重复提供参数:" + *it);
if (std::next(it) == cmd.end()) if (std::next(it) == cmd.end())
throw OptionsException("参数不足!" + *it + " 需要提供参数"); throw OptionsException("参数不足!" + *it + " 需要提供参数");
if (std::next(it)->at(0) == '-')
throw OptionsException("参数不足!" + *it + " 需要提供参数");
++it; ++it;
opt_str[rules->second.id] = *it; opt_str[rules->second.id] = *it;
break; break;
@@ -133,8 +131,6 @@ namespace ling {
throw OptionsException("重复提供参数:" + *it); throw OptionsException("重复提供参数:" + *it);
if (std::next(it) == cmd.end()) if (std::next(it) == cmd.end())
throw OptionsException("参数不足!" + *it + " 需要提供参数"); throw OptionsException("参数不足!" + *it + " 需要提供参数");
if (std::next(it)->at(0) == '-')
throw OptionsException("参数不足!" + *it + " 需要提供参数");
++it; ++it;
opt_int[rules->second.id] = toInt64(*it); opt_int[rules->second.id] = toInt64(*it);
break; break;
@@ -143,8 +139,6 @@ namespace ling {
throw OptionsException("重复提供参数:" + *it); throw OptionsException("重复提供参数:" + *it);
if (std::next(it) == cmd.end()) if (std::next(it) == cmd.end())
throw OptionsException("参数不足!" + *it + " 需要提供参数"); throw OptionsException("参数不足!" + *it + " 需要提供参数");
if (std::next(it)->at(0) == '-')
throw OptionsException("参数不足!" + *it + " 需要提供参数");
++it; ++it;
opt_double[rules->second.id] = std::stod(*it); opt_double[rules->second.id] = std::stod(*it);
break; break;

View File

@@ -6,6 +6,18 @@
#include <Order.h> #include <Order.h>
#include "OptionsException.h" #include "OptionsException.h"
TEST(Option, ) {
try {
ling::Order order({"--number", "-10"});
const auto number = order.addOption({"-n", "--number"}, ling::Order::INT);
order.analysis();
ASSERT_TRUE(number.isExistence());
ASSERT_EQ(number.getInt64(), -10);
} catch (const ling::OptionsException &e) {
ASSERT_FALSE(e.what());
}
}
TEST(Option, 16) { TEST(Option, 16) {
try { try {
ling::Order order({"--hex", "0x1F", "--hex2", "0x1f"}); ling::Order order({"--hex", "0x1F", "--hex2", "0x1f"});
@@ -66,8 +78,12 @@ TEST(Option, 异常参数测试) {
const auto cancel = order.addOption({"--cancel"}); const auto cancel = order.addOption({"--cancel"});
try { try {
order.analysis(); order.analysis();
ASSERT_FALSE("异常处理失败"); ASSERT_EQ(pts.getString(), "--all");
ASSERT_FALSE(all.isExistence());
ASSERT_TRUE(ok.isExistence());
ASSERT_FALSE(cancel.isExistence());
} catch (const ling::OptionsException &e) { } catch (const ling::OptionsException &e) {
ASSERT_FALSE("解析失败");
} }
} }