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

implements column-aligned multi-line help message for arguments #259

Merged
merged 1 commit into from
Jul 11, 2023
Merged
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
27 changes: 26 additions & 1 deletion include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ class Argument {

friend std::ostream &operator<<(std::ostream &stream,
const Argument &argument) {
auto stream_width = stream.width();
std::stringstream name_stream;
name_stream << " "; // indent
if (argument.is_positional(argument.m_names.front(),
Expand All @@ -668,7 +669,31 @@ class Argument {
name_stream << " " << argument.m_metavar;
}
}
stream << name_stream.str() << "\t" << argument.m_help;
auto spacing = name_stream.str().size();
auto pos = 0;
auto prev = 0;
auto first_line = true;
stream << name_stream.str();
while ((pos = argument.m_help.find('\n', prev)) != std::string::npos) {
auto line = argument.m_help.substr(prev, pos - prev + 1);
if (first_line) {
stream << "\t" << line;
first_line = false;
} else {
stream.width(stream_width);
stream << std::string(spacing, ' ') << "\t" << line;
}
prev += pos - prev + 1;
}
if (first_line)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-braces-around-statements ⚠️
statement should be inside braces

Suggested change
if (first_line)
if (first_line) {

stream << "\t" << argument.m_help;
else {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-braces-around-statements ⚠️
statement should be inside braces

Suggested change
else {
} else {

auto leftover = argument.m_help.substr(prev, argument.m_help.size() - prev);
if (leftover.size() > 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-container-size-empty ⚠️
the empty method should be used to check for emptiness instead of size

Suggested change
if (leftover.size() > 0) {
if (!leftover.empty()) {

stream.width(stream_width);
stream << std::string(spacing, ' ') << "\t" << leftover;
}
}

// print nargs spec
if (!argument.m_help.empty()) {
Expand Down