Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions include/argparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
Expand Down
3 changes: 3 additions & 0 deletions test/exit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
47 changes: 47 additions & 0 deletions test/unittest/test_argument_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ TEST_CASE("ArgumentParser does not handle help when requested to...")

CHECK(parsed.get_value<bool>("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<bool>("help") == true);
}
}

TEST_CASE("ArgumentParser does not handle parsing errors when requested to...")
Expand All @@ -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<bool>("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<bool>("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<bool>("v") == true);
}

TEST_CASE("ArgumentParser uses first command-line parameter as its name...")
Expand Down