Skip to content

Commit 6bf4607

Browse files
committed
Align columns across groups in --help output.
Patch from Leo Goodstadt. Fixes #6114. [SVN r86571]
1 parent 81844db commit 6bf4607

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

include/boost/program_options/options_description.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ namespace program_options {
199199
*/
200200
options_description& add(const options_description& desc);
201201

202+
/** Find the maximum width of the option column, including options
203+
in groups. */
204+
unsigned get_option_column_width() const;
205+
202206
public:
203207
/** Returns an object of implementation-defined type suitable for adding
204208
options to options_description. The returned object will
@@ -229,7 +233,7 @@ namespace program_options {
229233

230234
/** Outputs 'desc' to the specified stream, calling 'f' to output each
231235
option_description element. */
232-
void print(std::ostream& os) const;
236+
void print(std::ostream& os, unsigned width = 0) const;
233237

234238
private:
235239
typedef std::map<std::string, int>::const_iterator name2index_iterator;

src/options_description.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -604,12 +604,9 @@ namespace boost { namespace program_options {
604604
}
605605
}
606606

607-
void
608-
options_description::print(std::ostream& os) const
607+
unsigned
608+
options_description::get_option_column_width() const
609609
{
610-
if (!m_caption.empty())
611-
os << m_caption << ":\n";
612-
613610
/* Find the maximum width of the option column */
614611
unsigned width(23);
615612
unsigned i; // vc6 has broken for loop scoping
@@ -620,6 +617,11 @@ namespace boost { namespace program_options {
620617
ss << " " << opt.format_name() << ' ' << opt.format_parameter();
621618
width = (max)(width, static_cast<unsigned>(ss.str().size()));
622619
}
620+
621+
/* Get width of groups as well*/
622+
for (unsigned j = 0; j < groups.size(); ++j)
623+
width = max(width, groups[j]->get_option_column_width());
624+
623625
/* this is the column were description should start, if first
624626
column is longer, we go to a new line */
625627
const unsigned start_of_description_column = m_line_length - m_min_description_length;
@@ -628,9 +630,20 @@ namespace boost { namespace program_options {
628630

629631
/* add an additional space to improve readability */
630632
++width;
631-
633+
return width;
634+
}
635+
636+
void
637+
options_description::print(std::ostream& os, unsigned width) const
638+
{
639+
if (!m_caption.empty())
640+
os << m_caption << ":\n";
641+
642+
if (!width)
643+
width = get_option_column_width();
644+
632645
/* The options formatting style is stolen from Subversion. */
633-
for (i = 0; i < m_options.size(); ++i)
646+
for (unsigned i = 0; i < m_options.size(); ++i)
634647
{
635648
if (belong_to_group[i])
636649
continue;
@@ -643,7 +656,8 @@ namespace boost { namespace program_options {
643656
}
644657

645658
for (unsigned j = 0; j < groups.size(); ++j) {
646-
os << "\n" << *groups[j];
659+
os << "\n";
660+
groups[j]->print(os, width);
647661
}
648662
}
649663

0 commit comments

Comments
 (0)