2023-12-18 02:29:05 +00:00
|
|
|
#ifdef WITH_MODULE
|
|
|
|
import argparse;
|
|
|
|
#else
|
2022-10-04 01:53:37 +00:00
|
|
|
#include <argparse/argparse.hpp>
|
2023-12-18 02:29:05 +00:00
|
|
|
#endif
|
2022-10-04 01:53:37 +00:00
|
|
|
#include <doctest.hpp>
|
2023-12-18 02:29:05 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
2023-09-01 02:09:34 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <streambuf>
|
2022-10-04 01:53:37 +00:00
|
|
|
|
|
|
|
using doctest::test_suite;
|
|
|
|
|
|
|
|
TEST_CASE("Include all default arguments" * test_suite("default_args")) {
|
|
|
|
argparse::ArgumentParser parser("test");
|
|
|
|
auto help_msg{parser.help().str()};
|
|
|
|
REQUIRE(help_msg.find("shows help message") != std::string::npos);
|
|
|
|
REQUIRE(help_msg.find("prints version information") != std::string::npos);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Do not include default arguments" * test_suite("default_args")) {
|
|
|
|
argparse::ArgumentParser parser("test", "1.0",
|
|
|
|
argparse::default_arguments::none);
|
|
|
|
parser.parse_args({"test"});
|
|
|
|
REQUIRE_THROWS_AS(parser.get("--help"), std::logic_error);
|
|
|
|
REQUIRE_THROWS_AS(parser.get("--version"), std::logic_error);
|
|
|
|
}
|
2023-09-01 02:09:34 +00:00
|
|
|
|
|
|
|
TEST_CASE("Do not exit on default arguments" * test_suite("default_args")) {
|
|
|
|
argparse::ArgumentParser parser("test", "1.0",
|
|
|
|
argparse::default_arguments::all, false);
|
|
|
|
std::stringstream buf;
|
2023-12-18 02:29:05 +00:00
|
|
|
std::streambuf *saved_cout_buf = std::cout.rdbuf(buf.rdbuf());
|
2023-09-01 02:09:34 +00:00
|
|
|
parser.parse_args({"test", "--help"});
|
|
|
|
std::cout.rdbuf(saved_cout_buf);
|
|
|
|
REQUIRE(parser.is_used("--help"));
|
|
|
|
}
|