diff --git a/src/Order.cpp b/src/Order.cpp index 35e8e07..9aecd82 100644 --- a/src/Order.cpp +++ b/src/Order.cpp @@ -123,8 +123,6 @@ namespace ling { throw OptionsException("重复提供参数:" + *it); if (std::next(it) == cmd.end()) throw OptionsException("参数不足!" + *it + " 需要提供参数"); - if (std::next(it)->at(0) == '-') - throw OptionsException("参数不足!" + *it + " 需要提供参数"); ++it; opt_str[rules->second.id] = *it; break; @@ -133,8 +131,6 @@ namespace ling { throw OptionsException("重复提供参数:" + *it); if (std::next(it) == cmd.end()) throw OptionsException("参数不足!" + *it + " 需要提供参数"); - if (std::next(it)->at(0) == '-') - throw OptionsException("参数不足!" + *it + " 需要提供参数"); ++it; opt_int[rules->second.id] = toInt64(*it); break; @@ -143,8 +139,6 @@ namespace ling { throw OptionsException("重复提供参数:" + *it); if (std::next(it) == cmd.end()) throw OptionsException("参数不足!" + *it + " 需要提供参数"); - if (std::next(it)->at(0) == '-') - throw OptionsException("参数不足!" + *it + " 需要提供参数"); ++it; opt_double[rules->second.id] = std::stod(*it); break; diff --git a/test_main.cpp b/test_main.cpp index 5c0dbd7..e2f5fec 100644 --- a/test_main.cpp +++ b/test_main.cpp @@ -6,6 +6,18 @@ #include #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进制测试) { try { ling::Order order({"--hex", "0x1F", "--hex2", "0x1f"}); @@ -66,8 +78,12 @@ TEST(Option, 异常参数测试) { const auto cancel = order.addOption({"--cancel"}); try { 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) { + ASSERT_FALSE("解析失败"); } }