|
| 1 | +# rfl::cli — Command-Line Argument Parser |
| 2 | + |
| 3 | +Parse `argc`/`argv` into any reflectable struct via `rfl::cli::read<T>(argc, argv)`. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```cpp |
| 8 | +#include <rfl/cli.hpp> |
| 9 | + |
| 10 | +struct Config { |
| 11 | + std::string host_name; |
| 12 | + int port; |
| 13 | + bool verbose; |
| 14 | + std::optional<double> rate; |
| 15 | + std::vector<std::string> tags; |
| 16 | +}; |
| 17 | + |
| 18 | +int main(int argc, char* argv[]) { |
| 19 | + const auto result = rfl::cli::read<Config>(argc, argv); |
| 20 | + // ./app --host-name=localhost --port=8080 --verbose --tags=a,b,c |
| 21 | +} |
| 22 | +``` |
| 23 | +
|
| 24 | +Field names undergo automatic `snake_case` -> `kebab-case` conversion: |
| 25 | +`host_name` matches `--host-name`. |
| 26 | +
|
| 27 | +## Positional arguments |
| 28 | +
|
| 29 | +Wrap a field with `rfl::Positional<T>` to accept it as a bare (non-flag) argument: |
| 30 | +
|
| 31 | +```cpp |
| 32 | +struct Config { |
| 33 | + rfl::Positional<std::string> input_file; |
| 34 | + rfl::Positional<std::string> output_file; |
| 35 | + bool verbose; |
| 36 | +}; |
| 37 | +
|
| 38 | +// ./app input.txt output.txt --verbose |
| 39 | +``` |
| 40 | + |
| 41 | +Positional arguments are matched in declaration order. They can also be |
| 42 | +passed as named arguments: `--input-file=input.txt`. |
| 43 | + |
| 44 | +The `--` separator forces all subsequent tokens into positional: |
| 45 | + |
| 46 | +``` |
| 47 | +./app --verbose -- --not-a-flag.txt |
| 48 | +``` |
| 49 | + |
| 50 | +## Short aliases |
| 51 | + |
| 52 | +Wrap a field with `rfl::Short<"x", T>` to add a single-character alias: |
| 53 | + |
| 54 | +```cpp |
| 55 | +struct Config { |
| 56 | + rfl::Short<"p", int> port; |
| 57 | + rfl::Short<"v", bool> verbose; |
| 58 | + std::string host; |
| 59 | +}; |
| 60 | + |
| 61 | +// ./app -p 8080 -v --host=localhost |
| 62 | +// ./app -p=8080 -v --host=localhost |
| 63 | +// ./app --port=8080 --verbose --host=localhost (long names still work) |
| 64 | +``` |
| 65 | +
|
| 66 | +Short bool flags do not consume the next token as a value — `-v somefile` |
| 67 | +treats `somefile` as a positional argument, not as the value of `-v`. |
| 68 | +To explicitly set a bool short flag, use `=` syntax: `-v=true`, `-v=false`. |
| 69 | +
|
| 70 | +## Combining Positional and Short |
| 71 | +
|
| 72 | +`Positional` and `Short` can be used together in the same struct, but |
| 73 | +**cannot be nested** (`Positional<Short<...>>` is a compile-time error): |
| 74 | +
|
| 75 | +```cpp |
| 76 | +struct Config { |
| 77 | + rfl::Positional<std::string> input_file; |
| 78 | + rfl::Short<"o", std::string> output_dir; |
| 79 | + rfl::Short<"v", bool> verbose; |
| 80 | + int count; |
| 81 | +}; |
| 82 | +
|
| 83 | +// ./app data.csv -o /tmp/out -v --count=10 |
| 84 | +``` |
| 85 | + |
| 86 | +## Supported types |
| 87 | + |
| 88 | +| Type | CLI format | Notes | |
| 89 | +|------|-----------|-------| |
| 90 | +| `std::string` | `--key=value` | | |
| 91 | +| `int`, `long`, ... | `--key=42` | | |
| 92 | +| `float`, `double` | `--key=1.5` | | |
| 93 | +| `bool` | `--flag` or `--flag=true` | No `=` implies `true` | |
| 94 | +| `enum` | `--key=value_name` | Via `rfl::string_to_enum` | |
| 95 | +| `std::optional<T>` | omit for `nullopt` | | |
| 96 | +| `std::vector<T>` | `--key=a,b,c` | Comma-separated; empty elements skipped | |
| 97 | +| Nested struct | `--parent.child=val` | Dot-separated path | |
| 98 | +| `rfl::Flatten<T>` | fields inlined | No prefix needed | |
| 99 | +| `rfl::Rename<"x", T>` | `--x=val` | Bypasses kebab conversion | |
| 100 | +| `rfl::Positional<T>` | bare token | Matched in declaration order | |
| 101 | +| `rfl::Short<"x", T>` | `-x value` or `-x=value` | Single-character alias | |
| 102 | + |
| 103 | +## Architecture |
| 104 | + |
| 105 | +Parsing proceeds in three stages: |
| 106 | + |
| 107 | +1. **`parse_argv`** — categorizes raw tokens into `named`, `short_args`, |
| 108 | + and `positional` buckets (`ParsedArgs` struct). No type information needed. |
| 109 | +2. **`resolve_args`** — uses compile-time metadata from the target struct to |
| 110 | + map short aliases to long names, reclaim values from bool short flags, |
| 111 | + and merge positional arguments. Produces a flat `map<string, string>`. |
| 112 | +3. **`Reader`** — implements reflect-cpp's `IsReader` concept by presenting |
| 113 | + virtual tree nodes over the flat map. Each node is a `{map*, path}` pair — |
| 114 | + no data copying, just prefix-based lookup via `lower_bound`. |
| 115 | + |
| 116 | +## Files |
| 117 | + |
| 118 | +- `include/rfl/cli/read.hpp` — public API |
| 119 | +- `include/rfl/cli/Reader.hpp` — Reader + `parse_value` overloads |
| 120 | +- `include/rfl/cli/Parser.hpp` — Parser type alias |
| 121 | +- `include/rfl/cli/parse_argv.hpp` — `argv` -> `ParsedArgs` |
| 122 | +- `include/rfl/cli/resolve_args.hpp` — `ParsedArgs` -> `map<string, string>` |
| 123 | +- `include/rfl/cli.hpp` — aggregator header |
| 124 | +- `include/rfl/SnakeCaseToKebabCase.hpp` — processor |
| 125 | +- `include/rfl/Positional.hpp` — `Positional<T>` wrapper |
| 126 | +- `include/rfl/Short.hpp` — `Short<"x", T>` wrapper |
0 commit comments