mirror of https://github.com/jarro2783/cxxopts.git
Merge 2f7f449367 into 0f1c5a0a79
This commit is contained in:
commit
076d2cb41b
|
|
@ -2300,6 +2300,58 @@ format_description
|
|||
return result;
|
||||
}
|
||||
|
||||
String
|
||||
format_custom_help
|
||||
(
|
||||
const std::string& custom_help,
|
||||
std::size_t start,
|
||||
std::size_t allowed
|
||||
)
|
||||
{
|
||||
String result;
|
||||
std::size_t count = 0;
|
||||
std::size_t word_size = 0;
|
||||
|
||||
if(allowed <= start)
|
||||
{
|
||||
throw_or_mimic<exceptions::invalid_option_format>("Allowed column"
|
||||
"width must be greater than start column width!");
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < custom_help.length(); i++)
|
||||
{
|
||||
char c = custom_help[i];
|
||||
|
||||
while (count < start) {
|
||||
result.push_back(' ');
|
||||
count++;
|
||||
}
|
||||
|
||||
// record the start of a word
|
||||
word_size = (std::isspace(c)) ? 0 : word_size + 1;
|
||||
|
||||
result.push_back(c);
|
||||
|
||||
count = (c == '\n') ? 0 : count + 1;
|
||||
|
||||
if (count >= allowed)
|
||||
{
|
||||
// if we are in the middle of a word, backtrack until word_size is 0
|
||||
|
||||
for (std::size_t c1 = 0; c1 < word_size; c1++)
|
||||
{
|
||||
result.pop_back();
|
||||
i--;
|
||||
}
|
||||
|
||||
result.push_back('\n');
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
inline
|
||||
|
|
@ -2886,6 +2938,7 @@ Options::help(const std::vector<std::string>& help_groups, bool print_usage) con
|
|||
if (!m_custom_help.empty())
|
||||
{
|
||||
result += " " + toLocalString(m_custom_help);
|
||||
result = format_custom_help(result, OPTION_DESC_GAP, m_width);
|
||||
}
|
||||
|
||||
if (!m_positional.empty() && !m_positional_help.empty()) {
|
||||
|
|
|
|||
|
|
@ -1191,3 +1191,39 @@ TEST_CASE("No Options help", "[options]")
|
|||
CHECK_NOTHROW(options.parse(argc, argv));
|
||||
CHECK(options.help().find("test <posArg1>...<posArgN>") != std::string::npos);
|
||||
}
|
||||
|
||||
TEST_CASE("Format custom message with selected width", "[help]")
|
||||
{
|
||||
cxxopts::Options options("Custom message width",
|
||||
" - test custom message formatting width");
|
||||
|
||||
options.custom_help(
|
||||
"A very very long description that should be wrapped according to the "
|
||||
"specified width for help messages. This is to ensure that lines are not "
|
||||
"very long and remain readable. Just to make sure we have enough text here "
|
||||
"to trigger the wrapping functionality properly. Let's add a bit more text "
|
||||
"to be certain!");
|
||||
|
||||
options.set_width(60);
|
||||
const auto help = options.help();
|
||||
CHECK(
|
||||
help.find(
|
||||
"Custom message width A very very long description that \n should "
|
||||
"be wrapped according to the specified width for \n help messages. "
|
||||
"This is to ensure that lines are not very \n long and remain "
|
||||
"readable. Just to make sure we have \n enough text here to trigger "
|
||||
"the wrapping functionality \n properly. Let's add a bit more text "
|
||||
"to be certain!") != std::string::npos);
|
||||
|
||||
options.set_width(90);
|
||||
const auto help90 = options.help();
|
||||
CHECK(
|
||||
help90.find(
|
||||
"Custom message width A very very long description that should be "
|
||||
"wrapped according to \n the specified width for help messages. "
|
||||
"This is to ensure that lines are not very long \n and remain "
|
||||
"readable. Just to make sure we have enough text here to trigger the "
|
||||
"wrapping \n functionality properly. Let's add a bit more text to "
|
||||
"be certain!") !=
|
||||
std::string::npos);
|
||||
}
|
||||
Loading…
Reference in New Issue