Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format custom help message with configured column width #413

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions include/cxxopts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,59 @@ 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 c = 0; c < word_size; c++)
{
char last_char = result.back();
result.pop_back();
i--;
}

result.push_back('\n');
count = 0;
}
}

return result;
}

} // namespace

inline
Expand Down Expand Up @@ -2830,6 +2883,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()) {
Expand Down
Loading