argparse/test/test_get.cpp
Sean Robinson 1c2fd8726d Add argument name in exception thrown by Argument::get
As the user did not include the argument, the longest name for the unused
argument is in the last position of mNames.

This is an API change that may affect programs trying to match the
specific "No value provided" message.  The new error message appends the
argument that caused the error.

A solution which works with both versions is to look for "No value
provided" at the beginning of the error message.

- if (err.what() == "No value provided")
+ if (std:string(err.what()).rfind("No value provided", 0) == 0)

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-09-14 09:51:10 -07:00

30 lines
1.1 KiB
C++

#include <doctest.hpp>
#include <argparse/argparse.hpp>
using doctest::test_suite;
TEST_CASE("Getting a simple argument" * test_suite("ArgumentParser::get")) {
argparse::ArgumentParser program("test");
program.add_argument("-s", "--stuff");
REQUIRE_NOTHROW(program.parse_args({ "test", "-s", "./src" }));
REQUIRE(program.get("--stuff") == "./src");
}
TEST_CASE("Missing argument" * test_suite("ArgumentParser::get")) {
argparse::ArgumentParser program("test");
program.add_argument("-s", "--stuff");
REQUIRE_NOTHROW(program.parse_args({ "test" }));
REQUIRE_THROWS_WITH_AS(program.get("--stuff"),
"No value provided for '--stuff'.",
std::logic_error);
}
TEST_CASE("Implicit argument" * test_suite("ArgumentParser::get")) {
argparse::ArgumentParser program("test");
program.add_argument("-s", "--stuff").nargs(1);
REQUIRE_NOTHROW(program.parse_args({ "test" }));
REQUIRE_THROWS_WITH_AS(program.get("--stuff"),
"No value provided for '--stuff'.",
std::logic_error);
}