From e09af1952342538faa490439e198ff9dcbbc77b6 Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Sun, 27 Sep 2026 20:42:46 +0200 Subject: [PATCH 1/7] parser::program_name -> full_name --- docs/tutorial.md | 18 +++++----- include/argon/argument_parser.hpp | 36 +++++++++---------- tests/source/test_argument_parser_cfg.cpp | 22 +++++++----- .../test_argument_parser_parse_args.cpp | 14 ++++---- 4 files changed, 48 insertions(+), 42 deletions(-) diff --git a/docs/tutorial.md b/docs/tutorial.md index 8b99c09d..253aa2c9 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -1632,7 +1632,7 @@ For example: ```cpp // top-level parser -argon::argument_parser git("ap-git"); +argon::argument_parser git("argon-git"); git.version("v0.1.0") .description("A version control system built with CPP-ARGON") .default_arguments(argon::default_argument::o_help, argon::default_argument::o_version); @@ -1671,8 +1671,8 @@ push.description("Update remote refs along with associated objects"); All defined subparsers will be included in the parent parser's help message: ```txt -> ap-git --help -Program: ap-git v0.1.0 +> argon-git --help +Program: argon-git v0.1.0 A version control system built with CPP-ARGON @@ -1701,12 +1701,12 @@ When parsing command-line arguments, the parent parser will attempt to match the For example: ```cpp -argon::argument_parser git("ap-git"); +argon::argument_parser git("argon-git"); auto& submodule = git.add_subparser("submodule"); auto& submodule_init = submodule.add_subparser("init"); ``` -Running `ap-git submodule init ` will result in `` being parsed by the `submodule_init` parser. +Running `argon-git submodule init ` will result in `` being parsed by the `submodule_init` parser. ### Tracking Parser State @@ -1730,7 +1730,7 @@ Each parser tracks its state during parsing. The methods described below let you ```cpp // define the parser hierarchy -argon::argument_parser git("ap-git"); +argon::argument_parser git("argon-git"); auto& submodule = git.add_subparser("submodule"); auto& submodule_init = submodule.add_subparser("init"); @@ -1750,17 +1750,17 @@ std::cout << "submodule_init : invoked=" << submodule_init.invoked() << ", finalized=" << submodule_init.finalized() << '\n'; auto& active = git.resolved_parser(); -std::cout << "\nResolved parser : " << active.name() << " (" << active.program_name() << ")\n"; +std::cout << "\nResolved parser : " << active.name() << " (" << active.full_name() << ")\n"; ``` -If you run: `./ap-git submodule init`, you will get the following state: +If you run: `./argon-git submodule init`, you will get the following state: ```txt git : invoked=true, finalized=false submodule : invoked=true, finalized=false submodule_init : invoked=true, finalized=true -Resolved parser : init (ap-git submodule init) +Resolved parser : init (argon-git submodule init) ```
diff --git a/include/argon/argument_parser.hpp b/include/argon/argument_parser.hpp index 22860690..3d1e5166 100644 --- a/include/argon/argument_parser.hpp +++ b/include/argon/argument_parser.hpp @@ -198,17 +198,18 @@ class argument_parser { } /** - * @brief Returns the parser's full program name. + * @brief Returns the parser's full name. * * - For top-level parsers, this is the same as the parser's name. - * - For subparsers, the name is prefixed with its parent parser names. + * - For subparsers, the name is prefixed with its parent parser names: * - * Top-level parser: `git` - * Subparser: `git submodule` - * Nested subparser : `git submodule init` + * Example: + * - Top-level parser: `git` + * - Subparser: `git submodule` + * - Nested subparser : `git submodule init` */ - [[nodiscard]] std::string_view program_name() const noexcept { - return this->_program_name; + [[nodiscard]] std::string_view full_name() const noexcept { + return this->_full_name; } /** @@ -627,11 +628,11 @@ class argument_parser { throw std::logic_error(std::format( "A subparser with the given name ({}) already exists in parser '{}'", (*subparser_it)->_name, - this->_program_name + this->_full_name )); return *this->_subparsers.emplace_back( - std::unique_ptr(new argument_parser(name, this->_program_name, false)) + std::unique_ptr(new argument_parser(name, this->_full_name, false)) ); } @@ -974,7 +975,7 @@ class argument_parser { * @param os The output stream. */ void print_help(const bool verbose, std::ostream& os = std::cout) const noexcept { - os << "Program: " << this->_program_name; + os << "Program: " << this->_full_name; if (not this->_version.empty()) os << ' ' << this->_version; os << '\n'; @@ -995,7 +996,7 @@ class argument_parser { * @param os The output stream. */ void print_version(std::ostream& os = std::cout) const noexcept { - os << this->_program_name; + os << this->_full_name; if (this->_version.empty()) os << " (version unspecified)" << std::endl; else @@ -1061,7 +1062,7 @@ class argument_parser { const std::string_view name, const std::string_view parent_name, const bool dynamic ) : _name(name), - _program_name( + _full_name( dynamic ? "" : std::format("{}{}{}", parent_name, std::string(not parent_name.empty(), ' '), name) @@ -1089,17 +1090,17 @@ class argument_parser { if (this->_name.empty()) this->_name = "unknown"; - this->_program_name = this->_name; + this->_full_name = this->_name; this->_is_name_resolved = true; for (auto& sub : this->_subparsers) - sub->_update_program_name(this->_program_name); + sub->_update_program_name(this->_full_name); } void _update_program_name(std::string_view parent_name) { - this->_program_name = std::format("{} {}", parent_name, this->_name); + this->_full_name = std::format("{} {}", parent_name, this->_name); for (auto& sub : this->_subparsers) - sub->_update_program_name(this->_program_name); + sub->_update_program_name(this->_full_name); } void _verify_arg_name_pattern(const std::string_view arg_name) const { @@ -1626,8 +1627,7 @@ class argument_parser { // --- attributes --- std::string _name = ""; - std::string _program_name = - ""; // The name of the program in the format "... ". + std::string _full_name = ""; // Format: "... ". std::string _description = ""; std::string _version = ""; diff --git a/tests/source/test_argument_parser_cfg.cpp b/tests/source/test_argument_parser_cfg.cpp index c6d5690d..0ad53d24 100644 --- a/tests/source/test_argument_parser_cfg.cpp +++ b/tests/source/test_argument_parser_cfg.cpp @@ -28,22 +28,28 @@ TEST_CASE("argument_parser() should throw if the name contains whitespaces") { } TEST_CASE_FIXTURE( - test_argument_parser_cfg, "argument_parser() should set the name and program name members" + test_argument_parser_cfg, "argument_parser() should set the name and full name members" ) { CHECK_EQ(sut.name(), program_name); - CHECK_EQ(sut.program_name(), program_name); + CHECK_EQ(sut.full_name(), program_name); } TEST_CASE_FIXTURE( test_argument_parser_cfg, - "subparser's program name should be a concatenation of the parent parser's name and its own " - "name" + "subparser's full name should be a concatenation of the parent parser's name and its own name" ) { - constexpr std::string_view subparser_name = "subprogram"; + constexpr std::string_view subparser_1_name = "subprogram-1"; + constexpr std::string_view subparser_2_name = "subprogram-2"; - auto& subparser = sut.add_subparser(subparser_name); - CHECK_EQ(subparser.name(), subparser_name); - CHECK_EQ(subparser.program_name(), std::format("{} {}", sut.name(), subparser_name)); + auto& subparser_1 = sut.add_subparser(subparser_1_name); + CHECK_EQ(subparser_1.name(), subparser_1_name); + CHECK_EQ(subparser_1.full_name(), std::format("{} {}", sut.name(), subparser_1_name)); + + auto& subparser_2 = subparser_1.add_subparser(subparser_2_name); + CHECK_EQ(subparser_2.name(), subparser_2_name); + CHECK_EQ( + subparser_2.full_name(), std::format("{} {}", subparser_1.full_name(), subparser_2_name) + ); } // --- version --- diff --git a/tests/source/test_argument_parser_parse_args.cpp b/tests/source/test_argument_parser_parse_args.cpp index ab46a458..6e99685a 100644 --- a/tests/source/test_argument_parser_parse_args.cpp +++ b/tests/source/test_argument_parser_parse_args.cpp @@ -1946,7 +1946,7 @@ TEST_CASE_FIXTURE( REQUIRE_NOTHROW(dynamic_parser.parse_args(argc, argv)); CHECK_EQ(dynamic_parser.name(), "my_app"); - CHECK_EQ(dynamic_parser.program_name(), "my_app"); + CHECK_EQ(dynamic_parser.full_name(), "my_app"); free_argv(argc, argv); } @@ -1966,7 +1966,7 @@ TEST_CASE_FIXTURE( REQUIRE_NOTHROW(dynamic_parser.parse_args(argc, argv)); CHECK_EQ(dynamic_parser.name(), "my_app.exe"); - CHECK_EQ(dynamic_parser.program_name(), "my_app.exe"); + CHECK_EQ(dynamic_parser.full_name(), "my_app.exe"); free_argv(argc, argv); } @@ -1985,23 +1985,23 @@ TEST_CASE_FIXTURE( auto argv = to_char_2d_array(argv_vec); // Initial state check - subparser program names should be incomplete because the parent isn't resolved yet - CHECK_EQ(sub_cmd.program_name(), "commit"); - CHECK_EQ(nested_cmd.program_name(), "commit now"); + CHECK_EQ(sub_cmd.full_name(), "commit"); + CHECK_EQ(nested_cmd.full_name(), "commit now"); // Parse will trigger the resolution and propagation REQUIRE_NOTHROW(dynamic_parser.parse_args(argc, argv)); // Verify root parser CHECK_EQ(dynamic_parser.name(), "git"); - CHECK_EQ(dynamic_parser.program_name(), "git"); + CHECK_EQ(dynamic_parser.full_name(), "git"); // Verify first-level subparser CHECK_EQ(sub_cmd.name(), "commit"); - CHECK_EQ(sub_cmd.program_name(), "git commit"); + CHECK_EQ(sub_cmd.full_name(), "git commit"); // Verify nested subparser CHECK_EQ(nested_cmd.name(), "now"); - CHECK_EQ(nested_cmd.program_name(), "git commit now"); + CHECK_EQ(nested_cmd.full_name(), "git commit now"); free_argv(argc, argv); } From 571bc86883a381e26c61b583d06c0d10f3c3598b Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Sun, 27 Sep 2026 21:00:52 +0200 Subject: [PATCH 2/7] initial help msg formatting refactor --- include/argon/argument_parser.hpp | 47 +++++++++-- include/argon/detail/help_builder.hpp | 110 ++++++++++++++------------ include/argon/util/string.hpp | 76 ++++++++++++++++++ tests/source/test_help_builder.cpp | 22 +++--- 4 files changed, 188 insertions(+), 67 deletions(-) diff --git a/include/argon/argument_parser.hpp b/include/argon/argument_parser.hpp index 3d1e5166..afbfd74a 100644 --- a/include/argon/argument_parser.hpp +++ b/include/argon/argument_parser.hpp @@ -310,6 +310,21 @@ class argument_parser { return this->_flag_char; } + /** + * @brief Set the maximum line width for the formatted help output. + * @param width The max line width value (0 disables automatic align_textping). + * @return Reference to the argument parser. + */ + argument_parser& max_help_width(const std::size_t width) noexcept { + this->_max_help_width = width; + return *this; + } + + /// @return The configured maximum line width for the help message. + [[nodiscard]] std::size_t max_help_width() const noexcept { + return this->_max_help_width; + } + /** * @brief Add default arguments to the argument parser. * @tparam ArgvRange Type of the positional argument discriminator range. @@ -980,8 +995,11 @@ class argument_parser { os << ' ' << this->_version; os << '\n'; - if (not this->_description.empty()) - os << '\n' << std::string(this->_indent_width, ' ') << this->_description << '\n'; + if (not this->_description.empty()) { + os << '\n' + << util::align_text(this->_description, this->_indent_width, this->_max_help_width) + << '\n'; + } this->_print_subparsers(os); for (const auto& group : this->_argument_groups) @@ -1570,7 +1588,10 @@ class argument_parser { max_subparser_name_length = std::max(max_subparser_name_length, bld.name.length()); for (const auto& bld : builders) - os << '\n' << bld.get_basic(this->_indent_width, max_subparser_name_length); + os << '\n' + << bld.build_base( + this->_indent_width, max_subparser_name_length, this->_max_help_width + ); os << '\n'; } @@ -1599,12 +1620,19 @@ class argument_parser { os << " (" << util::join(group_attrs) << ')'; os << '\n'; - if (not group._description.empty()) - os << '\n' << std::string(this->_indent_width, ' ') << group._description << '\n'; + if (not group._description.empty()) { + os << '\n' + << util::align_text(group._description, this->_indent_width, this->_max_help_width) + << '\n'; + } if (verbose) { for (const auto& arg : visible_args) - os << '\n' << arg->help_builder(verbose).get(this->_indent_width) << '\n'; + os << '\n' + << arg->help_builder(verbose).build( + this->_indent_width, std::nullopt, this->_max_help_width + ) + << '\n'; } else { std::vector builders; @@ -1618,7 +1646,10 @@ class argument_parser { max_arg_name_length = std::max(max_arg_name_length, bld.name.length()); for (const auto& bld : builders) - os << '\n' << bld.get_basic(this->_indent_width, max_arg_name_length); + os << '\n' + << bld.build_base( + this->_indent_width, max_arg_name_length, this->_max_help_width + ); os << '\n'; } @@ -1636,6 +1667,8 @@ class argument_parser { char _flag_char = '-'; std::string _primary_flag_prefix = "--"; + std::size_t _max_help_width = 120ull; + // --- parsing cfg & state --- arg_ptr_vec_t _positional_args = {}; diff --git a/include/argon/detail/help_builder.hpp b/include/argon/detail/help_builder.hpp index 8a45575c..a8ebc4bc 100644 --- a/include/argon/detail/help_builder.hpp +++ b/include/argon/detail/help_builder.hpp @@ -13,6 +13,7 @@ #include "argon/traits.hpp" #include "argon/util/string.hpp" +#include #include #include #include @@ -78,22 +79,38 @@ class help_builder { /** * @param indent_width The indentation width. - * @param align_to Optional minimum width for the argument name; if provided, the name will be left-aligned and padded with spaces to this width. - * @return A basic argument description in the format " : " - * @note The help message will only be visible in the output if it's not `std::nullopt` + * @param align_to Optional minimum width for the argument name. + * @param max_line_width Optional maximum line width for text align_textping. + * @return A basic argument description string in the format " : " */ - [[nodiscard]] std::string get_basic( - const uint8_t indent_width, const std::optional align_to = std::nullopt + [[nodiscard]] std::string build_base( + const uint8_t indent_width, + const std::optional align_to = std::nullopt, + const std::optional max_line_width = std::nullopt ) const { std::ostringstream oss; + const std::size_t name_width = align_to.value_or(this->name.length()); + const std::size_t prefix_len = indent_width + name_width + 3; // + len(" : ") - oss << std::string(indent_width, ' '); - if (align_to.has_value()) - oss << std::setw(static_cast(align_to.value())) << std::left; - oss << this->name; + oss << std::string(indent_width, ' ') << std::left + << std::setw(static_cast(name_width)) << this->name; - if (not this->help.empty()) - oss << " : " << this->help; + if (this->help.empty()) + return oss.str(); + + oss << " : "; + + std::size_t text_width = 0; + if (max_line_width.has_value() && max_line_width.value() > prefix_len) + text_width = max_line_width.value() - prefix_len; + + const auto lines = util::wrap_text(this->help, text_width); + if (not lines.empty()) { + oss << lines.front(); + const std::string padding(prefix_len, ' '); + for (std::size_t i = 1; i < lines.size(); ++i) + oss << '\n' << padding << lines[i]; + } return oss.str(); } @@ -103,34 +120,34 @@ class help_builder { * to fit within a specified maximum line width. The output includes the argument name, * help message (if present), and any added parameters. * - * If no parameters are present, or the generated one-line representation fits within - * the `max_line_width`, a single-line format is used: - * " : (: , ...)" - * - * Otherwise, a multi-line format is returned, listing each parameter on its own line - * with additional indentation. - * * @param indent_width The number of spaces to insert before the argument name. + * @param align_to Optional minimum width for the argument name. * @param max_line_width Optional maximum number of characters allowed for the one-line representation. - * If the one-line output exceeds this, a multi-line format is used instead. * @return A formatted string describing the argument and its parameters. - * @note The help message is only included if it is not `std::nullopt`. */ - [[nodiscard]] std::string get( - const uint8_t indent_width, std::optional max_line_width = std::nullopt + [[nodiscard]] std::string build( + const uint8_t indent_width, + const std::optional align_to = std::nullopt, + const std::optional max_line_width = std::nullopt ) const { - std::ostringstream oss; - if (this->params.empty()) - return this->_get_single_line(indent_width); + return this->_build_compact(indent_width, align_to, max_line_width); if (max_line_width.has_value()) { - std::string single_line_str = this->_get_single_line(indent_width); - if (single_line_str.size() <= max_line_width.value()) + std::string single_line_str = + this->_build_compact(indent_width, align_to, max_line_width); + + std::size_t max_actual_len = 0; + std::istringstream iss(single_line_str); + std::string line; + while (std::getline(iss, line)) + max_actual_len = std::max(max_actual_len, line.length()); + + if (max_actual_len <= max_line_width.value()) return single_line_str; } - return this->_get_multi_line(indent_width); + return this->_build_verbose(indent_width, align_to, max_line_width); } std::string name; @@ -138,38 +155,31 @@ class help_builder { std::vector params; private: - /** - * @brief Generates a single-line string representation of the argument and its parameters. - * @param indent_width Number of spaces before the argument name. - * @return A single-line formatted description string. - */ - // clang-format off - [[nodiscard]] std::string _get_single_line(const uint8_t indent_width) const { + [[nodiscard]] std::string _build_compact( + const uint8_t indent_width, + const std::optional align_to, + const std::optional max_line_width + ) const { std::ostringstream oss; + oss << this->build_base(indent_width, align_to, max_line_width); - oss << this->get_basic(indent_width); if (not this->params.empty()) { - oss << " (" - << util::join(this->params | std::views::transform( - [](const auto& param) { return std::format("{}: {}", param.name, param.value); } - )) + oss << " (" << util::join(this->params | std::views::transform([](const auto& param) { + return std::format("{}: {}", param.name, param.value); + })) << ")"; } return oss.str(); } - // clang-format on - - /** - * @brief Generates a multi-line string representation of the argument and its parameters, formatting each parameter on its own line with aligned names. - * @param indent_width Number of spaces before the argument name and additional indentation for parameters. - * @return A multi-line formatted description string. - */ - [[nodiscard]] std::string _get_multi_line(const uint8_t indent_width) const { + [[nodiscard]] std::string _build_verbose( + const uint8_t indent_width, + const std::optional align_to, + const std::optional max_line_width + ) const { std::ostringstream oss; - - oss << this->get_basic(indent_width); + oss << this->build_base(indent_width, align_to, max_line_width); std::size_t max_param_name_len = 0ull; for (const auto& param : this->params) diff --git a/include/argon/util/string.hpp b/include/argon/util/string.hpp index 68f55ade..56b9590b 100644 --- a/include/argon/util/string.hpp +++ b/include/argon/util/string.hpp @@ -13,7 +13,9 @@ #include #include +#include #include +#include namespace argon::util { @@ -60,4 +62,78 @@ requires(traits::c_writable>) return oss.str(); } +// TODO: add doc +[[nodiscard]] inline std::vector wrap_text( + const std::string_view text, const std::size_t max_width +) { + std::vector lines; + if (text.empty()) + return lines; + + std::istringstream iss{std::string(text)}; + std::string manual_line; + + while (std::getline(iss, manual_line)) { + if (max_width == 0 or manual_line.length() <= max_width) { + lines.push_back(manual_line); + continue; + } + + std::string current_line; + std::istringstream word_stream(manual_line); + std::string word; + + while (word_stream >> word) { + if (current_line.empty()) { + current_line = word; + } + else if (current_line.length() + 1 + word.length() <= max_width) { + current_line += " " + word; + } + else { + lines.push_back(current_line); + current_line = word; + } + } + + if (not current_line.empty()) + lines.push_back(current_line); + } + + return lines; +} + +/** + * @brief Formats a block of text, wrapping it to the specified max width and preserving the indent. + * @param text The text to format. + * @param indent_width The number of spaces to prepend to each line. + * @param max_line_width The maximum total width of a line (including indent). 0 disables wrapping. + * @return The formatted string. + */ +[[nodiscard]] inline std::string align_text( + const std::string_view text, + const std::uint8_t indent_width, + const std::size_t max_line_width = 0 +) { + if (text.empty()) + return ""; + + std::size_t text_width = 0; + if (max_line_width > indent_width) + text_width = max_line_width - indent_width; + + const auto lines = wrap_text(text, text_width); + if (lines.empty()) + return ""; + + std::ostringstream oss; + const std::string padding(indent_width, ' '); + + oss << padding << lines.front(); + for (std::size_t i = 1; i < lines.size(); ++i) + oss << '\n' << padding << lines[i]; + + return oss.str(); +} + } // namespace argon::util diff --git a/tests/source/test_help_builder.cpp b/tests/source/test_help_builder.cpp index 6bf2c11b..466d5ded 100644 --- a/tests/source/test_help_builder.cpp +++ b/tests/source/test_help_builder.cpp @@ -3,6 +3,8 @@ #include #include +#include +#include TEST_SUITE_BEGIN("test_help_builder"); @@ -80,23 +82,23 @@ TEST_CASE("add_range_param should properly add a joined range parameter") { CHECK_EQ(sut.params[0].value, "1; 2; 3"); } -TEST_CASE("get_basic should return only the argument name if no help is provided") { +TEST_CASE("build_base should return only the argument name if no help is provided") { sut_type sut(arg_name, empty_msg); - std::string basic = sut.get_basic(indent_width); + std::string basic = sut.build_base(indent_width); CHECK_EQ(basic, std::string(indent_width, ' ') + arg_name); std::ostringstream expected_aligned; expected_aligned << std::string(indent_width, ' ') << std::setw(align_to) << std::left << arg_name; - CHECK_EQ(sut.get_basic(indent_width, align_to), expected_aligned.str()); + CHECK_EQ(sut.build_base(indent_width, align_to), expected_aligned.str()); } -TEST_CASE("get_basic should include help message if provided") { +TEST_CASE("build_base should include help message if provided") { sut_type sut(arg_name, help_msg); // Unaligned output (no align_to) - std::string basic = sut.get_basic(indent_width); + std::string basic = sut.build_base(indent_width); std::string expected_basic = std::string(indent_width, ' ') + arg_name + " : " + help_msg; CHECK_EQ(basic, expected_basic); @@ -106,10 +108,10 @@ TEST_CASE("get_basic should include help message if provided") { << std::string(indent_width, ' ') << std::setw(align_to) << std::left << arg_name << " : " << help_msg; - CHECK_EQ(sut.get_basic(indent_width, align_to), expected_aligned.str()); + CHECK_EQ(sut.build_base(indent_width, align_to), expected_aligned.str()); } -TEST_CASE("get should return single-line string if within max_line_width") { +TEST_CASE("build should return compact string if within max_line_width") { const std::string param_name = "param"; const std::string param_value = "value"; @@ -119,7 +121,7 @@ TEST_CASE("get should return single-line string if within max_line_width") { sut.add_param(param_name, param_value); CHECK_EQ( - sut.get(indent_width, max_line_width), + sut.build(indent_width, std::nullopt, max_line_width), std::format( "{}{} : {} ({}: {})", std::string(indent_width, ' '), @@ -131,7 +133,7 @@ TEST_CASE("get should return single-line string if within max_line_width") { ); } -TEST_CASE("get should fall back to multiline output if string is too wide") { +TEST_CASE("build should fall back to verbose multiline output if string is too wide") { constexpr std::size_t max_line_width = 0; // force multi-line const std::string param1_name = "flag"; const std::string param1_value = "on"; @@ -156,7 +158,7 @@ TEST_CASE("get should fall back to multiline output if string is too wide") { << std::string(indent_width * 2, ' ') << "- " << std::setw(static_cast(max_param_len)) << std::left << param2_name << " = " << param2_value; - CHECK_EQ(sut.get(indent_width, max_line_width), expected.str()); + CHECK_EQ(sut.build(indent_width, std::nullopt, max_line_width), expected.str()); } TEST_SUITE_END(); // test_help_builder From f53f8560b502c9be61084dbd8c99efd840a045e4 Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Sun, 27 Sep 2026 21:45:27 +0200 Subject: [PATCH 3/7] boolalpha in util::join --- include/argon/util/string.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/argon/util/string.hpp b/include/argon/util/string.hpp index 56b9590b..628e909d 100644 --- a/include/argon/util/string.hpp +++ b/include/argon/util/string.hpp @@ -43,12 +43,14 @@ template * @param range The input range to join. * @param delimiter The separator string to insert between elements. * @return A single string with all elements joined by the delimiter. + * @note This function applies the `std::boolalpha` to the string stream used within it. * @ingroup util */ template requires(traits::c_writable>) [[nodiscard]] std::string join(R&& range, const std::string_view delimiter = ", ") { std::ostringstream oss; + oss << std::boolalpha; auto it = std::ranges::begin(range); const auto end = std::ranges::end(range); From 2955b4ab252908ede33751f89a0086cb11310b7d Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Sun, 27 Sep 2026 22:01:35 +0200 Subject: [PATCH 4/7] format config --- include/argon/argument_parser.hpp | 44 +++++++++---------- include/argon/detail/help_builder.hpp | 63 +++++++++++---------------- include/argon/types.hpp | 7 +++ 3 files changed, 54 insertions(+), 60 deletions(-) diff --git a/include/argon/argument_parser.hpp b/include/argon/argument_parser.hpp index afbfd74a..d514bf01 100644 --- a/include/argon/argument_parser.hpp +++ b/include/argon/argument_parser.hpp @@ -311,18 +311,18 @@ class argument_parser { } /** - * @brief Set the maximum line width for the formatted help output. - * @param width The max line width value (0 disables automatic align_textping). + * @brief Set the formatting configuration for the help output. + * @param cfg The formatting configuration object. * @return Reference to the argument parser. */ - argument_parser& max_help_width(const std::size_t width) noexcept { - this->_max_help_width = width; + argument_parser& formatting(const format_config& cfg) noexcept { + this->_format_cfg = cfg; return *this; } - /// @return The configured maximum line width for the help message. - [[nodiscard]] std::size_t max_help_width() const noexcept { - return this->_max_help_width; + /// @return The configured help formatting options. + [[nodiscard]] const format_config& formatting() const noexcept { + return this->_format_cfg; } /** @@ -997,7 +997,11 @@ class argument_parser { if (not this->_description.empty()) { os << '\n' - << util::align_text(this->_description, this->_indent_width, this->_max_help_width) + << util::align_text( + this->_description, + this->_format_cfg.indent_width, + this->_format_cfg.max_line_width + ) << '\n'; } @@ -1588,10 +1592,7 @@ class argument_parser { max_subparser_name_length = std::max(max_subparser_name_length, bld.name.length()); for (const auto& bld : builders) - os << '\n' - << bld.build_base( - this->_indent_width, max_subparser_name_length, this->_max_help_width - ); + os << '\n' << bld.build_base(this->_format_cfg, max_subparser_name_length); os << '\n'; } @@ -1622,17 +1623,18 @@ class argument_parser { if (not group._description.empty()) { os << '\n' - << util::align_text(group._description, this->_indent_width, this->_max_help_width) + << util::align_text( + group._description, + this->_format_cfg.indent_width, + this->_format_cfg.max_line_width + ) << '\n'; } if (verbose) { for (const auto& arg : visible_args) os << '\n' - << arg->help_builder(verbose).build( - this->_indent_width, std::nullopt, this->_max_help_width - ) - << '\n'; + << arg->help_builder(verbose).build(this->_format_cfg, std::nullopt) << '\n'; } else { std::vector builders; @@ -1646,10 +1648,7 @@ class argument_parser { max_arg_name_length = std::max(max_arg_name_length, bld.name.length()); for (const auto& bld : builders) - os << '\n' - << bld.build_base( - this->_indent_width, max_arg_name_length, this->_max_help_width - ); + os << '\n' << bld.build_base(this->_format_cfg, max_arg_name_length); os << '\n'; } @@ -1667,7 +1666,7 @@ class argument_parser { char _flag_char = '-'; std::string _primary_flag_prefix = "--"; - std::size_t _max_help_width = 120ull; + format_config _format_cfg{}; // --- parsing cfg & state --- @@ -1693,7 +1692,6 @@ class argument_parser { static constexpr char _assign_char = '='; static constexpr std::uint8_t _primary_flag_prefix_length = 2u; static constexpr std::uint8_t _secondary_flag_prefix_length = 1u; - static constexpr std::uint8_t _indent_width = 2u; }; // --- argument_group method implementations --- diff --git a/include/argon/detail/help_builder.hpp b/include/argon/detail/help_builder.hpp index a8ebc4bc..9390d352 100644 --- a/include/argon/detail/help_builder.hpp +++ b/include/argon/detail/help_builder.hpp @@ -23,7 +23,7 @@ namespace argon::detail { -/// @brief A structure used to represent an argument's parameter description. +/// @brief A structure used to represent an element's parameter description. struct parameter_descriptor { std::string name; std::string value; @@ -33,7 +33,7 @@ struct parameter_descriptor { class help_builder { public: /** - * @param name The string representation of the argument's name. + * @param name The string representation of the element's name. * @param help A help message string. */ help_builder(const std::string& name, const std::string& help = "") : name(name), help(help) {} @@ -78,21 +78,18 @@ class help_builder { } /** - * @param indent_width The indentation width. - * @param align_to Optional minimum width for the argument name. - * @param max_line_width Optional maximum line width for text align_textping. - * @return A basic argument description string in the format " : " + * @param fmt The formatting configuration. + * @param align_to Optional minimum width for the element name. + * @return A basic element description string in the format " : " */ [[nodiscard]] std::string build_base( - const uint8_t indent_width, - const std::optional align_to = std::nullopt, - const std::optional max_line_width = std::nullopt + const format_config& fmt, const std::optional align_to = std::nullopt ) const { std::ostringstream oss; const std::size_t name_width = align_to.value_or(this->name.length()); - const std::size_t prefix_len = indent_width + name_width + 3; // + len(" : ") + const std::size_t prefix_len = fmt.indent_width + name_width + 3; // + len(" : ") - oss << std::string(indent_width, ' ') << std::left + oss << std::string(fmt.indent_width, ' ') << std::left << std::setw(static_cast(name_width)) << this->name; if (this->help.empty()) @@ -101,8 +98,8 @@ class help_builder { oss << " : "; std::size_t text_width = 0; - if (max_line_width.has_value() && max_line_width.value() > prefix_len) - text_width = max_line_width.value() - prefix_len; + if (fmt.max_line_width > prefix_len) + text_width = fmt.max_line_width - prefix_len; const auto lines = util::wrap_text(this->help, text_width); if (not lines.empty()) { @@ -116,26 +113,22 @@ class help_builder { } /** - * Generates a full string representation of the argument descriptor, optionally formatted - * to fit within a specified maximum line width. The output includes the argument name, + * Generates a full string representation of the element descriptor, optionally formatted + * to fit within a specified maximum line width. The output includes the element name, * help message (if present), and any added parameters. * - * @param indent_width The number of spaces to insert before the argument name. - * @param align_to Optional minimum width for the argument name. - * @param max_line_width Optional maximum number of characters allowed for the one-line representation. - * @return A formatted string describing the argument and its parameters. + * @param fmt The formatting configuration. + * @param align_to Optional minimum width for the element name. + * @return A formatted string describing the element and its parameters. */ [[nodiscard]] std::string build( - const uint8_t indent_width, - const std::optional align_to = std::nullopt, - const std::optional max_line_width = std::nullopt + const format_config& fmt, const std::optional align_to = std::nullopt ) const { if (this->params.empty()) - return this->_build_compact(indent_width, align_to, max_line_width); + return this->_build_compact(fmt, align_to); - if (max_line_width.has_value()) { - std::string single_line_str = - this->_build_compact(indent_width, align_to, max_line_width); + if (fmt.max_line_width > 0) { + std::string single_line_str = this->_build_compact(fmt, align_to); std::size_t max_actual_len = 0; std::istringstream iss(single_line_str); @@ -143,11 +136,11 @@ class help_builder { while (std::getline(iss, line)) max_actual_len = std::max(max_actual_len, line.length()); - if (max_actual_len <= max_line_width.value()) + if (max_actual_len <= fmt.max_line_width) return single_line_str; } - return this->_build_verbose(indent_width, align_to, max_line_width); + return this->_build_verbose(fmt, align_to); } std::string name; @@ -156,12 +149,10 @@ class help_builder { private: [[nodiscard]] std::string _build_compact( - const uint8_t indent_width, - const std::optional align_to, - const std::optional max_line_width + const format_config& fmt, const std::optional align_to ) const { std::ostringstream oss; - oss << this->build_base(indent_width, align_to, max_line_width); + oss << this->build_base(fmt, align_to); if (not this->params.empty()) { oss << " (" << util::join(this->params | std::views::transform([](const auto& param) { @@ -174,12 +165,10 @@ class help_builder { } [[nodiscard]] std::string _build_verbose( - const uint8_t indent_width, - const std::optional align_to, - const std::optional max_line_width + const format_config& fmt, const std::optional align_to ) const { std::ostringstream oss; - oss << this->build_base(indent_width, align_to, max_line_width); + oss << this->build_base(fmt, align_to); std::size_t max_param_name_len = 0ull; for (const auto& param : this->params) @@ -187,7 +176,7 @@ class help_builder { for (const auto& param : this->params) { oss << '\n' - << std::string(indent_width * 2, ' ') << "- " + << std::string(fmt.indent_width * 2, ' ') << "- " << std::setw(static_cast(max_param_name_len)) << std::left << param.name << " = " << param.value; } diff --git a/include/argon/types.hpp b/include/argon/types.hpp index fc335f04..ace7d17b 100644 --- a/include/argon/types.hpp +++ b/include/argon/types.hpp @@ -27,4 +27,11 @@ struct dynamic_name_t { /// @brief Tag value that enables dynamic deduction of the program name. inline constexpr dynamic_name_t dynamic_name{}; +/// @brief Configuration options for formatting the parser's help output. +struct format_config { + std::uint8_t indent_width = 2u; ///< The number of spaces to prepend to sections. + std::size_t max_line_width = + 120ull; ///< The maximum line width for text wrapping (0 disables wrapping). +}; + } // namespace argon From fd3d8a717ae6b71fe20dd14868c92469a64bab8b Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Sun, 27 Sep 2026 22:05:50 +0200 Subject: [PATCH 5/7] tests alignment --- tests/source/test_help_builder.cpp | 51 ++++++++++++++++++------------ 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/tests/source/test_help_builder.cpp b/tests/source/test_help_builder.cpp index 466d5ded..dda362a5 100644 --- a/tests/source/test_help_builder.cpp +++ b/tests/source/test_help_builder.cpp @@ -1,6 +1,7 @@ #include "doctest.h" #include +#include #include #include @@ -16,7 +17,7 @@ const std::string arg_name = "test-arg"; const std::string help_msg = "test-arg help message"; const std::string empty_msg = ""; -constexpr uint8_t indent_width = 2; +constexpr argon::format_config default_fmt{.indent_width = 2u, .max_line_width = 0ull}; constexpr std::size_t align_to = 15ull; } // namespace @@ -85,46 +86,50 @@ TEST_CASE("add_range_param should properly add a joined range parameter") { TEST_CASE("build_base should return only the argument name if no help is provided") { sut_type sut(arg_name, empty_msg); - std::string basic = sut.build_base(indent_width); - CHECK_EQ(basic, std::string(indent_width, ' ') + arg_name); + std::string basic = sut.build_base(default_fmt); + CHECK_EQ(basic, std::string(default_fmt.indent_width, ' ') + arg_name); std::ostringstream expected_aligned; expected_aligned - << std::string(indent_width, ' ') << std::setw(align_to) << std::left << arg_name; - CHECK_EQ(sut.build_base(indent_width, align_to), expected_aligned.str()); + << std::string(default_fmt.indent_width, ' ') << std::setw(align_to) << std::left + << arg_name; + CHECK_EQ(sut.build_base(default_fmt, align_to), expected_aligned.str()); } TEST_CASE("build_base should include help message if provided") { sut_type sut(arg_name, help_msg); // Unaligned output (no align_to) - std::string basic = sut.build_base(indent_width); - std::string expected_basic = std::string(indent_width, ' ') + arg_name + " : " + help_msg; + std::string basic = sut.build_base(default_fmt); + std::string expected_basic = + std::string(default_fmt.indent_width, ' ') + arg_name + " : " + help_msg; CHECK_EQ(basic, expected_basic); // Aligned output (with align_to) std::ostringstream expected_aligned; expected_aligned - << std::string(indent_width, ' ') << std::setw(align_to) << std::left << arg_name << " : " - << help_msg; + << std::string(default_fmt.indent_width, ' ') << std::setw(align_to) << std::left + << arg_name << " : " << help_msg; - CHECK_EQ(sut.build_base(indent_width, align_to), expected_aligned.str()); + CHECK_EQ(sut.build_base(default_fmt, align_to), expected_aligned.str()); } TEST_CASE("build should return compact string if within max_line_width") { const std::string param_name = "param"; const std::string param_value = "value"; - constexpr std::size_t max_line_width = std::numeric_limits::max(); + constexpr argon::format_config wide_fmt{ + .indent_width = 2u, .max_line_width = std::numeric_limits::max() + }; sut_type sut(arg_name, help_msg); sut.add_param(param_name, param_value); CHECK_EQ( - sut.build(indent_width, std::nullopt, max_line_width), + sut.build(wide_fmt, std::nullopt), std::format( "{}{} : {} ({}: {})", - std::string(indent_width, ' '), + std::string(wide_fmt.indent_width, ' '), arg_name, help_msg, param_name, @@ -134,7 +139,11 @@ TEST_CASE("build should return compact string if within max_line_width") { } TEST_CASE("build should fall back to verbose multiline output if string is too wide") { - constexpr std::size_t max_line_width = 0; // force multi-line + constexpr argon::format_config narrow_fmt{ + .indent_width = 2u, + .max_line_width = 0ull // force multi-line + }; + const std::string param1_name = "flag"; const std::string param1_value = "on"; const std::string param2_name = "mode"; @@ -146,19 +155,21 @@ TEST_CASE("build should fall back to verbose multiline output if string is too w std::size_t max_param_len = std::max(param1_name.length(), param2_name.length()); std::ostringstream expected; - expected << std::string(indent_width, ' ') << arg_name << " : " << help_msg; + expected << std::string(narrow_fmt.indent_width, ' ') << arg_name << " : " << help_msg; expected << "\n" - << std::string(indent_width * 2, ' ') << "- " << std::setw(static_cast(max_param_len)) - << std::left << param1_name << " = " << param1_value; + << std::string(narrow_fmt.indent_width * 2, ' ') << "- " + << std::setw(static_cast(max_param_len)) << std::left << param1_name << " = " + << param1_value; expected << "\n" - << std::string(indent_width * 2, ' ') << "- " << std::setw(static_cast(max_param_len)) - << std::left << param2_name << " = " << param2_value; + << std::string(narrow_fmt.indent_width * 2, ' ') << "- " + << std::setw(static_cast(max_param_len)) << std::left << param2_name << " = " + << param2_value; - CHECK_EQ(sut.build(indent_width, std::nullopt, max_line_width), expected.str()); + CHECK_EQ(sut.build(narrow_fmt, std::nullopt), expected.str()); } TEST_SUITE_END(); // test_help_builder From 0801332e3536b637b9363c9d1d5098a09bc91d89 Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Wed, 14 Oct 2026 21:09:31 +0200 Subject: [PATCH 6/7] docs alignment --- docs/tutorial.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/tutorial.md b/docs/tutorial.md index 253aa2c9..7a2f35d0 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -289,6 +289,24 @@ parser.add_positional_argument("number", "n") .help("a positive integer value"); ``` +> [!TIP] +> +> **Multi-line Help Messages** +> +> The help string fully supports manual line breaks (`\n`). The parser's formatting engine will automatically detect these line breaks and align all subsequent lines to the correct indentation offset. This is perfect for appending structural notes or warnings to your argument descriptions. +> ```cpp +> parser.add_optional_argument("legacy-feature") +> .help("Enables the legacy processing mode.\n\nWARNING: This feature is deprecated and will be removed in v6.0."); +> +> ``` +> The help message for the `legacy-feature` argument would look like this: +> ``` +> --legacy-feature : Enables the legacy processing mode. +> +> WARNING: This feature is deprecated +> and will be removed in v6.0. +> ``` +
#### Hidden - If this option is set for an argument, then it will not be included in the program description. From f5785bafeae9aca544f93edd5e665d129ce513ac Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Wed, 14 Oct 2026 22:04:36 +0200 Subject: [PATCH 7/7] resolved comments --- README.md | 7 +- docs/tutorial.md | 102 ++++++++++++++++++++++---- include/argon/argument_parser.hpp | 3 +- include/argon/detail/help_builder.hpp | 25 +++---- include/argon/util/string.hpp | 55 +++++++------- tests/source/test_help_builder.cpp | 4 +- 6 files changed, 135 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 0353a242..b70e5384 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,12 @@ - [Bazel Build System](/docs/tutorial.md#bazel-build-system) - [Downloading the Library](/docs/tutorial.md#downloading-the-library) - [The Parser Class](/docs/tutorial.md#the-parser-class) - - [Dynamic Program Name](/docs/tutorial.md#dynamic-program-name) + - [Program Name](/docs/tutorial.md#program-name) + - [Version and Description](/docs/tutorial.md#version-and-description) + - [Help Message Formatting](/docs/tutorial.md#help-message-formatting) + - [Flag Character](/docs/tutorial.md#flag-character) + - [Verbosity](/docs/tutorial.md#verbosity) + - [Unknown Arguments Policy](/docs/tutorial.md#unknown-arguments-policy) - [Adding Arguments](/docs/tutorial.md#adding-arguments) - [Syntax](/docs/tutorial.md#syntax) - [Names](/docs/tutorial.md#names) diff --git a/docs/tutorial.md b/docs/tutorial.md index 7a2f35d0..c72a325d 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -5,7 +5,12 @@ - [Bazel Build System](#bazel-build-system) - [Downloading the Library](#downloading-the-library) - [The Parser Class](#the-parser-class) - - [Dynamic Program Name](#dynamic-program-name) + - [Program Name](#program-name) + - [Version and Description](#version-and-description) + - [Help Message Formatting](#help-message-formatting) + - [Flag Character](#flag-character) + - [Verbosity](#verbosity) + - [Unknown Arguments Policy](#unknown-arguments-policy) - [Adding Arguments](#adding-arguments) - [Syntax](#syntax) - [Names](#names) @@ -148,26 +153,27 @@ If you do not use CMake, you can download the desired [library release](https:// ## The Parser Class -To use the argument parser in your code you need to use the `argon::argument_parser` class. +To use the argument parser in your code, you need to instantiate the `argon::argument_parser` class. This class acts as the central hub for defining your command-line interface, configuring its behavior, and parsing the input. ```cpp -argon::argument_parser parser("program"); -parser.version("alpha") +argon::argument_parser parser("myprog"); +parser.version("v0.1.0") .description("Description of the program") + .formatting({.indent_width = 4u, .max_line_width = 80ull}) .verbose(); ``` +The parser can be fully configured using a fluent API. The following subsections detail all available configuration attributes. + +### Program Name + +When creating an argument parser instance, you must provide a program name to the constructor. This name is used in the generated help message. + > [!IMPORTANT] -> * When creating an argument parser instance, you must provide either a strict program name string or the [`argon::dynamic_name` tag](#dynamic-program-name) to the constructor. If a strict program name is provided, it cannot be empty and must not contain whitespace characters. -> * Additional parameters you can specify for a parser instance include: -> * The program's version and description - used in the parser's configuration output (`std::cout << parser`). -> * Verbosity mode - `false` by default; if set to `true` the parser's configuration output will include more detailed info about arguments' parameters in addition to their names and help messages. -> * [Arguments](#arguments) - specify the values/options accepted by the program. -> * [The unknown argument flags handling policy](#4-unknown-argument-flag-handling). -> * [Argument Groups](#argument-groups) - organize related optional arguments into sections and optionally enforce usage rules. -> * [Subparsers](#subparsers) - create hierarchical CLI subcommand structures. +> +> If you provide a strict string (e.g., `"myprog"`), it cannot be empty and **must not contain whitespace characters**. -### Dynamic Program Name +**Dynamic Program Name** If you prefer not to hardcode the program name, you can instruct the parser to dynamically deduce it from the command-line input (specifically `argv[0]`) by initializing it with the `argon::dynamic_name` tag: @@ -178,11 +184,79 @@ argon::argument_parser parser(argon::dynamic_name); parser.try_parse_args(argc, argv); ``` -When the parser processes `argc` and `argv`, it extracts the executable's base name, strips any preceding directory paths (e.g., `/usr/bin/my_app` or `C:\path\to\my_app.exe` cleanly becomes `my_app` / `my_app.exe`), and automatically propagates this resolved name down to all of its [subparsers](#subparsers). +When the parser processes `argc` and `argv`, it extracts the executable's base name and cleanly strips any preceding directory paths (e.g., `/usr/bin/my_app` or `C:\path\to\my_app.exe` becomes `my_app`). This resolved name is automatically propagated down to all of its [subparsers](#subparsers). > [!WARNING] +> > If you configure a parser with `argon::dynamic_name`, you **must** use the `argc, argv` parsing overloads (like `parse_args(argc, argv)`). Attempting to use the range-based parsing functions (e.g., passing a `std::span` or `std::vector`) before the parser has resolved its name will result in a `std::logic_error`. +### Version and Description + +You can provide a version and a description for your program. These are displayed at the very top of the parser's configuration output (e.g., when the user passes the `--help` flag). + +```cpp +parser.version("v1.0.0") + .description("A high-performance file processing utility."); +``` + +> [!WARNING] +> The version string cannot contain control characters (such as line breaks or tabs). Attempting to use them will throw an `argon::invalid_configuration` exception. + +### Help Message Formatting + +You can customize the layout of the parser's automatically generated help message by passing an `argon::format_config` object to the `formatting()` method. This allows you to control the indentation of argument descriptions and enable automatic text wrapping for long help messages. + +```cpp +parser.formatting({ + .indent_width = 4u, // Set section indentation to 4 spaces (default: 2) + .max_line_width = 80ull // Wrap help text at 80 characters (default: 120) +}); + +``` + +> [!NOTE] +> +> Setting `max_line_width` to `0` disables automatic text wrapping entirely. Manual line breaks (`\n`) in your help strings are always respected and properly aligned regardless of the `max_line_width` setting. + +### Flag Character + +By default, the parser uses the POSIX-standard hyphen (`-`) as the flag prefix character (e.g., `-h`, `--help`). You can change this to support different CLI styles, such as Windows-style slashes (`/`), by setting a custom flag character. + +```cpp +argon::argument_parser parser("program"); + +// Change the flag character to a forward slash +parser.flag_char('/'); + +// force the usage of a single flag character by making the `help` name secondary +parser.add_optional_argument("help", argon::n_secondary) + .help("Display the help message"); + +parser.try_parse_args(argc, argv); + +/* Example execution: +> ./program /help +``` + +> [!WARNING] +> +> * The flag character must be a printable ASCII character and cannot be the same as the assignment character (`=`). +> * You **must** set the flag character before adding any arguments to the parser. Attempting to change it after arguments have been added will result in an `argon::invalid_configuration` exception. + +### Verbosity + +The verbosity mode is `false` by default. If enabled using `.verbose()`, the parser's help output will include highly detailed information about each argument's configuration parameters (such as its `nargs` bounds, valid choices, default values, and implicit values) natively appended to the standard name and help message. + +```cpp +parser.verbose(true); +``` + +### Unknown Arguments Policy + +By default, the parser throws an exception if it encounters a command-line argument that it does not recognize. You can alter this behavior (e.g., to ignore them, warn the user, or treat them as positional values) using the `unknown_arguments_policy()` method. + +For detailed information on the available policies and their behaviors, see the [Unknown Argument Flag Handling](#4-unknown-argument-flag-handling) section. +


diff --git a/include/argon/argument_parser.hpp b/include/argon/argument_parser.hpp index d514bf01..546b8e40 100644 --- a/include/argon/argument_parser.hpp +++ b/include/argon/argument_parser.hpp @@ -1633,8 +1633,7 @@ class argument_parser { if (verbose) { for (const auto& arg : visible_args) - os << '\n' - << arg->help_builder(verbose).build(this->_format_cfg, std::nullopt) << '\n'; + os << '\n' << arg->help_builder(verbose).build(this->_format_cfg) << '\n'; } else { std::vector builders; diff --git a/include/argon/detail/help_builder.hpp b/include/argon/detail/help_builder.hpp index 9390d352..5ef0e77f 100644 --- a/include/argon/detail/help_builder.hpp +++ b/include/argon/detail/help_builder.hpp @@ -79,14 +79,13 @@ class help_builder { /** * @param fmt The formatting configuration. - * @param align_to Optional minimum width for the element name. + * @param align_to Optional minimum width for the element name (0 means no extra alignment). * @return A basic element description string in the format " : " */ - [[nodiscard]] std::string build_base( - const format_config& fmt, const std::optional align_to = std::nullopt - ) const { + [[nodiscard]] std::string build_base(const format_config& fmt, const std::size_t align_to = 0) + const { std::ostringstream oss; - const std::size_t name_width = align_to.value_or(this->name.length()); + const std::size_t name_width = std::max(align_to, this->name.length()); const std::size_t prefix_len = fmt.indent_width + name_width + 3; // + len(" : ") oss << std::string(fmt.indent_width, ' ') << std::left @@ -118,12 +117,10 @@ class help_builder { * help message (if present), and any added parameters. * * @param fmt The formatting configuration. - * @param align_to Optional minimum width for the element name. + * @param align_to Optional minimum width for the element name (0 means no extra alignment). * @return A formatted string describing the element and its parameters. */ - [[nodiscard]] std::string build( - const format_config& fmt, const std::optional align_to = std::nullopt - ) const { + [[nodiscard]] std::string build(const format_config& fmt, const std::size_t align_to = 0) const { if (this->params.empty()) return this->_build_compact(fmt, align_to); @@ -148,9 +145,8 @@ class help_builder { std::vector params; private: - [[nodiscard]] std::string _build_compact( - const format_config& fmt, const std::optional align_to - ) const { + [[nodiscard]] std::string _build_compact(const format_config& fmt, const std::size_t align_to) + const { std::ostringstream oss; oss << this->build_base(fmt, align_to); @@ -164,9 +160,8 @@ class help_builder { return oss.str(); } - [[nodiscard]] std::string _build_verbose( - const format_config& fmt, const std::optional align_to - ) const { + [[nodiscard]] std::string _build_verbose(const format_config& fmt, const std::size_t align_to) + const { std::ostringstream oss; oss << this->build_base(fmt, align_to); diff --git a/include/argon/util/string.hpp b/include/argon/util/string.hpp index 628e909d..3767069b 100644 --- a/include/argon/util/string.hpp +++ b/include/argon/util/string.hpp @@ -2,10 +2,8 @@ // This file is part of the CPP-ARGON project (https://github.com/SpectraL519/cpp-argon). // Licensed under the MIT License. See the LICENSE file in the project root for full license information. -/** - * @file argon/util/string.hpp - * @brief Provides common string utility functions. - */ +/// @file argon/util/string.hpp +/// @brief Provides common string utility functions. #pragma once @@ -19,12 +17,10 @@ namespace argon::util { -/** - * @brief Converts a value to `std::string`. - * @tparam T The value type (must satisfy the @ref argon::traits::c_writable concept). - * @param value The value to convert. - * @ingroup util - */ +/// @brief Converts a value to `std::string`. +/// @tparam T The value type (must satisfy the @ref argon::traits::c_writable concept). +/// @param value The value to convert. +/// @ingroup util template [[nodiscard]] std::string as_string(T&& value) noexcept { std::ostringstream oss; @@ -37,15 +33,13 @@ template return std::ranges::any_of(str, [](unsigned char c) { return std::isspace(c); }); } -/** - * @brief Joins elements of a range into a single string with a delimiter. - * @tparam R The type of the value range. The value type of R must satisfy the @ref c_writable concept. - * @param range The input range to join. - * @param delimiter The separator string to insert between elements. - * @return A single string with all elements joined by the delimiter. - * @note This function applies the `std::boolalpha` to the string stream used within it. - * @ingroup util - */ +/// @brief Joins elements of a range into a single string with a delimiter. +/// @tparam R The type of the value range. The value type of R must satisfy the @ref c_writable concept. +/// @param range The input range to join. +/// @param delimiter The separator string to insert between elements. +/// @return A single string with all elements joined by the delimiter. +/// @note This function applies the `std::boolalpha` to the string stream used within it. +/// @ingroup util template requires(traits::c_writable>) [[nodiscard]] std::string join(R&& range, const std::string_view delimiter = ", ") { @@ -64,7 +58,16 @@ requires(traits::c_writable>) return oss.str(); } -// TODO: add doc +/// @brief Wraps a block of text into multiple lines based on a maximum line width. +/// +/// This function processes the text line by line, preserving any existing manual +/// line breaks (`\n`). If a line exceeds the specified `max_width`, it is wrapped +/// at word boundaries. If `max_width` is 0, automatic text wrapping is disabled. +/// +/// @param text The input text to be wrapped. +/// @param max_width The maximum number of characters allowed per line (0 disables wrapping). +/// @return A vector of strings, where each element represents a single line of the wrapped text. +/// @ingroup util [[nodiscard]] inline std::vector wrap_text( const std::string_view text, const std::size_t max_width ) { @@ -105,13 +108,11 @@ requires(traits::c_writable>) return lines; } -/** - * @brief Formats a block of text, wrapping it to the specified max width and preserving the indent. - * @param text The text to format. - * @param indent_width The number of spaces to prepend to each line. - * @param max_line_width The maximum total width of a line (including indent). 0 disables wrapping. - * @return The formatted string. - */ +/// @brief Formats a block of text, wrapping it to the specified max width and preserving the indent. +/// @param text The text to format. +/// @param indent_width The number of spaces to prepend to each line. +/// @param max_line_width The maximum total width of a line (including indent). 0 disables wrapping. +/// @return The formatted string. [[nodiscard]] inline std::string align_text( const std::string_view text, const std::uint8_t indent_width, diff --git a/tests/source/test_help_builder.cpp b/tests/source/test_help_builder.cpp index dda362a5..0b81c665 100644 --- a/tests/source/test_help_builder.cpp +++ b/tests/source/test_help_builder.cpp @@ -126,7 +126,7 @@ TEST_CASE("build should return compact string if within max_line_width") { sut.add_param(param_name, param_value); CHECK_EQ( - sut.build(wide_fmt, std::nullopt), + sut.build(wide_fmt), std::format( "{}{} : {} ({}: {})", std::string(wide_fmt.indent_width, ' '), @@ -169,7 +169,7 @@ TEST_CASE("build should fall back to verbose multiline output if string is too w << std::setw(static_cast(max_param_len)) << std::left << param2_name << " = " << param2_value; - CHECK_EQ(sut.build(narrow_fmt, std::nullopt), expected.str()); + CHECK_EQ(sut.build(narrow_fmt), expected.str()); } TEST_SUITE_END(); // test_help_builder