Put description to next line if we'd overflow otherwise.

Fixes #689.

Patch from Sascha Ochsenknecht.


[SVN r57271]
This commit is contained in:
Vladimir Prus
2009-11-01 10:44:49 +00:00
parent 8f40132e63
commit 122fe22e07
2 changed files with 34 additions and 4 deletions

View File

@@ -504,11 +504,18 @@ namespace boost { namespace program_options {
if (!opt.description().empty())
{
for(unsigned pad = first_column_width - ss.str().size();
pad > 0;
--pad)
if (ss.str().size() >= first_column_width)
{
os.put(' ');
os.put('\n'); // first column is too long, lets put description in new line
for (unsigned pad = first_column_width; pad > 0; --pad)
{
os.put(' ');
}
} else {
for(unsigned pad = first_column_width - ss.str().size(); pad > 0; --pad)
{
os.put(' ');
}
}
format_description(os, opt.description(),
@@ -533,6 +540,11 @@ namespace boost { namespace program_options {
ss << " " << opt.format_name() << ' ' << opt.format_parameter();
width = (max)(width, static_cast<unsigned>(ss.str().size()));
}
/* this is the column were description should start, if first
column is longer, we go to a new line */
unsigned start_of_description_column = m_line_length / 2;
width = (min)(width, start_of_description_column-1);
/* add an additional space to improve readability */
++width;

View File

@@ -76,10 +76,28 @@ void test_formatting()
ss << desc;
}
void test_long_default_value()
{
options_description desc;
desc.add_options()
("cfgfile,c",
value<std::string>()->default_value("/usr/local/etc/myprogramXXXXXXXXX/configuration.conf"),
"the configfile");
stringstream ss;
ss << desc;
BOOST_CHECK_EQUAL(ss.str(),
" -c [ --cfgfile ] arg (=/usr/local/etc/myprogramXXXXXXXXX/configuration.conf)\n"
" the configfile\n"
);
}
int main(int, char* [])
{
test_type();
test_approximation();
test_formatting();
test_long_default_value();
return 0;
}