@@ -9,6 +9,7 @@ Author: Daniel Kroening, kroening@kroening.com
99#include " parse_options.h"
1010
1111#include < algorithm>
12+ #include < cctype>
1213#include < climits>
1314#include < iostream>
1415
@@ -23,6 +24,7 @@ Author: Daniel Kroening, kroening@kroening.com
2324#include " exception_utils.h"
2425#include " exit_codes.h"
2526#include " signal_catcher.h"
27+ #include " string_utils.h"
2628
2729parse_options_baset::parse_options_baset (
2830 const std::string &_optstring,
@@ -151,3 +153,54 @@ banner_string(const std::string &front_end, const std::string &version)
151153
152154 return align_center_with_border (version_str);
153155}
156+
157+ std::string help_entry (
158+ const std::string &option,
159+ const std::string &description,
160+ const std::size_t left_margin,
161+ const std::size_t width)
162+ {
163+ PRECONDITION (!option.empty ());
164+ PRECONDITION (!std::isspace (option.front ()));
165+ PRECONDITION (!std::isspace (option.back ()));
166+ PRECONDITION (option.length () <= width);
167+
168+ PRECONDITION (!description.empty ());
169+ PRECONDITION (!std::isspace (description.front ()));
170+ PRECONDITION (!std::isspace (description.back ()));
171+
172+ PRECONDITION (left_margin < width);
173+
174+ std::string result;
175+
176+ if (option.length () >= left_margin - 1 )
177+ {
178+ result = " " + option + " \n " ;
179+ result += wrap_line (description, left_margin, width) + " \n " ;
180+
181+ return result;
182+ }
183+
184+ std::string padding (left_margin - option.length () - 1 , ' ' );
185+ result = " " + option + padding;
186+
187+ if (description.length () <= (width - left_margin))
188+ {
189+ return result + description + " \n " ;
190+ }
191+
192+ auto it = description.cbegin () + (width - left_margin);
193+ auto rit = std::reverse_iterator<decltype (it)>(it) - 1 ;
194+
195+ auto rit_space = std::find (rit, description.crend (), ' ' );
196+ auto it_space = rit_space.base () - 1 ;
197+ CHECK_RETURN (*it_space == ' ' );
198+
199+ result.append (description.cbegin (), it_space);
200+ result.append (" \n " );
201+
202+ result +=
203+ wrap_line (it_space + 1 , description.cend (), left_margin, width) + " \n " ;
204+
205+ return result;
206+ }
0 commit comments