From 00278eae7726f5dc5a381872fb663c9398d1d07d Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 27 Nov 2026 08:01:50 +0100 Subject: [PATCH] Add UsageOptions::header, max_option_width, and hidden_groups to rfl::cli::usage --- include/rfl/cli/usage.hpp | 48 ++++++++++++++++++++++++++++++--- tests/cli/test_usage_custom.cpp | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/include/rfl/cli/usage.hpp b/include/rfl/cli/usage.hpp index 9d882423f..252c80dee 100644 --- a/include/rfl/cli/usage.hpp +++ b/include/rfl/cli/usage.hpp @@ -44,6 +44,9 @@ namespace rfl::cli { /// Runtime customization for `rfl::cli::usage` and `rfl::cli::read_app`. struct UsageOptions { bool detailed = true; + /// If non-empty, shown before the Usage line in detailed output (overrides + /// any header from rfl::cli::App). + std::string header = {}; /// If non-empty, replaces the auto-generated synopsis after "Usage: ". std::string synopsis = {}; /// If non-empty, shown after the Usage line in detailed output. @@ -53,6 +56,13 @@ struct UsageOptions { /// If non-empty, places built-in `--help` and `--version` in this group /// (at the start of the group if fields already use it). std::string help_group = {}; + /// Maximum left-column width used to align option descriptions; options + /// wider than this place their description on the next line instead of + /// widening every other row. Set to 0 for no cap. + size_t max_option_width = 40; + /// Group names to omit from generated detailed help output (e.g. conditional + /// feature groups hidden at runtime). + std::vector hidden_groups = {}; }; namespace internal { @@ -674,7 +684,18 @@ inline std::string format_usage(const std::string& _program_name, const std::optional _delimiter, const bool _options_first, const std::string& _header) { - auto out = _opts.detailed ? _header : std::string(); + auto out = std::string(); + if (_opts.detailed) { + if (!_opts.header.empty()) { + out += _opts.header; + if (!out.ends_with('\n')) { + out += '\n'; + } + out += '\n'; + } else { + out += _header; + } + } if (!_opts.synopsis.empty()) { out += "Usage: " + _opts.synopsis + "\n"; } else { @@ -708,7 +729,12 @@ inline std::string format_usage(const std::string& _program_name, for (const auto& arg : _args) { rows.emplace_back(usage_arg_to_string(arg, ", ", _delimiter), usage_help_text(arg)); - width = std::max(width, rows.back().first.size()); + const size_t row_w = rows.back().first.size(); + if (_opts.max_option_width == 0 || row_w <= _opts.max_option_width) { + width = std::max(width, row_w); + } else { + width = std::max(width, _opts.max_option_width); + } } constexpr size_t indent = 2; @@ -718,7 +744,11 @@ inline std::string format_usage(const std::string& _program_name, const std::string& _right) { out += std::string(indent, ' ') + _left; if (!_right.empty()) { - out += std::string(width - _left.size() + gap, ' '); + if (_left.size() <= width) { + out += std::string(width - _left.size() + gap, ' '); + } else { + out += continuation; + } for (const char c : _right) { if (c == '\n') { out += continuation; @@ -864,6 +894,18 @@ rfl::Result usage(const std::string& _program_name, args = std::move(builtin_args); args.insert(args.end(), fields.begin(), fields.end()); } + if (!_opts.hidden_groups.empty()) { + args.erase( + std::remove_if( + args.begin(), args.end(), + [&](const internal::UsageArg& _a) { + return !_a.group.empty() && + std::find(_opts.hidden_groups.begin(), + _opts.hidden_groups.end(), + _a.group) != _opts.hidden_groups.end(); + }), + args.end()); + } return internal::format_usage( _program_name, args, _opts, delimiter, options.options_first, internal::app_header(_program_name)); diff --git a/tests/cli/test_usage_custom.cpp b/tests/cli/test_usage_custom.cpp index 0a83917c0..32f1be14a 100644 --- a/tests/cli/test_usage_custom.cpp +++ b/tests/cli/test_usage_custom.cpp @@ -71,4 +71,52 @@ TEST(cli, test_usage_custom_synopsis_description_footer_and_help_group) { EXPECT_EQ(text.find("input-filenames"), std::string::npos); } +TEST(cli, test_usage_custom_header_before_usage) { + const rfl::cli::UsageOptions opts{ + .detailed = true, + .header = "solc, the Solidity commandline compiler.\n\nNo warranty.", + .synopsis = "solc [options] [input_file...]", + }; + const auto text = rfl::cli::usage("solc", opts); + ASSERT_TRUE(text) << text.error().what(); + EXPECT_TRUE(text->starts_with( + "solc, the Solidity commandline compiler.\n\nNo warranty.\n\n" + "Usage: solc [options] [input_file...]\n")) + << *text; +} + +TEST(cli, test_usage_max_option_width_wraps_wide_options) { + struct WideOptions { + rfl::Description<"Short flag", rfl::DefaultVal> verbose = false; + rfl::Description<"Very wide option label", std::optional> + very_long_option_name_exceeding_forty_characters; + }; + const auto text = + rfl::cli::usage("tool", true); + ASSERT_TRUE(text) << text.error().what(); + // Short option should not be padded past the 40-char cap. + EXPECT_EQ(text->find("--verbose "), + std::string::npos) + << *text; + // Wide option wraps its description onto the next line. + EXPECT_NE( + text->find( + "--very-long-option-name-exceeding-forty-characters=\n"), + std::string::npos) + << *text; +} + +TEST(cli, test_usage_hidden_groups) { + const rfl::cli::UsageOptions opts{ + .detailed = true, + .hidden_groups = {"INPUT OPTIONS"}, + }; + const auto text = rfl::cli::usage("osmium cat", opts); + ASSERT_TRUE(text) << text.error().what(); + EXPECT_NE(text->find("COMMAND OPTIONS:"), std::string::npos) << *text; + EXPECT_NE(text->find("COMMON OPTIONS:"), std::string::npos) << *text; + EXPECT_EQ(text->find("INPUT OPTIONS:"), std::string::npos) << *text; + EXPECT_EQ(text->find("--input-format"), std::string::npos) << *text; +} + } // namespace test_usage_custom