-
Notifications
You must be signed in to change notification settings - Fork 1
ParserBuilder #37
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
Merged
Merged
ParserBuilder #37
Conversation
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
…ith IsPartOf subclass of std::disjunction. Added is_part_of_v helper variable.
…y a tuple with a lits of Parsables.
…e form of tuple types. Allows for more extensive customization later
…n a singleton API, users can easily implement it themselves.
clang-tidy review says "All clean, LGTM! 👍" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
In this development branch I have implemented a
ParserBuilder
class, as proposed in #35.The previous approach of passing all configuration options as template parameters directly to the
Parser
class led to complex, unreadable code, especially as the number of options and flags grew. By introducingParserBuilder
, the user can now build the parser step-by-step, resulting in cleaner and more maintainable code. This change simplifies parser creation without sacrificing compile-time safety and flexibility. The builder pattern not only makes the code more readable but also allows users to conditionally or incrementally add flags and options, making it more adaptable to dynamic configurations.An example use-case can be seen below:
CLArgs::Parser parser = CLArgs::ParserBuilder{} .add_program_description<"Program description">() .add_flag<VerboseFlag>() .add_option<ConfigOption>() .build();
The
ParserBuilder
uses compile-time template instantiation to incrementally assemble the parser's configuration. Eachadd_flag
,add_option
, andadd_program_description
call modifies the parser type at compile time, ensuring that type safety is maintained throughout the process. This approach avoids runtime overhead while making the parser-building process more flexible.The
Parser
type that is returned is the same as before, and the same API applies. As you can see, I have also added the notion of a "program description," which is an optional string the user can provide. If added, the generated help message will include this description.A key advantage of this new builder pattern is the ability to conditionally add flags or options based on the target environment. For example, users can add platform-specific options or flags, such as enabling specific flags only for Windows builds, as shown below:
Related Issues
Closes #35.
How Has This Been Tested?
I have adapted existing tests to build with the new parser API, and they all pass. I have also introduced new unit tests, specific for the builder. Finally, manual and visual testing has been performed throughout development.
Further considerations
Though this new builder API does make the user-side parser definition more readable, I have a growing concern with regards to the Parser class definition. Consider the simple
program()
method:The current growth of template parameters is becoming unwieldy, particularly when considering future features such as positional arguments (#8), sub-commands, and validators (#4). To manage this complexity, I propose encapsulating the configuration into a ParserConfig struct. This would store all parameters as static members, simplifying the function signatures and improving readability. While there is a risk that the complexity could simply be shifted to this struct, the benefits of a centralized configuration object could outweigh that, especially as we introduce more features.