Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/rfl/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
52 changes: 52 additions & 0 deletions include/rfl/cli/PureFlags.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef RFL_CLI_PUREFLAGS_HPP_
#define RFL_CLI_PUREFLAGS_HPP_

#include <type_traits>

#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 <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple;
}
};

namespace internal {

template <class T>
struct pure_flags : std::false_type {};

template <>
struct pure_flags<PureFlags> : std::true_type {};

template <class... Ps>
struct pure_flags<Processors<Ps...>>
: std::bool_constant<(pure_flags<Ps>::value || ...)> {};

/// Whether the processors contain rfl::cli::PureFlags.
template <class T>
constexpr bool pure_flags_v =
pure_flags<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;

} // namespace internal

} // namespace rfl::cli

#endif
6 changes: 6 additions & 0 deletions include/rfl/cli/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "AllowAbbreviations.hpp"
#include "OptionsFirst.hpp"
#include "Parser.hpp"
#include "PureFlags.hpp"
#include "SeparateOptionValues.hpp"
#include "Reader.hpp"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -674,6 +679,7 @@ Options make_options() {
}
options.allow_abbreviations = allow_abbreviations_v<ProcessorsType>;
options.options_first = options_first_v<ProcessorsType>;
options.pure_flags = pure_flags_v<ProcessorsType>;
using U = unwrapped_option_t<T, ProcessorsType>;
if constexpr (_all_paths) {
collect_value_options<T, ProcessorsType, type_list<>>(&options);
Expand Down
8 changes: 5 additions & 3 deletions include/rfl/cli/parse_argv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ inline rfl::Result<ParsedArgs> 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();
Expand Down
2 changes: 1 addition & 1 deletion include/rfl/cli/resolve_args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
10 changes: 9 additions & 1 deletion include/rfl/cli/usage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
79 changes: 79 additions & 0 deletions tests/cli/test_pure_flags.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <gtest/gtest.h>

#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <string>
#include <vector>

namespace test_pure_flags {

struct GetIdConfig {
rfl::Short<"H", rfl::DefaultVal<bool>> with_history = false;
rfl::Short<"r", rfl::DefaultVal<bool>> add_referenced = false;
rfl::Positional<std::string> input;
rfl::Positional<rfl::DefaultVal<std::vector<std::string>>> ids;
};

TEST(cli, test_pure_flags_numeric_positionals) {
const std::vector<std::string> args = {"in.osm", "-H", "1", "0", "42"};
const auto res =
rfl::cli::read<GetIdConfig, rfl::cli::PureFlags,
rfl::cli::ListDelimiter<"">>(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<std::string>{"1", "0", "42"}));
}

TEST(cli, test_pure_flags_bool_word_positionals) {
const std::vector<std::string> args = {"in.osm", "-r", "false", "true"};
const auto res =
rfl::cli::read<GetIdConfig, rfl::cli::PureFlags,
rfl::cli::ListDelimiter<"">>(args);
ASSERT_TRUE(res) << res.error().what();
EXPECT_TRUE(res->add_referenced.value().get());
EXPECT_EQ(res->ids.value().get(),
(std::vector<std::string>{"false", "true"}));
}

TEST(cli, test_pure_flags_grouped_and_explicit_equals) {
const std::vector<std::string> args = {"-Hr", "in.osm", "0"};
const auto res = rfl::cli::read<GetIdConfig, rfl::cli::PureFlags>(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<std::string>{"0"}));

const std::vector<std::string> explicit_false = {"-H=false", "in.osm", "1"};
const auto res2 =
rfl::cli::read<GetIdConfig, rfl::cli::PureFlags>(explicit_false);
ASSERT_TRUE(res2) << res2.error().what();
EXPECT_FALSE(res2->with_history.value().get());
EXPECT_EQ(res2->ids.value().get(), (std::vector<std::string>{"1"}));
}

struct UsageFlagConfig {
rfl::Short<"v", rfl::Description<"Verbose output", rfl::DefaultVal<bool>>>
verbose = false;
rfl::Description<"Use color", rfl::DefaultVal<bool>> color = true;
rfl::Positional<std::string> input;
};

TEST(cli, test_pure_flags_usage_formatting) {
const auto text =
rfl::cli::usage<UsageFlagConfig, rfl::cli::PureFlags>("tool", true);
ASSERT_TRUE(text) << text.error().what();
// Pure flags omit `[=<bool>]` in both synopsis and argument rows.
EXPECT_NE(text->find("Usage: tool [-v|--verbose] [--color] <input>\n"),
std::string::npos)
<< *text;
EXPECT_EQ(text->find("[=<bool>]"), 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