From 0c7929c5a8e1614b4a2577790cb2ff85b556cd83 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Thu, 26 Nov 2026 20:51:45 +0100 Subject: [PATCH] Add rfl::cli::PureFlags for pure boolean switches and clean flag help --- include/rfl/cli.hpp | 1 + include/rfl/cli/PureFlags.hpp | 52 +++++++++++++++++++++ include/rfl/cli/options.hpp | 6 +++ include/rfl/cli/parse_argv.hpp | 8 ++-- include/rfl/cli/resolve_args.hpp | 2 +- include/rfl/cli/usage.hpp | 10 +++- tests/cli/test_pure_flags.cpp | 79 ++++++++++++++++++++++++++++++++ 7 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 include/rfl/cli/PureFlags.hpp create mode 100644 tests/cli/test_pure_flags.cpp diff --git a/include/rfl/cli.hpp b/include/rfl/cli.hpp index c37792eed..7c9c24797 100644 --- a/include/rfl/cli.hpp +++ b/include/rfl/cli.hpp @@ -8,6 +8,7 @@ #include "cli/ListDelimiter.hpp" #include "cli/OptionsFirst.hpp" #include "cli/Parser.hpp" +#include "cli/PureFlags.hpp" #include "cli/Reader.hpp" #include "cli/SeparateOptionValues.hpp" #include "cli/options.hpp" diff --git a/include/rfl/cli/PureFlags.hpp b/include/rfl/cli/PureFlags.hpp new file mode 100644 index 000000000..1312bdc9d --- /dev/null +++ b/include/rfl/cli/PureFlags.hpp @@ -0,0 +1,52 @@ +#ifndef RFL_CLI_PUREFLAGS_HPP_ +#define RFL_CLI_PUREFLAGS_HPP_ + +#include + +#include "../Processors.hpp" + +namespace rfl::cli { + +/// A setting for rfl::cli::read and rfl::cli::read_app that prevents short +/// boolean flags from consuming a following `true`, `false`, `1`, or `0` token +/// as their value. Without it, `-v 1` sets `verbose = true` and consumes `1`, +/// whereas `--verbose 1` leaves `1` as a positional argument. With PureFlags, +/// both short and long boolean flags behave as pure switches and never consume +/// the next token (explicit `=` syntax like `--verbose=false` and `-v=false` +/// still works). +/// +/// This is essential for CLIs whose positional arguments can be numeric IDs +/// such as `0` or `1` (e.g., `tool in.osm -H 1`). +/// +/// This is a marker type (doesn't modify data) that only rfl::cli looks at. +/// Other formats ignore it. +struct PureFlags { + public: + template + static auto process(auto&& _named_tuple) { + return _named_tuple; + } +}; + +namespace internal { + +template +struct pure_flags : std::false_type {}; + +template <> +struct pure_flags : std::true_type {}; + +template +struct pure_flags> + : std::bool_constant<(pure_flags::value || ...)> {}; + +/// Whether the processors contain rfl::cli::PureFlags. +template +constexpr bool pure_flags_v = + pure_flags>>::value; + +} // namespace internal + +} // namespace rfl::cli + +#endif diff --git a/include/rfl/cli/options.hpp b/include/rfl/cli/options.hpp index 8a2fc7008..a815aef52 100644 --- a/include/rfl/cli/options.hpp +++ b/include/rfl/cli/options.hpp @@ -32,6 +32,7 @@ #include "AllowAbbreviations.hpp" #include "OptionsFirst.hpp" #include "Parser.hpp" +#include "PureFlags.hpp" #include "SeparateOptionValues.hpp" #include "Reader.hpp" @@ -141,6 +142,10 @@ struct Options { /// later token is a positional argument (rfl::cli::OptionsFirst). bool options_first = false; + /// Whether short boolean flags never consume a following `true`/`false`/`1`/`0` + /// token (rfl::cli::PureFlags). + bool pure_flags = false; + /// Returns the long options that `--_prefix` could abbreviate: every /// long option that starts with `_prefix`, in alphabetical order. Only the /// last segment of a dotted name is abbreviated, so `_prefix` must spell @@ -674,6 +679,7 @@ Options make_options() { } options.allow_abbreviations = allow_abbreviations_v; options.options_first = options_first_v; + options.pure_flags = pure_flags_v; using U = unwrapped_option_t; if constexpr (_all_paths) { collect_value_options>(&options); diff --git a/include/rfl/cli/parse_argv.hpp b/include/rfl/cli/parse_argv.hpp index 09889a238..c793518f2 100644 --- a/include/rfl/cli/parse_argv.hpp +++ b/include/rfl/cli/parse_argv.hpp @@ -154,9 +154,11 @@ inline rfl::Result parse_string_views( return std::string(_args[++*_i]); case OptionKind::flag: - // -f false: a flag takes a bool literal. Any other token stays where - // it is, so `-v input.txt` leaves "input.txt" positional. - if (has_next && is_bool_literal(_args[*_i + 1])) { + // -f false: a flag takes a bool literal unless PureFlags is set. Any + // other token stays where it is, so `-v input.txt` leaves "input.txt" + // positional. + if (!_options.pure_flags && has_next + && is_bool_literal(_args[*_i + 1])) { return std::string(_args[++*_i]); } return std::string(); diff --git a/include/rfl/cli/resolve_args.hpp b/include/rfl/cli/resolve_args.hpp index ceafaa1ed..f43ba2046 100644 --- a/include/rfl/cli/resolve_args.hpp +++ b/include/rfl/cli/resolve_args.hpp @@ -60,7 +60,7 @@ inline void apply_option_kinds(const Options& _options, ParsedArgs* _parsed) { } auto& value = it->second; if (option.kind == OptionKind::flag && !value.empty() - && !is_bool_literal(value)) { + && (_options.pure_flags || !is_bool_literal(value))) { reclaimed.push_back(value); value.clear(); } diff --git a/include/rfl/cli/usage.hpp b/include/rfl/cli/usage.hpp index a2878da2e..2c04b69b6 100644 --- a/include/rfl/cli/usage.hpp +++ b/include/rfl/cli/usage.hpp @@ -485,8 +485,16 @@ class UsageCollector { _arg.kind = option.kind; _arg.implicit_value = std::string(option.implicit_value); _arg.is_list = option.is_list; + const bool pure_flag = + options_->pure_flags && _arg.kind == OptionKind::flag && !_arg.positional; + if (pure_flag) { + _arg.bare = true; + } if (_default) { - _arg.default_value = usage_default_to_string(*_default, delimiter_); + auto def = usage_default_to_string(*_default, delimiter_); + if (!(pure_flag && def == "false")) { + _arg.default_value = std::move(def); + } } args_.emplace_back(std::move(_arg)); } diff --git a/tests/cli/test_pure_flags.cpp b/tests/cli/test_pure_flags.cpp new file mode 100644 index 000000000..13578a478 --- /dev/null +++ b/tests/cli/test_pure_flags.cpp @@ -0,0 +1,79 @@ +#include + +#include +#include +#include +#include + +namespace test_pure_flags { + +struct GetIdConfig { + rfl::Short<"H", rfl::DefaultVal> with_history = false; + rfl::Short<"r", rfl::DefaultVal> add_referenced = false; + rfl::Positional input; + rfl::Positional>> ids; +}; + +TEST(cli, test_pure_flags_numeric_positionals) { + const std::vector args = {"in.osm", "-H", "1", "0", "42"}; + const auto res = + rfl::cli::read>(args); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_TRUE(res->with_history.value().get()); + EXPECT_FALSE(res->add_referenced.value().get()); + EXPECT_EQ(res->input.value(), "in.osm"); + EXPECT_EQ(res->ids.value().get(), + (std::vector{"1", "0", "42"})); +} + +TEST(cli, test_pure_flags_bool_word_positionals) { + const std::vector args = {"in.osm", "-r", "false", "true"}; + const auto res = + rfl::cli::read>(args); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_TRUE(res->add_referenced.value().get()); + EXPECT_EQ(res->ids.value().get(), + (std::vector{"false", "true"})); +} + +TEST(cli, test_pure_flags_grouped_and_explicit_equals) { + const std::vector args = {"-Hr", "in.osm", "0"}; + const auto res = rfl::cli::read(args); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_TRUE(res->with_history.value().get()); + EXPECT_TRUE(res->add_referenced.value().get()); + EXPECT_EQ(res->input.value(), "in.osm"); + EXPECT_EQ(res->ids.value().get(), (std::vector{"0"})); + + const std::vector explicit_false = {"-H=false", "in.osm", "1"}; + const auto res2 = + rfl::cli::read(explicit_false); + ASSERT_TRUE(res2) << res2.error().what(); + EXPECT_FALSE(res2->with_history.value().get()); + EXPECT_EQ(res2->ids.value().get(), (std::vector{"1"})); +} + +struct UsageFlagConfig { + rfl::Short<"v", rfl::Description<"Verbose output", rfl::DefaultVal>> + verbose = false; + rfl::Description<"Use color", rfl::DefaultVal> color = true; + rfl::Positional input; +}; + +TEST(cli, test_pure_flags_usage_formatting) { + const auto text = + rfl::cli::usage("tool", true); + ASSERT_TRUE(text) << text.error().what(); + // Pure flags omit `[=]` in both synopsis and argument rows. + EXPECT_NE(text->find("Usage: tool [-v|--verbose] [--color] \n"), + std::string::npos) + << *text; + EXPECT_EQ(text->find("[=]"), std::string::npos) << *text; + // Default: false. is suppressed for pure flags, while Default: true. is kept. + EXPECT_EQ(text->find("Default: false."), std::string::npos) << *text; + EXPECT_NE(text->find("Default: true."), std::string::npos) << *text; +} + +} // namespace test_pure_flags