diff --git a/include/argparse.hpp b/include/argparse.hpp index 6319b91..60665ae 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -1213,9 +1213,7 @@ namespace argparse auto get_dest_name() const -> std::string override { - return m_impl.get_dest().empty() - ? get_name() - : m_impl.get_dest(); + return get_name(); } auto get_metavar_name() const -> std::string override @@ -1960,6 +1958,10 @@ namespace argparse auto dest(std::string dest) -> ArgumentBuilder & { + if (is_positional()) + { + throw option_error("'dest' is an invalid argument for positionals"); + } m_options.dest = std::move(dest); return *this; } diff --git a/test/unittest/test_argument_parser.cpp b/test/unittest/test_argument_parser.cpp index bb08ab9..36fbd7f 100644 --- a/test/unittest/test_argument_parser.cpp +++ b/test/unittest/test_argument_parser.cpp @@ -313,6 +313,20 @@ TEST_CASE("ArgumentParser replaces '{prog}' with program name in usage text") CHECK(parser.format_help() == "usage: program [options]"s); } +TEST_CASE("Adding a positional argument with dest option set results in error") +{ + auto parser = argparse::ArgumentParser(); + + CHECK_THROWS_WITH_AS(parser.add_argument("pos").dest("foo"), "'dest' is an invalid argument for positionals", argparse::option_error); +} + +TEST_CASE("Adding an optional argument with dest option does not result in error") +{ + auto parser = argparse::ArgumentParser(); + + CHECK_NOTHROW(parser.add_argument("-o").dest("foo")); +} + TEST_CASE("Adding a positional argument with required option set results in error") { auto parser = argparse::ArgumentParser(); diff --git a/test/unittest/test_parsing_positional.cpp b/test/unittest/test_parsing_positional.cpp index a797ef3..b35ba9c 100644 --- a/test/unittest/test_parsing_positional.cpp +++ b/test/unittest/test_parsing_positional.cpp @@ -129,16 +129,6 @@ TEST_CASE("The resulting attribute name for positional argument is based on its CHECK(args.get("foo")); } -TEST_CASE("The resulting attribute name for positional argument is based on dest parameter") -{ - auto parser = argparse::ArgumentParser(); - parser.add_argument("foo").dest("bar"); - - auto const args = parser.parse_args(2, cstr_arr{"prog", "val"}); - - CHECK(args.get("bar")); -} - TEST_CASE_TEMPLATE("Parsing a positional argument with choices set accepts one of the values", T, char, signed char, unsigned char, short int, unsigned short int, int, unsigned int, long int, unsigned long int, long long int, unsigned long long int, float, double, long double, std::string, foo::Custom) { auto parser = argparse::ArgumentParser().handle(argparse::Handle::none);