ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
argument_group.h
1/*
2 * Copyright 2025 Dominik Czekai
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18#include <memory>
19#include <type_traits>
20#include <utility>
21#include <vector>
22
23#include "argument.h"
24
25namespace cli::commands
26{
27
30{
31public:
37 template <typename... Args>
38 requires((!std::same_as<std::remove_cvref_t<Args>, ArgumentGroup> && ...) &&
39 (std::derived_from<std::remove_cvref_t<Args>, ArgumentBase> && ...))
40 explicit ArgumentGroup(bool exclusive, bool inclusive, Args &&...args)
41 : exclusive(exclusive), inclusive(inclusive)
42 {
43 (arguments.emplace_back(
44 std::make_shared<std::remove_cvref_t<Args>>(std::forward<Args>(args))),
45 ...);
46 }
47
48 virtual ~ArgumentGroup() = default;
49
53 [[nodiscard]] const std::vector<std::shared_ptr<ArgumentBase>> &getArguments() const noexcept
54 {
55 return arguments;
56 }
57
62 [[nodiscard]] bool isExclusive() const noexcept { return exclusive; }
63
68 [[nodiscard]] bool isInclusive() const noexcept { return inclusive; }
69
74 bool isRequired() const;
75
78 void addArgument(const std::shared_ptr<ArgumentBase> &arg) { arguments.push_back(arg); }
79
80private:
81 std::vector<std::shared_ptr<ArgumentBase>> arguments;
82 bool exclusive;
83 bool inclusive;
84};
85
88{
89public:
93 template <typename... Args>
94 requires((!std::same_as<std::remove_cvref_t<Args>, ExclusiveGroup> && ...) &&
95 (std::derived_from<std::remove_cvref_t<Args>, ArgumentBase> && ...))
96 explicit ExclusiveGroup(Args &&...args)
97 : ArgumentGroup(true, false, std::forward<Args>(args)...)
98 {
99 }
100};
101
104{
105public:
109 template <typename... Args>
110 requires((!std::same_as<std::remove_cvref_t<Args>, InclusiveGroup> && ...) &&
111 (std::derived_from<std::remove_cvref_t<Args>, ArgumentBase> && ...))
112 explicit InclusiveGroup(Args &&...args)
113 : ArgumentGroup(false, true, std::forward<Args>(args)...)
114 {
115 }
116};
117} // namespace cli::commands
Base class for command-line arguments.
Definition argument.h:41
Base class for command-line argument groups.
Definition argument_group.h:30
const std::vector< std::shared_ptr< ArgumentBase > > & getArguments() const noexcept
Get the arguments in the group.
Definition argument_group.h:53
bool isInclusive() const noexcept
Check if the group is inclusive.
Definition argument_group.h:68
bool isRequired() const
Check if this group is required.
Definition argument_group.cpp:23
void addArgument(const std::shared_ptr< ArgumentBase > &arg)
Add an argument to the group.
Definition argument_group.h:78
bool isExclusive() const noexcept
Check if the group is exclusive.
Definition argument_group.h:62
ArgumentGroup(bool exclusive, bool inclusive, Args &&...args)
Construct a new ArgumentGroup.
Definition argument_group.h:40
Exclusive argument group.
Definition argument_group.h:88
ExclusiveGroup(Args &&...args)
Construct a new ExclusiveGroup.
Definition argument_group.h:96
Inclusive argument group.
Definition argument_group.h:104
InclusiveGroup(Args &&...args)
Construct a new InclusiveGroup.
Definition argument_group.h:112