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/exit/CMakeLists.txt b/test/exit/CMakeLists.txt index ad3f2ef5..65882d80 100644 --- a/test/exit/CMakeLists.txt +++ b/test/exit/CMakeLists.txt @@ -13,6 +13,9 @@ 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) +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") diff --git a/test/unittest/test_argument_parser.cpp b/test/unittest/test_argument_parser.cpp index 5a8c328b..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...") @@ -104,6 +113,44 @@ 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") +{ + 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 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 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...")