Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
cli11_formatter.hpp
Go to the documentation of this file.
2#include <iomanip>
3#include <sstream>
4
5namespace bb {
6
7class Formatter : public CLI::Formatter {
8 public:
9 Formatter() { column_width_ = 25; }
10
11 std::string make_option_opts(const CLI::Option* opt) const override
12 {
13 std::stringstream out;
14 if (!opt->get_envname().empty()) {
15 out << " [" << opt->get_envname() << "]";
16 }
17 return out.str();
18 }
19
20 std::string make_option_desc(const CLI::Option* opt) const override
21 {
22 const size_t wrap_width = 60;
23 std::stringstream out;
24
25 if (opt->get_required()) {
26 out << "\e[3mREQUIRED\e[0m ";
27 }
28 std::string desc = opt->get_description();
29 wrap_text(out, desc, wrap_width);
30 CLI::Validator* is_member_validator = get_validator(const_cast<CLI::Option*>(opt), "is_member");
31 if (is_member_validator) {
32 out << "\n";
33 std::string options = is_member_validator->get_description();
34 wrap_text(out, "Options: " + replace(options, ",", ", "), wrap_width);
35 }
36 return out.str();
37 }
38
39 std::string make_subcommand(const CLI::App* sub) const override
40 {
41 const size_t left_col_width = 25;
42 const size_t wrap_width = 60;
43 std::stringstream out;
44 std::string name = sub->get_display_name(true) + (sub->get_required() ? " " + get_label("REQUIRED") : "");
45 out << std::setw(left_col_width) << std::left << name;
46 std::string desc = sub->get_description();
47 std::istringstream iss(desc);
48 std::string word;
49 size_t current_line_length = 0;
50 std::string indent(left_col_width, ' ');
51 bool first_word = true;
52 while (iss >> word) {
53 if (!first_word && current_line_length + word.size() + 1 > wrap_width) {
54 out << "\n" << indent;
55 current_line_length = 0;
56 } else if (!first_word) {
57 out << " ";
58 current_line_length++;
59 }
60 out << word;
61 current_line_length += word.size();
62 first_word = false;
63 }
64 out << "\n";
65 return out.str();
66 }
67
68 std::string make_help(const CLI::App* app, std::string name, CLI::AppFormatMode mode) const override
69 {
70 std::stringstream out;
71 if (mode == CLI::AppFormatMode::Normal) {
72 out << CLI::Formatter::make_help(app, name, mode);
73 }
74 return out.str();
75 }
76
77 private:
78 static void wrap_text(std::ostream& out, const std::string& text, size_t width)
79 {
80 // Split by newlines first to preserve explicit line breaks
81 std::istringstream lines(text);
82 std::string line;
83 bool first_line = true;
84 while (std::getline(lines, line)) {
85 if (!first_line) {
86 out << "\n";
87 }
88 first_line = false;
89
90 // If the line fits within width, output it verbatim to preserve formatting
91 if (line.length() <= width) {
92 out << line;
93 continue;
94 }
95
96 // Line is too long - need to wrap it
97 // Preserve leading whitespace for continuation lines
98 size_t indent = 0;
99 while (indent < line.size() && (line[indent] == ' ' || line[indent] == '\t')) {
100 indent++;
101 }
102 std::string leading_space = line.substr(0, indent);
103 std::string content = line.substr(indent);
104
105 // Output leading whitespace
106 out << leading_space;
107
108 // Now wrap the content, using the same indent for continuation lines
109 std::istringstream words(content);
110 std::string word;
111 size_t line_length = indent;
112 while (words >> word) {
113 if (line_length + word.length() + 1 > width) {
114 out << "\n" << leading_space;
115 line_length = indent;
116 }
117 if (line_length > indent) {
118 out << " ";
119 line_length++;
120 }
121 out << word;
122 line_length += word.length();
123 }
124 }
125 }
126
127 CLI::Validator* get_validator(CLI::Option* opt, const std::string& name) const
128 {
129 CLI::Validator* result;
130 try {
131 result = opt->get_validator(name);
132 } catch (const CLI::OptionNotFound& err) {
133 result = nullptr;
134 }
135 return result;
136 }
137
138 std::string replace(std::string& in, const std::string& pat, const std::string& rep) const
139 {
140 size_t pos = 0;
141 while ((pos = in.find(pat, pos)) != std::string::npos) {
142 in.replace(pos, pat.size(), rep);
143 pos += rep.size();
144 }
145 return in;
146 }
147};
148} // namespace bb
static void wrap_text(std::ostream &out, const std::string &text, size_t width)
std::string make_option_desc(const CLI::Option *opt) const override
std::string make_help(const CLI::App *app, std::string name, CLI::AppFormatMode mode) const override
std::string replace(std::string &in, const std::string &pat, const std::string &rep) const
std::string make_subcommand(const CLI::App *sub) const override
std::string make_option_opts(const CLI::Option *opt) const override
CLI::Validator * get_validator(CLI::Option *opt, const std::string &name) const
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13