From b86ffda4c66f8d5ceb63bdc82ab77a889d32d65a Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 6 Feb 2025 21:49:11 +0100 Subject: [PATCH 1/7] Add unit test --- test/unittest/test_argument_parser.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/unittest/test_argument_parser.cpp b/test/unittest/test_argument_parser.cpp index 5a8c328b..7dc88ec2 100644 --- a/test/unittest/test_argument_parser.cpp +++ b/test/unittest/test_argument_parser.cpp @@ -106,6 +106,16 @@ TEST_CASE("ArgumentParser does not handle parsing errors when requested to...") } } +TEST_CASE("ArgumentParser does not handle version when requested to handle nothing") +{ + auto parser = argparse::ArgumentParser().handle(argparse::Handle::none); + parser.add_argument("-v").action(argparse::version); + + auto const parsed = parser.parse_args(2, cstr_arr{"prog", "-v"}); + + CHECK(parsed.get_value("v") == true); +} + TEST_CASE("ArgumentParser uses first command-line parameter as its name...") { auto parser = argparse::ArgumentParser().add_help(false); From f0d2aa8737c8c36e8a8f27031501ba56bd366703 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 6 Feb 2025 21:50:43 +0100 Subject: [PATCH 2/7] Add unit test --- test/unittest/test_argument_parser.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/unittest/test_argument_parser.cpp b/test/unittest/test_argument_parser.cpp index 7dc88ec2..3ec037ba 100644 --- a/test/unittest/test_argument_parser.cpp +++ b/test/unittest/test_argument_parser.cpp @@ -116,6 +116,16 @@ TEST_CASE("ArgumentParser does not handle version when requested to handle nothi CHECK(parsed.get_value("v") == true); } +TEST_CASE("ArgumentParser does not handle version when requested to handle errors") +{ + auto parser = argparse::ArgumentParser().handle(argparse::Handle::errors); + parser.add_argument("-v").action(argparse::version); + + auto const parsed = parser.parse_args(2, cstr_arr{"prog", "-v"}); + + CHECK(parsed.get_value("v") == true); +} + TEST_CASE("ArgumentParser uses first command-line parameter as its name...") { auto parser = argparse::ArgumentParser().add_help(false); From d78e425b33de23e92e4fc210b47e9269bd0f9eac Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 6 Feb 2025 22:21:22 +0100 Subject: [PATCH 3/7] Separate handling version from handling help --- include/argparse.h | 8 +++++--- test/unittest/test_argument_parser.cpp | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/argparse.h b/include/argparse.h index d4076e60..1fff0b62 100644 --- a/include/argparse.h +++ b/include/argparse.h @@ -53,7 +53,9 @@ namespace argparse none = 0, errors = 1, help = 2, - errors_and_help = errors | help + version = 4, + errors_and_help = errors | help, + errors_help_version = errors | help | version }; class parsing_error @@ -185,7 +187,7 @@ namespace argparse } catch (VersionRequested const &) { - if (m_handle & Handle::help) + if (m_handle & Handle::version) { std::cout << format_version() << std::endl; std::exit(EXIT_SUCCESS); @@ -283,7 +285,7 @@ namespace argparse , m_prog() , m_description() , m_epilog() - , m_handle(Handle::errors_and_help) + , m_handle(Handle::errors_help_version) { add_argument("-h", "--help").action(help).help("show this help message and exit"); } diff --git a/test/unittest/test_argument_parser.cpp b/test/unittest/test_argument_parser.cpp index 3ec037ba..2689f909 100644 --- a/test/unittest/test_argument_parser.cpp +++ b/test/unittest/test_argument_parser.cpp @@ -126,6 +126,16 @@ TEST_CASE("ArgumentParser does not handle version when requested to handle error CHECK(parsed.get_value("v") == true); } +TEST_CASE("ArgumentParser does not handle version when requested to handle help") +{ + auto parser = argparse::ArgumentParser().handle(argparse::Handle::help); + parser.add_argument("-v").action(argparse::version); + + auto const parsed = parser.parse_args(2, cstr_arr{"prog", "-v"}); + + CHECK(parsed.get_value("v") == true); +} + TEST_CASE("ArgumentParser uses first command-line parameter as its name...") { auto parser = argparse::ArgumentParser().add_help(false); From 486bffba6676ef19ecbbf0727c1d7728a1447869 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 6 Feb 2025 22:23:11 +0100 Subject: [PATCH 4/7] Extend unit test --- test/unittest/test_argument_parser.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/unittest/test_argument_parser.cpp b/test/unittest/test_argument_parser.cpp index 2689f909..951dced7 100644 --- a/test/unittest/test_argument_parser.cpp +++ b/test/unittest/test_argument_parser.cpp @@ -104,6 +104,14 @@ TEST_CASE("ArgumentParser does not handle parsing errors when requested to...") CHECK_THROWS_AS(parser.parse_args(1, cstr_arr{"prog"}), argparse::parsing_error); } + + SUBCASE("...handle version") + { + auto parser = argparse::ArgumentParser().handle(argparse::Handle::version); + parser.add_argument("pos"); + + CHECK_THROWS_AS(parser.parse_args(1, cstr_arr{"prog"}), argparse::parsing_error); + } } TEST_CASE("ArgumentParser does not handle version when requested to handle nothing") From bdc325297f8b3443f6ed038ee010a054d27affe4 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 6 Feb 2025 22:23:50 +0100 Subject: [PATCH 5/7] Extend unit test --- test/unittest/test_argument_parser.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/unittest/test_argument_parser.cpp b/test/unittest/test_argument_parser.cpp index 951dced7..ad61668c 100644 --- a/test/unittest/test_argument_parser.cpp +++ b/test/unittest/test_argument_parser.cpp @@ -85,6 +85,15 @@ TEST_CASE("ArgumentParser does not handle help when requested to...") CHECK(parsed.get_value("help") == true); } + + SUBCASE("...handle version") + { + auto parser = argparse::ArgumentParser().handle(argparse::Handle::version); + + auto const parsed = parser.parse_args(2, cstr_arr{"prog", "-h"}); + + CHECK(parsed.get_value("help") == true); + } } TEST_CASE("ArgumentParser does not handle parsing errors when requested to...") From 28878728bdfd8e1dc2ae29ec8856ec7f4254222d Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 6 Feb 2025 22:25:54 +0100 Subject: [PATCH 6/7] Add normal-test --- test/exit/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/exit/CMakeLists.txt b/test/exit/CMakeLists.txt index ad3f2ef5..561be8fc 100644 --- a/test/exit/CMakeLists.txt +++ b/test/exit/CMakeLists.txt @@ -13,6 +13,8 @@ target_compile_options(app PRIVATE /W4 /WX /permissive->) target_link_libraries(app PRIVATE cpp-argparse) +add_test(NAME normal-test COMMAND app arg --optional opt) + add_test(NAME help-test COMMAND app --help) set_property(TEST help-test PROPERTY PASS_REGULAR_EXPRESSION "usage: app \\[-h\\] \\[--optional OPTIONAL\\] \\[-v\\] positional\n\npositional arguments:\n positional\n\noptional arguments:\n -h, --help show this help message and exit\n --optional OPTIONAL\n -v, --version show program's version number and exit\n") From 813614d74e14833f3a5f460fe0b2d2d55dbd3feb Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Thu, 6 Feb 2025 22:28:50 +0100 Subject: [PATCH 7/7] Extend normal-test --- test/exit/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/exit/CMakeLists.txt b/test/exit/CMakeLists.txt index 561be8fc..65882d80 100644 --- a/test/exit/CMakeLists.txt +++ b/test/exit/CMakeLists.txt @@ -14,6 +14,7 @@ target_compile_options(app PRIVATE target_link_libraries(app PRIVATE cpp-argparse) add_test(NAME normal-test COMMAND app arg --optional opt) +set_property(TEST normal-test PROPERTY PASS_REGULAR_EXPRESSION "positional: arg\noptional: opt") add_test(NAME help-test COMMAND app --help) set_property(TEST help-test PROPERTY PASS_REGULAR_EXPRESSION "usage: app \\[-h\\] \\[--optional OPTIONAL\\] \\[-v\\] positional\n\npositional arguments:\n positional\n\noptional arguments:\n -h, --help show this help message and exit\n --optional OPTIONAL\n -v, --version show program's version number and exit\n")