Merge pull request #132 from skrobinson/feat-improve-errors

Improve error messages
This commit is contained in:
Pranav 2021-09-14 15:03:48 -05:00 committed by GitHub
commit ab0a28c3bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 2 deletions

View File

@ -472,7 +472,8 @@ public:
} else if (mDefaultValue.has_value()) {
return start;
} else {
throw std::runtime_error("Too few arguments");
throw std::runtime_error("Too few arguments for '" +
std::string(mUsedName) + "'.");
}
}
@ -759,7 +760,7 @@ private:
if (mDefaultValue.has_value()) {
return std::any_cast<T>(mDefaultValue);
}
throw std::logic_error("No value provided");
throw std::logic_error("No value provided for '" + mNames.back() + "'.");
}
/*
@ -910,12 +911,16 @@ public:
}
/* Getter for options with default values.
* @throws std::logic_error if parse_args() has not been previously called
* @throws std::logic_error if there is no such option
* @throws std::logic_error if the option has no value
* @throws std::bad_any_cast if the option is not of type T
*/
template <typename T = std::string>
T get(std::string_view aArgumentName) const {
if (!mIsParsed) {
throw std::logic_error("Nothing parsed, no arguments are available.");
}
return (*this)[aArgumentName].get<T>();
}
@ -1075,6 +1080,7 @@ private:
throw std::runtime_error("Unknown argument");
}
}
mIsParsed = true;
}
/*
@ -1114,6 +1120,7 @@ private:
std::string mVersion;
std::string mDescription;
std::string mEpilog;
bool mIsParsed = false;
std::list<Argument> mPositionalArguments;
std::list<Argument> mOptionalArguments;
std::map<std::string_view, list_iterator, std::less<>> mArgumentMap;

View File

@ -30,6 +30,7 @@ file(GLOB ARGPARSE_TEST_SOURCES
test_compound_arguments.cpp
test_container_arguments.cpp
test_const_correct.cpp
test_get.cpp
test_help.cpp
test_invalid_arguments.cpp
test_is_used.cpp

37
test/test_get.cpp Normal file
View File

@ -0,0 +1,37 @@
#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("Skipped call to parse_args" * test_suite("ArgumentParser::get")) {
argparse::ArgumentParser program("test");
program.add_argument("stuff");
REQUIRE_THROWS_WITH_AS(program.get("stuff"),
"Nothing parsed, no arguments are available.",
std::logic_error);
}
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);
}

View File

@ -3,6 +3,14 @@
using doctest::test_suite;
TEST_CASE("Missing argument" * test_suite("parse_args")) {
argparse::ArgumentParser program("test");
program.add_argument("--config").nargs(1);
REQUIRE_THROWS_WITH_AS(program.parse_args({ "test", "--config" }),
"Too few arguments for '--config'.",
std::runtime_error);
}
TEST_CASE("Parse a string argument with value" * test_suite("parse_args")) {
argparse::ArgumentParser program("test");
program.add_argument("--config");

View File

@ -13,6 +13,15 @@ TEST_CASE("Parse positional arguments" * test_suite("positional_arguments")) {
REQUIRE(program.get("output") == "thrust_profile.csv");
}
TEST_CASE("Missing expected positional argument" *
test_suite("positional_arguments")) {
argparse::ArgumentParser program("test");
program.add_argument("input");
REQUIRE_THROWS_WITH_AS(program.parse_args({ "test" }),
"1 argument(s) expected. 0 provided.",
std::runtime_error);
}
TEST_CASE("Parse positional arguments with fixed nargs" *
test_suite("positional_arguments")) {
argparse::ArgumentParser program("test");