Skip to content

Accept const argv and ranges of arguments in rfl::cli::read - #784

Merged
liuzicheng1987 merged 1 commit into
munich-data-engineering:mainfrom
clockblocker:cli-const-argv-and-ranges-main
Nov 18, 2026
Merged

liuzicheng1987 merged 1 commit into
munich-data-engineering:mainfrom
clockblocker:cli-const-argv-and-ranges-main

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

AI-authored

Why

Boost.Program_options, CLI11 and cxxopts take const char* const* argv, and Boost.Program_options and CLI11 also take a std::vector<std::string>.

#623 asks for both in rfl::cli.

Today a const char** argv needs a const_cast, and arguments from a container or a test need a fake argv.


Implements the three points of #623.

  • Const argv: read and parse_argv take const char* const argv[] and const wchar_t* const argv[], so const char** callers compile. The (int, char* argv[]) and (int, wchar_t* argv[]) overloads stay and forward to them.
  • Ranges: read<T>(args) and parse_argv(args) accept a range of narrow strings (std::vector<std::string>, std::span<char*>, const char* const[], ...), a braced list (read<T>({"--port=8080"})), or a range of wide strings (converted to UTF-8). A null pointer in a range or array of pointers is an error; in a braced list it is undefined behaviour at the call site, and the docs say so.
  • Which narrow ranges compile: the parser keeps a std::string_view to each element, so the range must be a std::forward_range of lvalue strings, pointers or string_views. std::views::istream<std::string> and views yielding temporary std::strings don't compile. Wide ranges may be single-pass.
  • No skipping: the range overloads parse every element, including the first. The argc/argv overloads still skip argv[0] and keep their error messages; the wide ones no longer convert argv[0]. The docs show the std::span(argv, argc) pitfall.
  • Like parse_argv(argc, argv), the new public parse_argv(args) reads -5 as a short option, since it doesn't know the struct; read reads it as a negative number.
const char** argv = ...;                       // before: const_cast<char**>(argv)
const auto a = rfl::cli::read<Config>(argc, argv);
const std::vector<std::string> args = {"--port=8080"};
const auto b = rfl::cli::read<Config>(args);   // first element is parsed

The four char*[]/wchar_t*[] forwarding overloads stay: without them &rfl::cli::read<T> no longer converts to (int, char**). The README shows the std::vector<std::string> overload.

Tests: tests/cli/test_const_argv_and_ranges.cpp (18 tests plus static_asserts for the rejected ranges). json, cli and generic pass (Apple clang); every tests/cli/*.cpp compiles with g++-16 -O3 -Wall -Werror. The docs example was compiled and run.

🤖 Generated with Claude Code

@clockblocker
clockblocker force-pushed the cli-const-argv-and-ranges-main branch from f111f1a to a6dc52d Compare November 16, 2026 04:55
@liuzicheng1987

liuzicheng1987 commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

@clockblocker there appear to be merge conflicts because of your other PR. Could you fix them?

@clockblocker
clockblocker force-pushed the cli-const-argv-and-ranges-main branch from a6dc52d to 287c961 Compare November 17, 2026 03:49
Implements munich-data-engineering#623.

read and parse_argv take `const char* const argv[]` and
`const wchar_t* const argv[]`; the `char*[]`/`wchar_t*[]` overloads stay
and forward to them, so `&rfl::cli::read<T>` still converts to
(int, char**). New overloads take a range of narrow or wide strings or a
braced list. They parse every element, including the first, and reject
null pointers with an error. A range of narrow strings must be a
forward range of lvalue strings, pointers or string_views, because the
parser keeps a string_view to each element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@clockblocker
clockblocker force-pushed the cli-const-argv-and-ranges-main branch from 287c961 to 158a0cc Compare November 17, 2026 17:38
@liuzicheng1987

Copy link
Copy Markdown
Collaborator

@clockblocker thanks, I am merging.

@liuzicheng1987
liuzicheng1987 merged commit a2f0e74 into munich-data-engineering:main Nov 18, 2026
176 checks passed
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.

2 participants