|
9 | 9 | #include "parse_options.h"
|
10 | 10 |
|
11 | 11 | #include <algorithm>
|
| 12 | +#include <cctype> |
12 | 13 | #include <climits>
|
13 | 14 | #include <iostream>
|
14 | 15 |
|
|
23 | 24 | #include "exception_utils.h"
|
24 | 25 | #include "exit_codes.h"
|
25 | 26 | #include "signal_catcher.h"
|
| 27 | +#include "string_utils.h" |
26 | 28 |
|
27 | 29 | parse_options_baset::parse_options_baset(
|
28 | 30 | const std::string &_optstring,
|
@@ -151,3 +153,54 @@ banner_string(const std::string &front_end, const std::string &version)
|
151 | 153 |
|
152 | 154 | return align_center_with_border(version_str);
|
153 | 155 | }
|
| 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