From 6b0a4ac2053c0c55157e872eb1eef292ccdbfd6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Weing=C3=A4rtner?= Date: Wed, 20 Apr 2016 17:07:22 +0200 Subject: [PATCH] improve option_column_width calculation * also have two spaces if option takes an argument * don't widen the column if the option would need to have it's description on a separate line anyway Fixes #12091. --- src/options_description.cpp | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/options_description.cpp b/src/options_description.cpp index 6592a5d..b145222 100644 --- a/src/options_description.cpp +++ b/src/options_description.cpp @@ -577,7 +577,12 @@ namespace boost { namespace program_options { unsigned first_column_width, unsigned line_length) { stringstream ss; - ss << " " << opt.format_name() << ' ' << opt.format_parameter(); + ss << " " << opt.format_name(); + string const param(opt.format_parameter()); + if (!param.empty()) + ss << ' ' << param; + /* two spaces to improve readability */ + ss << " "; // Don't use ss.rdbuf() since g++ 2.96 is buggy on it. os << ss.str(); @@ -608,28 +613,35 @@ namespace boost { namespace program_options { options_description::get_option_column_width() const { /* Find the maximum width of the option column */ - unsigned width(23); + unsigned width(24); + /* this is the column were description should start */ + unsigned const max_width(m_line_length - m_min_description_length); + unsigned i; // vc6 has broken for loop scoping for (i = 0; i < m_options.size(); ++i) { const option_description& opt = *m_options[i]; stringstream ss; - ss << " " << opt.format_name() << ' ' << opt.format_parameter(); - width = (max)(width, static_cast(ss.str().size())); + ss << " " << opt.format_name(); + + string const param(opt.format_parameter()); + if (!param.empty()) + ss << ' ' << param; + + /* two spaces to improve readability */ + ss << " "; + + unsigned const new_width(static_cast(ss.str().size())); + + /* only widen if this option doesn't need a separate desciption line anyway */ + if (new_width <= max_width) + width = (max)(width, new_width); } /* Get width of groups as well*/ for (unsigned j = 0; j < groups.size(); ++j) width = max(width, groups[j]->get_option_column_width()); - /* this is the column were description should start, if first - column is longer, we go to a new line */ - const unsigned start_of_description_column = m_line_length - m_min_description_length; - - width = (min)(width, start_of_description_column-1); - - /* add an additional space to improve readability */ - ++width; return width; } -- 2.8.0.rc3