From 910ee43713f95780bce3539f47f1443682c224c9 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 2 Oct 2025 18:44:43 +0200 Subject: [PATCH 1/7] Enable parsing negative numbers as values for positional arguments --- include/argparse.hpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index eba3f6d..e0dd0c3 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -1030,7 +1030,18 @@ namespace argparse past_pseudo_arg = true; return false; } - return !token.m_token.starts_with("-"); + if (!token.m_token.starts_with("-")) + { + return true; + } + auto iss = std::istringstream(token.m_token); + auto num = int(); + iss >> num; + if (!iss.fail() && (iss.eof() || iss.peek() == std::istringstream::traits_type::eof())) + { + return true; + } + return false; }); } From 2ee1ab528116be34fe66cce07b213b027d2aae30 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 2 Oct 2025 18:45:03 +0200 Subject: [PATCH 2/7] Unskip unit test --- test/unittest/test_parsing_positional.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/test_parsing_positional.cpp b/test/unittest/test_parsing_positional.cpp index bc43819..83dbe62 100644 --- a/test/unittest/test_parsing_positional.cpp +++ b/test/unittest/test_parsing_positional.cpp @@ -1935,7 +1935,7 @@ TEST_CASE("Parsing a positional argument yields correct value for positive numbe CHECK(args.get_value("num") == 65); } -TEST_CASE("Parsing a positional argument yields correct value for negative number" * doctest::skip()) +TEST_CASE("Parsing a positional argument yields correct value for negative number") { auto parser = argparse::ArgumentParser(); parser.add_argument("num").type(); From 085ab8570b1958b6e0132d6806d3489ddea0ddd9 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 2 Oct 2025 18:50:11 +0200 Subject: [PATCH 3/7] Extend unit test --- test/unittest/test_parsing_positional.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/unittest/test_parsing_positional.cpp b/test/unittest/test_parsing_positional.cpp index 83dbe62..eeadfdb 100644 --- a/test/unittest/test_parsing_positional.cpp +++ b/test/unittest/test_parsing_positional.cpp @@ -1915,14 +1915,23 @@ TEST_CASE("Parsing missing positional argument with nargs set throws an exceptio CHECK_THROWS_WITH_AS(parser.parse_args(1, cstr_arr{"prog"}), "the following arguments are required: p1 p2 p3", argparse::parsing_error); } -TEST_CASE("Parsing a positional argument yields correct value for positive number") +TEST_CASE_TEMPLATE("Parsing a positional argument yields correct value for positive number", T, short int, unsigned short int, int, unsigned int, long int, unsigned long int, long long int, unsigned long long int, float, double, long double) { auto parser = argparse::ArgumentParser(); - parser.add_argument("num").type(); + parser.add_argument("num").type(); - auto const args = parser.parse_args(2, cstr_arr{"prog", "65"}); + if constexpr (std::is_integral_v) + { + auto const args = parser.parse_args(2, cstr_arr{"prog", "65"}); - CHECK(args.get_value("num") == 65); + CHECK(args.get_value("num") == T(65)); + } + else + { + auto const args = parser.parse_args(2, cstr_arr{"prog", "1.125"}); + + CHECK(args.get_value("num") == T(1.125)); + } } TEST_CASE("Parsing a positional argument yields correct value for positive number") From 1e5782d5b702766e17f91afa6b8b78a18190e182 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 2 Oct 2025 18:53:17 +0200 Subject: [PATCH 4/7] Extend unit test --- test/unittest/test_parsing_positional.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/unittest/test_parsing_positional.cpp b/test/unittest/test_parsing_positional.cpp index eeadfdb..19bc9db 100644 --- a/test/unittest/test_parsing_positional.cpp +++ b/test/unittest/test_parsing_positional.cpp @@ -1934,14 +1934,23 @@ TEST_CASE_TEMPLATE("Parsing a positional argument yields correct value for posit } } -TEST_CASE("Parsing a positional argument yields correct value for positive number") +TEST_CASE_TEMPLATE("Parsing a positional argument yields correct value for positive number", T, short int, unsigned short int, int, unsigned int, long int, unsigned long int, long long int, unsigned long long int, float, double, long double) { auto parser = argparse::ArgumentParser(); - parser.add_argument("num").type(); + parser.add_argument("num").type(); + + if constexpr (std::is_integral_v) + { + auto const args = parser.parse_args(3, cstr_arr{"prog", "--", "65"}); - auto const args = parser.parse_args(3, cstr_arr{"prog", "--", "65"}); + CHECK(args.get_value("num") == T(65)); + } + else + { + auto const args = parser.parse_args(3, cstr_arr{"prog", "--", "1.125"}); - CHECK(args.get_value("num") == 65); + CHECK(args.get_value("num") == T(1.125)); + } } TEST_CASE("Parsing a positional argument yields correct value for negative number") From 6d25f622bf97f0d1d099f6f15e076a723f085e07 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 2 Oct 2025 19:10:42 +0200 Subject: [PATCH 5/7] Fix negative floating point numbers --- include/argparse.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index e0dd0c3..2c2124d 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -1035,7 +1035,7 @@ namespace argparse return true; } auto iss = std::istringstream(token.m_token); - auto num = int(); + auto num = double(); iss >> num; if (!iss.fail() && (iss.eof() || iss.peek() == std::istringstream::traits_type::eof())) { From 1038df499d9fd87b9a6228ad7512591c89ce63e4 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 2 Oct 2025 19:10:58 +0200 Subject: [PATCH 6/7] Extend unit test --- test/unittest/test_parsing_positional.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/unittest/test_parsing_positional.cpp b/test/unittest/test_parsing_positional.cpp index 19bc9db..0b16791 100644 --- a/test/unittest/test_parsing_positional.cpp +++ b/test/unittest/test_parsing_positional.cpp @@ -1953,14 +1953,23 @@ TEST_CASE_TEMPLATE("Parsing a positional argument yields correct value for posit } } -TEST_CASE("Parsing a positional argument yields correct value for negative number") +TEST_CASE_TEMPLATE("Parsing a positional argument yields correct value for negative number", T, short int, int, long int, long long int, float, double, long double) { auto parser = argparse::ArgumentParser(); - parser.add_argument("num").type(); + parser.add_argument("num").type(); - auto const args = parser.parse_args(2, cstr_arr{"prog", "-65"}); + if constexpr (std::is_integral_v) + { + auto const args = parser.parse_args(2, cstr_arr{"prog", "-65"}); - CHECK(args.get_value("num") == -65); + CHECK(args.get_value("num") == T(-65)); + } + else + { + auto const args = parser.parse_args(2, cstr_arr{"prog", "-1.125"}); + + CHECK(args.get_value("num") == T(-1.125)); + } } TEST_CASE("Parsing a positional argument yields correct value for negative number") From d70ff605ba04523d8ce1b3f85b188088a521186c Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 2 Oct 2025 19:12:36 +0200 Subject: [PATCH 7/7] Extend unit test --- test/unittest/test_parsing_positional.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/unittest/test_parsing_positional.cpp b/test/unittest/test_parsing_positional.cpp index 0b16791..891aed5 100644 --- a/test/unittest/test_parsing_positional.cpp +++ b/test/unittest/test_parsing_positional.cpp @@ -1972,12 +1972,21 @@ TEST_CASE_TEMPLATE("Parsing a positional argument yields correct value for negat } } -TEST_CASE("Parsing a positional argument yields correct value for negative number") +TEST_CASE_TEMPLATE("Parsing a positional argument yields correct value for negative number", T, short int, int, long int, long long int, float, double, long double) { auto parser = argparse::ArgumentParser(); - parser.add_argument("num").type(); + parser.add_argument("num").type(); - auto const args = parser.parse_args(3, cstr_arr{"prog", "--", "-65"}); + if constexpr (std::is_integral_v) + { + auto const args = parser.parse_args(3, cstr_arr{"prog", "--", "-65"}); - CHECK(args.get_value("num") == -65); + CHECK(args.get_value("num") == T(-65)); + } + else + { + auto const args = parser.parse_args(3, cstr_arr{"prog", "--", "-1.125"}); + + CHECK(args.get_value("num") == T(-1.125)); + } }