mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 14:44:40 +00:00
Passed test for multiline help message alignment
This commit is contained in:
parent
5595375786
commit
19d85eadb0
@ -1,6 +1,8 @@
|
||||
#include <argparse/argparse.hpp>
|
||||
#include <doctest.hpp>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
|
||||
using doctest::test_suite;
|
||||
|
||||
@ -73,3 +75,57 @@ TEST_CASE("Users can replace default -h/--help" * test_suite("help")) {
|
||||
program.parse_args({"test", "--help"});
|
||||
REQUIRE_FALSE(buffer.str().empty());
|
||||
}
|
||||
|
||||
TEST_CASE("Multiline help message alignment") {
|
||||
argparse::ArgumentParser program("program");
|
||||
program.add_argument("input1")
|
||||
.help(
|
||||
"This is the first line of help message.\n"
|
||||
"And this is the second line of help message."
|
||||
);
|
||||
program.add_argument("program_input2")
|
||||
.help("There is only one line.");
|
||||
program.add_argument("-p", "--prog_input3")
|
||||
.help(
|
||||
R"(Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
Sed ut perspiciatis unde omnis iste natus error sit voluptatem
|
||||
accusantium doloremque laudantium, totam rem aperiam...)"
|
||||
);
|
||||
|
||||
std::ostringstream stream;
|
||||
stream << program;
|
||||
std::string help_text = stream.str();
|
||||
|
||||
// Split the help text into lines
|
||||
std::istringstream iss(help_text);
|
||||
std::vector<std::string> help_lines;
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
help_lines.push_back(line);
|
||||
}
|
||||
|
||||
// A map to store the starting position of the help text for each argument
|
||||
std::unordered_map<std::string, int> help_start_positions;
|
||||
|
||||
for (const auto& line : help_lines) {
|
||||
// Find the argument names in the line
|
||||
std::smatch match;
|
||||
if (std::regex_search(line, match, std::regex(R"((input1|program_input2|--prog_input3))"))) {
|
||||
std::string arg_name = match.str();
|
||||
|
||||
// Find the position of the first non-space character after the argument name
|
||||
int position = line.find_first_not_of(' ', match.position() + arg_name.size());
|
||||
if (position == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
help_start_positions[arg_name] = position;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the positions are the same for all arguments
|
||||
REQUIRE(!help_start_positions.empty());
|
||||
int first_position = help_start_positions.begin()->second;
|
||||
for (const auto& entry : help_start_positions) {
|
||||
REQUIRE(entry.second == first_position);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user