args-parser 6.3.6
Loading...
Searching...
No Matches
group_iface.hpp
Go to the documentation of this file.
1
2/*
3 SPDX-FileCopyrightText: 2026 Igor Mironchik <igor.mironchik@gmail.com>
4 SPDX-License-Identifier: MIT
5*/
6
7#ifndef ARGS__GROUP_IFACE_HPP__INCLUDED
8#define ARGS__GROUP_IFACE_HPP__INCLUDED
9
10// Args include.
11#include "arg_iface.hpp"
12#include "exceptions.hpp"
13#include "types.hpp"
14#include "utils.hpp"
15
16// C++ include.
17#include <algorithm>
18#include <memory>
19#include <type_traits>
20#include <utility>
21#include <vector>
22
23namespace Args
24{
25
26class Command;
27
28//
29// GroupIface
30//
31
33class GroupIface : public ArgIface
34{
35public:
37 using ArgPtr = std::unique_ptr<ArgIface, details::Deleter<ArgIface>>;
39 using Arguments = std::vector<ArgPtr>;
40
41 template<typename T>
42 explicit GroupIface(T &&name,
43 bool required = false)
44 : m_name(std::forward<T>(name))
45 , m_required(required)
46 {
47 }
48
49 virtual ~GroupIface()
50 {
51 }
52
54 const Arguments &children() const
55 {
56 return m_children;
57 }
58
61 {
62 return addArg(ArgPtr(&arg, details::Deleter<ArgIface>(false)));
63 }
64
67 {
68 return addArg(ArgPtr(arg, details::Deleter<ArgIface>(false)));
69 }
70
72 virtual GroupIface &addArg(ArgPtr arg)
73 {
74 if (std::find(m_children.cbegin(), m_children.cend(), arg) == m_children.cend()) {
75 if (cmdLine()) {
76 arg->setCmdLine(cmdLine());
77 }
78
79 m_children.push_back(std::move(arg));
80 }
81 return *this;
82 }
83
90 String name() const override
91 {
92 return m_name;
93 }
94
96 bool isWithValue() const override
97 {
98 return false;
99 }
100
102 bool isRequired() const override
103 {
104 return m_required;
105 }
106
108 virtual GroupIface &setRequired(bool on = true)
109 {
110 m_required = on;
111 return *this;
112 }
113
115 const String &flag() const override
116 {
118 }
119
121 const String &argumentName() const override
122 {
124 }
125
127 const String &valueSpecifier() const override
128 {
130 }
131
133 const String &description() const override
134 {
136 }
137
139 const String &longDescription() const override
140 {
142 }
143
145 void clear() override
146 {
147 std::for_each(children().begin(), children().end(), [&](const auto &ch) {
148 ch->clear();
149 });
150 }
151
152protected:
168 const String &name) override
169 {
170 for (const auto &arg : details::asConst(m_children)) {
171 ArgIface *tmp = arg->findArgument(name);
172
173 if (tmp != nullptr) {
174 return tmp;
175 }
176 }
177
178 return nullptr;
179 }
180
188 Context &) override
189 {
190 }
191
200 StringList &flags,
202 StringList &names) const override
203 {
204 for (const auto &arg : details::asConst(m_children)) {
205 arg->checkCorrectnessBeforeParsing(flags, names);
206 }
207 }
208
210 void checkCorrectnessAfterParsing() const override
211 {
212 for (const auto &arg : details::asConst(m_children)) {
213 arg->checkCorrectnessAfterParsing();
214 }
215
216 if (isRequired() && !isDefined()) {
217 throw BaseException(String(SL("Not defined required argument \"")) + name() + SL("\""));
218 }
219 }
220
222 void setCmdLine(CmdLine *cmdLine) override
223 {
225
226 for (const auto &arg : details::asConst(m_children)) {
227 arg->setCmdLine(cmdLine);
228 }
229 }
230
234 const String &name,
236 StringList &possibleNames) const override
237 {
238 bool ret = false;
239
240 std::for_each(children().cbegin(), children().cend(), [&](const auto &ch) {
241 if (ch->isMisspelledName(name, possibleNames))
242 ret = true;
243 });
244
245 return ret;
246 }
247
248protected:
251
252private:
254
255
256 String m_name;
258 bool m_required;
259}; // class GroupIface
260
261} /* namespace Args */
262
263#endif // ARGS__GROUP_IFACE_HPP__INCLUDED
friend class CmdLine
Definition arg_iface.hpp:30
virtual void setCmdLine(CmdLine *cmdLine)
Set command line parser.
CmdLine * cmdLine() const
Definition arg_iface.hpp:86
virtual bool isDefined() const =0
Base exception of the library.
Command in the command line interface.
Definition command.hpp:28
Context is a list of words in the command line that user presented with interface for interacting wit...
Definition context.hpp:36
virtual GroupIface & setRequired(bool on=true)
Set required flag.
const String & description() const override
void setCmdLine(CmdLine *cmdLine) override
Set command line parser.
virtual GroupIface & addArg(ArgPtr arg)
Add argument.
GroupIface & addArg(ArgIface *arg)
Add argument.
String name() const override
bool isRequired() const override
const String & flag() const override
const String & valueSpecifier() const override
GroupIface & addArg(ArgIface &arg)
Add argument.
bool isWithValue() const override
void process(Context &) override
Process argument's staff, for example take values from context.
void clear() override
Clear state of the argument.
Arguments m_children
List of children.
const Arguments & children() const
bool isMisspelledName(const String &name, StringList &possibleNames) const override
const String & argumentName() const override
std::unique_ptr< ArgIface, details::Deleter< ArgIface > > ArgPtr
Smart pointer to the argument.
std::vector< ArgPtr > Arguments
List of child arguments.
const String & longDescription() const override
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
GroupIface(T &&name, bool required=false)
ArgIface * findArgument(const String &name) override
void checkCorrectnessAfterParsing() const override
Check correctness of the argument after parsing.
virtual ~GroupIface()
constexpr std::add_const< T >::type & asConst(T &t) noexcept
Adds const to non-const objects.
Definition utils.hpp:45
Definition api.hpp:18
std::string String
String type.
Definition types.hpp:314
std::vector< String > StringList
List of strings.
Definition types.hpp:336
static const String c_string
Definition utils.hpp:146
#define SL(str)
Definition types.hpp:328
#define DISABLE_COPY(Class)
Macro for disabling copy.
Definition utils.hpp:25