Skip to content

Commit 3945f84

Browse files
committed
Format custom help message with configured column width
1 parent 929bf2d commit 3945f84

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

include/cxxopts.hpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,58 @@ format_description
23002300
return result;
23012301
}
23022302

2303+
String
2304+
format_custom_help
2305+
(
2306+
const std::string& custom_help,
2307+
std::size_t start,
2308+
std::size_t allowed
2309+
)
2310+
{
2311+
String result;
2312+
std::size_t count = 0;
2313+
std::size_t word_size = 0;
2314+
2315+
if(allowed <= start)
2316+
{
2317+
throw_or_mimic<exceptions::invalid_option_format>("Allowed column"
2318+
"width must be greater than start column width!");
2319+
}
2320+
2321+
for (std::size_t i = 0; i < custom_help.length(); i++)
2322+
{
2323+
char c = custom_help[i];
2324+
2325+
while (count < start) {
2326+
result.push_back(' ');
2327+
count++;
2328+
}
2329+
2330+
// record the start of a word
2331+
word_size = (std::isspace(c)) ? 0 : word_size + 1;
2332+
2333+
result.push_back(c);
2334+
2335+
count = (c == '\n') ? 0 : count + 1;
2336+
2337+
if (count >= allowed)
2338+
{
2339+
// if we are in the middle of a word, backtrack until word_size is 0
2340+
2341+
for (std::size_t c = 0; c < word_size; c++)
2342+
{
2343+
result.pop_back();
2344+
i--;
2345+
}
2346+
2347+
result.push_back('\n');
2348+
count = 0;
2349+
}
2350+
}
2351+
2352+
return result;
2353+
}
2354+
23032355
} // namespace
23042356

23052357
inline
@@ -2886,6 +2938,7 @@ Options::help(const std::vector<std::string>& help_groups, bool print_usage) con
28862938
if (!m_custom_help.empty())
28872939
{
28882940
result += " " + toLocalString(m_custom_help);
2941+
result = format_custom_help(result, OPTION_DESC_GAP, m_width);
28892942
}
28902943

28912944
if (!m_positional.empty() && !m_positional_help.empty()) {

0 commit comments

Comments
 (0)