39 : name(name), help(help) {}
46 void add_param(
const std::string& name,
const std::string& value) {
47 this->params.emplace_back(name, value);
56 template <c_writable T>
57 void add_param(
const std::string& name,
const T& value) {
58 std::ostringstream oss;
59 oss << std::boolalpha << value;
60 this->params.emplace_back(name, oss.str());
70 template <std::ranges::range R>
73 const std::string& name,
75 const std::string_view delimiter = default_delimiter
77 this->params.emplace_back(name,
join(range, delimiter));
87 const uint8_t indent_width,
const std::optional<std::size_t> align_to = std::nullopt
89 std::ostringstream oss;
91 oss << std::string(indent_width,
' ');
92 if (align_to.has_value())
93 oss << std::setw(static_cast<int>(align_to.value())) << std::left;
96 if (this->help.has_value())
97 oss <<
" : " << this->help.value();
120 [[nodiscard]] std::string
get(
121 const uint8_t indent_width, std::optional<std::size_t> max_line_width = std::nullopt
123 std::ostringstream oss;
125 if (this->params.empty())
126 return this->_get_single_line(indent_width);
128 if (max_line_width.has_value()) {
129 std::string single_line_str = this->_get_single_line(indent_width);
130 if (single_line_str.size() <= max_line_width.value())
131 return single_line_str;
134 return this->_get_multi_line(indent_width);
138 std::optional<std::string> help;
139 std::vector<parameter_descriptor> params;
148 [[nodiscard]] std::string _get_single_line(
const uint8_t indent_width)
const {
149 std::ostringstream oss;
152 if (not this->params.empty()) {
154 <<
join(this->params | std::views::transform(
155 [](
const auto& param) {
return std::format(
"{}: {}", param.name, param.value); }
170 [[nodiscard]] std::string _get_multi_line(
const uint8_t indent_width)
const {
171 std::ostringstream oss;
175 std::size_t max_param_name_len = 0ull;
176 for (
const auto& param : this->params)
177 max_param_name_len = std::max(max_param_name_len, param.name.size());
179 for (
const auto& param : this->params) {
181 << std::string(indent_width * 2,
' ') <<
"- "
182 << std::setw(
static_cast<int>(max_param_name_len)) << std::left << param.name
183 <<
" = " << param.value;
189 static constexpr std::string_view default_delimiter =
", ";
A structure used to represent an argument's description.
std::string get(const uint8_t indent_width, std::optional< std::size_t > max_line_width=std::nullopt) const
std::string get_basic(const uint8_t indent_width, const std::optional< std::size_t > align_to=std::nullopt) const
argument_descriptor(const std::string &name, const std::optional< std::string > &help)
void add_range_param(const std::string &name, const R &range, const std::string_view delimiter=default_delimiter)
Adds a range parameter descriptor with the given value.
void add_param(const std::string &name, const T &value)
Adds a parameter descriptor with the given value.
void add_param(const std::string &name, const std::string &value)
Adds a parameter descriptor with the given string value.
The concept is satisfied when T overloads the std::ostream operator <<.
Provides the general concept definitions.
Provides common string utility functions.
std::string join(const R &range, const std::string_view delimiter=", ")
Joins elements of a range into a single string with a delimiter.
A structure used to represent an argument's parameter description.