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 std::istringstream words(text);
81 std::string word;
82 size_t line_length = 0;
83 while (words >> word) {
84 if (line_length + word.length() + 1 > width) {
85 out << "\n";
86 line_length = 0;
87 }
88 if (line_length > 0) {
89 out << " ";
90 line_length++;
91 }
92 out << word;
93 line_length += word.length();
94 }
95 }
96
97 CLI::Validator* get_validator(CLI::Option* opt, const std::string& name) const
98 {
99 CLI::Validator* result;
100 try {
101 result = opt->get_validator(name);
102 } catch (const CLI::OptionNotFound& err) {
103 result = nullptr;
104 }
105 return result;
106 }
107
108 std::string replace(std::string& in, const std::string& pat, const std::string& rep) const
109 {
110 size_t pos = 0;
111 while ((pos = in.find(pat, pos)) != std::string::npos) {
112 in.replace(pos, pat.size(), rep);
113 pos += rep.size();
114 }
115 return in;
116 }
117};
118} // 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.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13