Skip to content

Commit 8ef633f

Browse files
committed
Add function help_entry() to format command line help entries
1 parent e7aacf2 commit 8ef633f

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/util/parse_options.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Author: Daniel Kroening, [email protected]
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, [email protected]
2324
#include "exception_utils.h"
2425
#include "exit_codes.h"
2526
#include "signal_catcher.h"
27+
#include "string_utils.h"
2628

2729
parse_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+
}

src/util/parse_options.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@ banner_string(const std::string &front_end, const std::string &version);
5858
/// ```
5959
std::string align_center_with_border(const std::string &text);
6060

61+
std::string help_entry(
62+
const std::string &option,
63+
const std::string &description,
64+
const std::size_t left_margin = 30,
65+
const std::size_t width = 80);
66+
6167
#endif // CPROVER_UTIL_PARSE_OPTIONS_H

unit/util/parse_options.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ TEST_CASE("align_center_with_border", "[core][util]")
1515
align_center_with_border("test-text") ==
1616
"* * test-text * *");
1717
}
18+
19+
TEST_CASE("help_entry", "[core][util]")
20+
{
21+
REQUIRE(help_entry("--abc", "xyz", 8, 12) == " --abc xyz\n");
22+
REQUIRE(help_entry("--abc", "xxxx x", 8, 12) == " --abc xxxx\n x\n");
23+
REQUIRE(help_entry("--abc", "xx xx", 8, 12) == " --abc xx\n xx\n");
24+
REQUIRE(help_entry("--abcdef", "xyz", 8, 12) == " --abcdef\n xyz\n");
25+
REQUIRE(help_entry("--abcdefg", "xyz", 8, 12) == " --abcdefg\n xyz\n");
26+
}

0 commit comments

Comments
 (0)