-
Notifications
You must be signed in to change notification settings - Fork 0
YT-CPPAP-45: Add argument groups support #94
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
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4f0e520
initial design
SpectraL519 54845a1
aligned help printing to use groups
SpectraL519 2876dcf
argument group state verification logic
SpectraL519 49d18e7
error fix
SpectraL519 004082a
added tests
SpectraL519 d86a595
added doc comments
SpectraL519 36d35f3
docs update; group printing improvement
SpectraL519 2864f2f
tests alignment; demo update
SpectraL519 e2b38df
resolved comments
SpectraL519 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
Submodule cpp-ap-demo
updated
from 1c50d9 to 8f11f6
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
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,108 @@ | ||
| // Copyright (c) 2023-2025 Jakub Musiał | ||
| // This file is part of the CPP-AP project (https://github.com/SpectraL519/cpp-ap). | ||
| // Licensed under the MIT License. See the LICENSE file in the project root for full license information. | ||
|
|
||
| /// @file ap/argument_group.hpp | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "detail/argument_base.hpp" | ||
|
|
||
| #include <memory> | ||
|
|
||
| namespace ap { | ||
|
|
||
| /** | ||
| * @brief Represents a group of arguments. | ||
| * | ||
| * Groups allow arguments to be organized under a dedicated section in the parser's help message. | ||
| * | ||
| * A group can be marked as: | ||
| * - required: **at least one** argument from the group must be used in the command-line | ||
| * - mutually exclusive: **at most one** argument from the group can be used in the command-line | ||
| * | ||
| * @note - This class is not intended to be constructed directly, but rather through the `add_group` method of @ref ap::argument_parser. | ||
| * @note - User defined groups may contain only optional arguments (and flags). | ||
| * | ||
| * Example usage: | ||
| * @code{.cpp} | ||
| * ap::argument_parser parser("myprog"); | ||
| * auto& out_opts = parser.add_group("Output Options").mutually_exclusive(); | ||
| * | ||
| * group.add_optional_argument(out_opts, "output", "o") | ||
| * .nargs(1) | ||
| * .help("Print output to the given file"); | ||
| * | ||
| * group.add_optional_argument<ap::none_type>(out_opts, "print", "p") | ||
| * .help("Print output to the console"); | ||
| * @endcode | ||
| * Here `out_opts` is a mutually exclusive group, so using both arguments at the same time would cause an error. | ||
| */ | ||
| class argument_group { | ||
| public: | ||
| argument_group() = delete; | ||
|
|
||
| /** | ||
| * @brief Set the `required` attribute of the group. | ||
| * | ||
| * - If set to true, the parser will require at least one argument from the group to be used in the command-line. | ||
| * - If no arguments from the group are used, an exception will be thrown. | ||
| * - Argument groups are NOT required by default. | ||
| * | ||
| * @param r The value to set for the attribute (default: true). | ||
| * @return Reference to the group instance. | ||
| */ | ||
| argument_group& required(const bool r = true) noexcept { | ||
| this->_required = r; | ||
| return *this; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Set the `mutually_exclusive` attribute of the group. | ||
| * | ||
| * - If set to true, the parser will allow at most one argument from the group to be used in the command-line. | ||
| * - If more than one argument from the group is used, an exception will be thrown. | ||
| * - Argument groups are NOT mutually exclusive by default. | ||
| * | ||
| * @param me The value to set for the attribute (default: true). | ||
| * @return Reference to the group instance. | ||
| */ | ||
| argument_group& mutually_exclusive(const bool me = true) noexcept { | ||
| this->_mutually_exclusive = me; | ||
| return *this; | ||
| } | ||
|
|
||
| friend class argument_parser; | ||
|
|
||
| private: | ||
| using arg_ptr_t = std::shared_ptr<detail::argument_base>; ///< The argument pointer type alias. | ||
| using arg_ptr_vec_t = std::vector<arg_ptr_t>; ///< The argument pointer list type alias. | ||
|
|
||
| /** | ||
| * @brief Factory method to create an argument group. | ||
| * @param parser The owning parser. | ||
| * @param name Name of the group. | ||
| */ | ||
| static std::unique_ptr<argument_group> create(argument_parser& parser, std::string_view name) { | ||
| return std::unique_ptr<argument_group>(new argument_group(parser, name)); | ||
| } | ||
|
|
||
| /// Construct a new argument group with the given name. | ||
| argument_group(argument_parser& parser, const std::string_view name) | ||
| : _parser(&parser), _name(name) {} | ||
|
|
||
| /// Add a new argument to this group (called internally by parser). | ||
| void _add_argument(arg_ptr_t arg) noexcept { | ||
| this->_arguments.emplace_back(std::move(arg)); | ||
| } | ||
|
|
||
| argument_parser* _parser; ///< Pointer to the owning parser. | ||
| std::string _name; ///< Name of the group (used in help output). | ||
| arg_ptr_vec_t _arguments; ///< A list of arguments that belong to this group. | ||
|
|
||
| bool _required : 1 = false; ///< The required attribute value (default: false). | ||
| bool _mutually_exclusive : 1 = | ||
| false; ///< The mutually exclusive attribute value (default: false). | ||
| }; | ||
|
|
||
| } // namespace ap |
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.