-
Notifications
You must be signed in to change notification settings - Fork 164
Add CLI argument parser (rfl::cli::read) #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Centimo
wants to merge
7
commits into
getml:main
Choose a base branch
from
Centimo:feature/cli-argument-parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
857b10b
Add CLI argument parser (rfl::cli::read)
Centimo 31d31f6
Add Positional<T> and Short<_name, T> wrappers for CLI module
Centimo d918825
Fix CLI module bugs: move semantics, split(), null-safety, bool-flag …
Centimo a1af419
Update CLI docs: add Positional, Short, architecture stages
Centimo 8948485
Add JSON roundtrip tests for Positional and Short wrappers
Centimo 1a621cd
Fix GCC 12 -Warray-bounds, negative number parsing, reclaim ordering
Centimo 427b16f
Flatten parse_argv: extract add_short lambda, simplify peek-ahead
Centimo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # rfl::cli — Command-Line Argument Parser | ||
|
|
||
| Parse `argc`/`argv` into any reflectable struct via `rfl::cli::read<T>(argc, argv)`. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```cpp | ||
| #include <rfl/cli.hpp> | ||
|
|
||
| struct Config { | ||
| std::string host_name; | ||
| int port; | ||
| bool verbose; | ||
| std::optional<double> rate; | ||
| std::vector<std::string> tags; | ||
| }; | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| const auto result = rfl::cli::read<Config>(argc, argv); | ||
| // ./app --host-name=localhost --port=8080 --verbose --tags=a,b,c | ||
| } | ||
| ``` | ||
|
|
||
| Field names undergo automatic `snake_case` -> `kebab-case` conversion: | ||
| `host_name` matches `--host-name`. | ||
|
|
||
| ## Positional arguments | ||
|
|
||
| Wrap a field with `rfl::Positional<T>` to accept it as a bare (non-flag) argument: | ||
|
|
||
| ```cpp | ||
| struct Config { | ||
| rfl::Positional<std::string> input_file; | ||
| rfl::Positional<std::string> output_file; | ||
| bool verbose; | ||
| }; | ||
|
|
||
| // ./app input.txt output.txt --verbose | ||
| ``` | ||
|
|
||
| Positional arguments are matched in declaration order. They can also be | ||
| passed as named arguments: `--input-file=input.txt`. | ||
|
|
||
| The `--` separator forces all subsequent tokens into positional: | ||
|
|
||
| ``` | ||
| ./app --verbose -- --not-a-flag.txt | ||
| ``` | ||
|
|
||
| ## Short aliases | ||
|
|
||
| Wrap a field with `rfl::Short<"x", T>` to add a single-character alias: | ||
|
|
||
| ```cpp | ||
| struct Config { | ||
| rfl::Short<"p", int> port; | ||
| rfl::Short<"v", bool> verbose; | ||
| std::string host; | ||
| }; | ||
|
|
||
| // ./app -p 8080 -v --host=localhost | ||
| // ./app -p=8080 -v --host=localhost | ||
| // ./app --port=8080 --verbose --host=localhost (long names still work) | ||
| ``` | ||
|
|
||
| Short bool flags do not consume the next token as a value — `-v somefile` | ||
| treats `somefile` as a positional argument, not as the value of `-v`. | ||
| To explicitly set a bool short flag, use `=` syntax: `-v=true`, `-v=false`. | ||
|
|
||
| ## Combining Positional and Short | ||
|
|
||
| `Positional` and `Short` can be used together in the same struct, but | ||
| **cannot be nested** (`Positional<Short<...>>` is a compile-time error): | ||
|
|
||
| ```cpp | ||
| struct Config { | ||
| rfl::Positional<std::string> input_file; | ||
| rfl::Short<"o", std::string> output_dir; | ||
| rfl::Short<"v", bool> verbose; | ||
| int count; | ||
| }; | ||
|
|
||
| // ./app data.csv -o /tmp/out -v --count=10 | ||
| ``` | ||
|
|
||
| ## Supported types | ||
|
|
||
| | Type | CLI format | Notes | | ||
| |------|-----------|-------| | ||
| | `std::string` | `--key=value` | | | ||
| | `int`, `long`, ... | `--key=42` | | | ||
| | `float`, `double` | `--key=1.5` | | | ||
| | `bool` | `--flag` or `--flag=true` | No `=` implies `true` | | ||
| | `enum` | `--key=value_name` | Via `rfl::string_to_enum` | | ||
| | `std::optional<T>` | omit for `nullopt` | | | ||
| | `std::vector<T>` | `--key=a,b,c` | Comma-separated; empty elements skipped | | ||
| | Nested struct | `--parent.child=val` | Dot-separated path | | ||
| | `rfl::Flatten<T>` | fields inlined | No prefix needed | | ||
| | `rfl::Rename<"x", T>` | `--x=val` | Bypasses kebab conversion | | ||
| | `rfl::Positional<T>` | bare token | Matched in declaration order | | ||
| | `rfl::Short<"x", T>` | `-x value` or `-x=value` | Single-character alias | | ||
|
|
||
| ## Architecture | ||
|
|
||
| Parsing proceeds in three stages: | ||
|
|
||
| 1. **`parse_argv`** — categorizes raw tokens into `named`, `short_args`, | ||
| and `positional` buckets (`ParsedArgs` struct). No type information needed. | ||
| 2. **`resolve_args`** — uses compile-time metadata from the target struct to | ||
| map short aliases to long names, reclaim values from bool short flags, | ||
| and merge positional arguments. Produces a flat `map<string, string>`. | ||
| 3. **`Reader`** — implements reflect-cpp's `IsReader` concept by presenting | ||
| virtual tree nodes over the flat map. Each node is a `{map*, path}` pair — | ||
| no data copying, just prefix-based lookup via `lower_bound`. | ||
|
|
||
| ## Files | ||
|
|
||
| - `include/rfl/cli/read.hpp` — public API | ||
| - `include/rfl/cli/Reader.hpp` — Reader + `parse_value` overloads | ||
| - `include/rfl/cli/Parser.hpp` — Parser type alias | ||
| - `include/rfl/cli/parse_argv.hpp` — `argv` -> `ParsedArgs` | ||
| - `include/rfl/cli/resolve_args.hpp` — `ParsedArgs` -> `map<string, string>` | ||
| - `include/rfl/cli.hpp` — aggregator header | ||
| - `include/rfl/SnakeCaseToKebabCase.hpp` — processor | ||
| - `include/rfl/Positional.hpp` — `Positional<T>` wrapper | ||
| - `include/rfl/Short.hpp` — `Short<"x", T>` wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| #ifndef RFL_POSITIONAL_HPP_ | ||
| #define RFL_POSITIONAL_HPP_ | ||
|
|
||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| #include "default.hpp" | ||
|
|
||
| namespace rfl { | ||
|
|
||
| /// Marks a field as positional for CLI argument parsing. | ||
| /// For non-CLI formats (JSON, YAML, etc.), this is transparent. | ||
| template <class T> | ||
| struct Positional { | ||
| /// The underlying type. | ||
| using Type = T; | ||
|
|
||
| Positional() requires std::is_default_constructible_v<Type> | ||
| : value_(Type()) {} | ||
|
|
||
| Positional(const Type& _value) : value_(_value) {} | ||
|
|
||
| Positional(Type&& _value) noexcept : value_(std::move(_value)) {} | ||
|
|
||
| Positional(Positional<T>&& _field) noexcept = default; | ||
|
|
||
| Positional(const Positional<T>& _field) = default; | ||
|
|
||
| template <class U> | ||
| Positional(const Positional<U>& _field) : value_(_field.get()) {} | ||
|
|
||
| template <class U> | ||
| Positional(Positional<U>&& _field) : value_(std::move(_field.value_)) {} | ||
|
|
||
| template <class U> | ||
| requires std::is_convertible_v<U, Type> | ||
| Positional(const U& _value) : value_(_value) {} | ||
|
|
||
| template <class U> | ||
| requires std::is_convertible_v<U, Type> | ||
| Positional(U&& _value) noexcept : value_(std::forward<U>(_value)) {} | ||
|
|
||
| /// Assigns the underlying object to its default value. | ||
| template <class U = Type> | ||
| requires std::is_default_constructible_v<U> | ||
| Positional(const Default&) : value_(Type()) {} | ||
|
|
||
| ~Positional() = default; | ||
|
|
||
| /// Returns the underlying object. | ||
| const Type& get() const { return value_; } | ||
|
|
||
| /// Returns the underlying object. | ||
| Type& operator()() { return value_; } | ||
|
|
||
| /// Returns the underlying object. | ||
| const Type& operator()() const { return value_; } | ||
|
|
||
| /// Assigns the underlying object. | ||
| auto& operator=(const Type& _value) { | ||
| value_ = _value; | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object. | ||
| auto& operator=(Type&& _value) noexcept { | ||
| value_ = std::move(_value); | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object. | ||
| template <class U> | ||
| requires std::is_convertible_v<U, Type> | ||
| auto& operator=(const U& _value) { | ||
| value_ = _value; | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object to its default value. | ||
| template <class U = Type> | ||
| requires std::is_default_constructible_v<U> | ||
| auto& operator=(const Default&) { | ||
| value_ = Type(); | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object. | ||
| Positional<T>& operator=(const Positional<T>& _field) = default; | ||
|
|
||
| /// Assigns the underlying object. | ||
| Positional<T>& operator=(Positional<T>&& _field) = default; | ||
|
|
||
| /// Assigns the underlying object. | ||
| template <class U> | ||
| auto& operator=(const Positional<U>& _field) { | ||
| value_ = _field.get(); | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object. | ||
| template <class U> | ||
| auto& operator=(Positional<U>&& _field) { | ||
| value_ = std::move(_field.value_); | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object. | ||
| void set(const Type& _value) { value_ = _value; } | ||
|
|
||
| /// Assigns the underlying object. | ||
| void set(Type&& _value) { value_ = std::move(_value); } | ||
|
|
||
| /// Returns the underlying object. | ||
| Type& value() { return value_; } | ||
|
|
||
| /// Returns the underlying object. | ||
| const Type& value() const { return value_; } | ||
|
|
||
| /// The underlying value. | ||
| Type value_; | ||
| }; | ||
|
|
||
| } // namespace rfl | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.