Skip to content

Read positional arguments in command-line order in rfl::cli - #789

Open
clockblocker wants to merge 1 commit into
munich-data-engineering:mainfrom
clockblocker:cli-option-table-main
Open

clockblocker wants to merge 1 commit into
munich-data-engineering:mainfrom
clockblocker:cli-option-table-main

Conversation

@clockblocker

Copy link
Copy Markdown
Contributor

Summary

Builds an internal option table from the struct in rfl::cli::read so rfl::cli knows which short options are bool flags while parsing, fixing reversed positional arguments and wrapped bool short flags.

Example

struct Args {
  rfl::Short<"q", rfl::DefaultVal<bool>> quiet = false;
  rfl::Short<"v", std::optional<bool>> verbose;
  rfl::Short<"n", bool> dry_run;
  rfl::Short<"o", std::optional<std::string>> output;
  rfl::Positional<std::optional<std::string>> first;
  rfl::Positional<std::optional<std::string>> second;
};
Input Before (main) After
in.txt -n out.txt first="out.txt", second="in.txt" (reversed) first="in.txt", second="out.txt"
-q in.txt / -v in.txt Could not cast 'in.txt' to boolean ... quiet=true / verbose=true, first="in.txt"
-n=x x treated as positional Could not cast 'x' to boolean ...
-o / -o -n / -o -- x output="" Missing value for option '-o'. (-o= still passes "")
Root rfl::NamedTuple with input_file Field named 'input_file' not found. Reads using original NamedTuple field names

Key details

  • Adds include/rfl/cli/options.hpp (rfl::cli::internal), keeping the public parse_argv and resolve_args<T, Ps> signatures unchanged.
  • Duplicate Short names in a struct are reported before argv parse errors.
  • Long options, Short+Positional compile errors, and outermost-wrapper rules are unchanged.

AI-authored.

rfl::cli decided after parsing which short options were bool flags, so a
token a flag had taken went back to the positional arguments ahead of the
others: `x -v y` gave a=y, b=x.

rfl::cli::read now derives an internal option table from the struct
(rfl::cli::internal::make_options, options.hpp) and parses with it, so it
knows while parsing what each short option does with the next token:

- Positional arguments keep their command-line order: `x -v y` gives
  a=x, b=y.
- Short<"q", std::optional<bool>> and Short<"q", DefaultVal<bool>> are
  flags: `-q in.txt` leaves in.txt positional.
- `-o` without a value fails with "Missing value for option '-o'.", and
  `-v=x` on a bool short flag fails to cast x.
- A root rfl::NamedTuple whose field names the processors change reads
  its Short and Positional fields.
- "Duplicate short name" is reported before errors in the arguments.

The public parse_argv and resolve_args keep working without the table.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
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