mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
implements column-aligned multi-line help message for arguments (issue #248)
This commit is contained in:
parent
15d745f584
commit
3d7596765b
@ -649,6 +649,7 @@ public:
|
|||||||
|
|
||||||
friend std::ostream &operator<<(std::ostream &stream,
|
friend std::ostream &operator<<(std::ostream &stream,
|
||||||
const Argument &argument) {
|
const Argument &argument) {
|
||||||
|
auto stream_width = stream.width();
|
||||||
std::stringstream name_stream;
|
std::stringstream name_stream;
|
||||||
name_stream << " "; // indent
|
name_stream << " "; // indent
|
||||||
if (argument.is_positional(argument.m_names.front(),
|
if (argument.is_positional(argument.m_names.front(),
|
||||||
@ -668,7 +669,31 @@ public:
|
|||||||
name_stream << " " << argument.m_metavar;
|
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)
|
||||||
|
stream << "\t" << argument.m_help;
|
||||||
|
else {
|
||||||
|
auto leftover = argument.m_help.substr(prev, argument.m_help.size() - prev);
|
||||||
|
if (leftover.size() > 0) {
|
||||||
|
stream.width(stream_width);
|
||||||
|
stream << std::string(spacing, ' ') << "\t" << leftover;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// print nargs spec
|
// print nargs spec
|
||||||
if (!argument.m_help.empty()) {
|
if (!argument.m_help.empty()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user