Skip to content

Let a list as the last positional field take the remaining arguments in rfl::cli - #36

Closed
clockblocker wants to merge 1 commit into
wave-base-8from
cli-list-positional
Closed

clockblocker wants to merge 1 commit into
wave-base-8from
cli-list-positional

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

Lets a list declared as the last rfl::Positional field consume all remaining positional arguments in rfl::cli::read, instead of failing with Too many positional arguments. Purely additive.

Example

struct Config {
  rfl::Positional<std::string> command;
  rfl::Positional<std::vector<std::string>> files;
  rfl::DefaultVal<bool> verbose = false;
};

// Before: Too many positional arguments: expected at most 2, got 3.
// After:  command="run", files={"a", "b", "c"}, verbose=true
rfl::cli::read<Config>({"run", "a,b", "--verbose", "c"});

// rfl::cli::usage<Config>("app"):
// Usage: app <command> <files>... [--verbose[=<bool>]]

Key details

  • Each remaining positional argument is split at commas and appended in order; plain lists require ≥1 argument, while std::optional / rfl::DefaultVal allow 0.
  • Long-option spelling (--files=a --files=b) still works, but mixing --files=... with bare positionals returns a conflict error.
  • Earlier positional list fields still consume a single argument; public resolve_args<T, Ps> keeps its one-per-field flat-map behavior.
  • Docs updated in cli.md ("A list as the last positional argument").

AI-authored. Depends on: #26.

…in rfl::cli

If the last positional field is a list, it takes every positional
argument after those of the other positional fields, each split at the
commas: `app run a b,c` gives files = [a, b, c]. A list before other
positional fields still takes one argument, and so does every positional
field in the public resolve_args<T>, whose flat-map result cannot hold
them.

usage shows a list as the last positional as `<files>...`.

Only inputs that used to fail change meaning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@clockblocker

Copy link
Copy Markdown
Owner Author

Branch is preserved on fork and showcase/all-lanes (d0f1fca1) as a reference artifact and parked in #117 (Moonshots & Possible Scope Creep).

clockblocker added a commit that referenced this pull request Nov 30, 2026
…te O(N*D) read deep copies

Previously, rfl::generic::Reader used by-value Generic, Generic::Array, and
Generic::Object as its InputVarType, InputArrayType, and InputObjectType.
As a result, every traversal step (to_object, to_array, get_field_from_object,
get_field_from_array, read_object, read_array) deep-copied entire nested
containers and subtrees, causing O(N*D) (O(N^2) on deep trees) time and
allocation complexity during rfl::from_generic / rfl::generic::read.

Changes:
- Switched rfl::generic::Reader::InputVarType, InputArrayType, and
  InputObjectType to non-owning const pointers (const Generic*,
  const Generic::Array*, const Generic::Object*) using std::get_if with
  explicit nullptr guards returning rfl::Error across all Reader methods.
- Avoided mutating rfl::Object's internal cursor in get_field_from_object
  by scanning the const object directly and returning &v.
- Updated rfl::generic::read to pass &_g to Parser::read.
- Adapted rfl::parsing::Parser<generic::Reader, generic::Writer, json::Raw>
  in include/rfl/json/Raw.hpp to handle pointer InputVarType safely (returning
  an error on nullptr).
- Added unit tests in tests/generic/test_generic_zero_copy.cpp covering
  generic::Reader zero-copy pointer identity, nullptr safety across all Reader
  methods, deep-80 from_generic<Generic> / from_generic<DeepNode> round-trips,
  and rfl::json::Raw generic::Reader integration + nullptr handling.

Maintainer review notes:
1. Public API change (requires explicit maintainer sign-off per Rule #3):
   rfl::generic::Reader::{InputVarType, InputArrayType, InputObjectType} change
   from value types (Generic, Generic::Array, Generic::Object) to non-owning
   const pointers (const Generic*, const Generic::Array*, const
   Generic::Object*), matching the non-owning pointer pattern already used by
   rfl::toml::Reader (raw pointers) and rfl::json::Reader (pointer handles).
   External callers that only use rfl::from_generic / rfl::generic::read are
   unaffected, whereas custom Parser<generic::Reader, ...> specializations
   consuming generic::Reader::InputVarType directly must dereference and
   null-check the pointer.
2. PR dependency note (PR #191 / #36):
   The 5-line adaptation in include/rfl/json/Raw.hpp depends on rfl::json::Raw
   introduced by PR #191 (#36). If this PR is upstreamed ahead of #36, the
   include/rfl/json/Raw.hpp hunk and test_json_raw_with_generic_reader test are
   cleanly separable and can be dropped without affecting any other file.

Empirical verification (Clang 23 -O2, stacked on perf/algo-c2a, D=50/100/200):
- D=200 from_generic<DeepNode>: 10850 us / 61,706 allocs ->  42.0 us /   602 allocs (258x faster, 102x fewer allocs)
- D=200 from_generic<Generic>:   2959 us / 22,305 allocs -> 186.7 us / 2,005 allocs (15.8x faster, 11x fewer allocs)
- Linear allocation scaling confirmed at D=50/100/200:
  from_generic<DeepNode> (152/302/602 vs 4181/15856/61706),
  from_generic<Generic>  (505/1005/2005 vs 1830/6155/22305).

Verified with standalone header self-containment checks (including
xml/Parser.hpp against pugixml) and full CTest suite (JSON, YAML, XML,
Generic; 832/832 passed, 1 skipped locale test, 0 warnings) on both
Clang 23.1.6 and GCC 13.4.0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant