Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r58095 - in trunk: boost/program_options libs/program_options/src libs/program_options/test
From: s.ochsenknecht_at_[hidden]
Date: 2009-12-02 08:35:55


Author: s_ochsenknecht
Date: 2009-12-02 08:35:54 EST (Wed, 02 Dec 2009)
New Revision: 58095
URL: http://svn.boost.org/trac/boost/changeset/58095

Log:
Additional parameter to allow user to specify width of column for description text, patch from Chard, Fixes #3703
Text files modified:
   trunk/boost/program_options/options_description.hpp | 12 ++++++++-
   trunk/libs/program_options/src/options_description.cpp | 27 +++++++++++++++------
   trunk/libs/program_options/test/options_description_test.cpp | 49 ++++++++++++++++++++++++++++++++++++++++
   3 files changed, 78 insertions(+), 10 deletions(-)

Modified: trunk/boost/program_options/options_description.hpp
==============================================================================
--- trunk/boost/program_options/options_description.hpp (original)
+++ trunk/boost/program_options/options_description.hpp 2009-12-02 08:35:54 EST (Wed, 02 Dec 2009)
@@ -158,12 +158,18 @@
         static const unsigned m_default_line_length;
         
         /** Creates the instance. */
- options_description(unsigned line_length = m_default_line_length);
+ options_description(unsigned line_length = m_default_line_length,
+ unsigned description_length = m_default_line_length / 2);
         /** Creates the instance. The 'caption' parameter gives the name of
             this 'options_description' instance. Primarily useful for output.
+ The 'description_length' specifies the number of columns that
+ should be reserved for the description text; if the option text
+ encroaches into this, then the description will start on the next
+ line.
         */
         options_description(const std::string& caption,
- unsigned line_length = m_default_line_length);
+ unsigned line_length = m_default_line_length,
+ unsigned description_length = m_default_line_length / 2);
         /** Adds new variable description. Throws duplicate_variable_error if
             either short or long name matches that of already present one.
         */
@@ -213,6 +219,8 @@
 
         std::string m_caption;
         const unsigned m_line_length;
+ const unsigned m_description_length;
+
         // Data organization is chosen because:
         // - there could be two names for one option
         // - option_add_proxy needs to know the last added option

Modified: trunk/libs/program_options/src/options_description.cpp
==============================================================================
--- trunk/libs/program_options/src/options_description.cpp (original)
+++ trunk/libs/program_options/src/options_description.cpp 2009-12-02 08:35:54 EST (Wed, 02 Dec 2009)
@@ -205,15 +205,26 @@
 
     const unsigned options_description::m_default_line_length = 80;
 
- options_description::options_description(unsigned line_length)
+ options_description::options_description(unsigned line_length,
+ unsigned description_length)
     : m_line_length(line_length)
- {}
-
- options_description::options_description(const string& caption,
- unsigned line_length)
- : m_caption(caption), m_line_length(line_length)
- {}
+ , m_description_length(description_length)
+ {
+ // we require a space between the option and description parts, so add 1.
+ assert(m_description_length < m_line_length - 1);
+ }
 
+ options_description::options_description(const std::string& caption,
+ unsigned line_length,
+ unsigned description_length)
+ : m_caption(caption)
+ , m_line_length(line_length)
+ , m_description_length(description_length)
+ {
+ // we require a space between the option and description parts, so add 1.
+ assert(m_description_length < m_line_length - 1);
+ }
+
     void
     options_description::add(shared_ptr<option_description> desc)
     {
@@ -543,7 +554,7 @@
         }
         /* 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;
+ const unsigned start_of_description_column = m_line_length - m_description_length;
 
         width = (min)(width, start_of_description_column-1);
         

Modified: trunk/libs/program_options/test/options_description_test.cpp
==============================================================================
--- trunk/libs/program_options/test/options_description_test.cpp (original)
+++ trunk/libs/program_options/test/options_description_test.cpp 2009-12-02 08:35:54 EST (Wed, 02 Dec 2009)
@@ -111,6 +111,54 @@
    );
 }
 
+void test_formatting_description_length()
+{
+ {
+ options_description desc("",
+ options_description::m_default_line_length,
+ options_description::m_default_line_length / 2U);
+ desc.add_options()
+ ("an-option-that-sets-the-max", new untyped_value(), // > 40 available for desc
+ "this description sits on the same line, but wrapping should still work correctly")
+ ("a-long-option-that-would-leave-very-little-space-for-description", new untyped_value(),
+ "the description of the long opt, but placed on the next line\n"
+ " \talso ensure that the tabulation works correctly when a"
+ " description size has been set");
+
+ stringstream ss;
+ ss << desc;
+ BOOST_CHECK_EQUAL(ss.str(),
+ " --an-option-that-sets-the-max arg this description sits on the same line,\n"
+ " but wrapping should still work \n"
+ " correctly\n"
+ " --a-long-option-that-would-leave-very-little-space-for-description arg\n"
+ " the description of the long opt, but \n"
+ " placed on the next line\n"
+ " also ensure that the tabulation \n"
+ " works correctly when a description \n"
+ " size has been set\n");
+ }
+ {
+ // the default behaviour reserves 23 (+1 space) characters for the
+ // option column; this shows that the min_description_length does not
+ // breach that.
+ options_description desc("",
+ options_description::m_default_line_length,
+ options_description::m_default_line_length - 10U); // leaves < 23 (default option space)
+ desc.add_options()
+ ("an-option-that-encroaches-description", new untyped_value(),
+ "this description should always be placed on the next line, and wrapping should continue as normal");
+
+ stringstream ss;
+ ss << desc;
+ BOOST_CHECK_EQUAL(ss.str(),
+ " --an-option-that-encroaches-description arg\n"
+ //123456789_123456789_
+ " this description should always be placed on the next line, and \n"
+ " wrapping should continue as normal\n");
+ }
+}
+
 void test_long_default_value()
 {
     options_description desc;
@@ -175,6 +223,7 @@
     test_type();
     test_approximation();
     test_formatting();
+ test_formatting_description_length();
     test_long_default_value();
     test_word_wrapping();
     test_default_values();


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk