args-parser 6.3.6
Loading...
Searching...
No Matches
multi_arg.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__MULTI_ARG_HPP__INCLUDED
8#define ARGS__MULTI_ARG_HPP__INCLUDED
9
10// Args include.
11#include "arg.hpp"
12#include "context.hpp"
13#include "exceptions.hpp"
14#include "types.hpp"
15#include "utils.hpp"
16#include "value_utils.hpp"
17
18// C++ include.
19#include <utility>
20
21namespace Args
22{
23
24//
25// MultiArg
26//
27
33class MultiArg : public Arg
34{
35public:
37 template<typename T>
40 Char flag,
42 T &&name,
44 bool isWithValue = false,
46 bool isRequired = false);
47
49 explicit MultiArg(
51 Char flag,
53 bool isWithValue = false,
55 bool isRequired = false);
56
58 template<typename T>
59 explicit MultiArg(
61 T &&name,
63 bool isWithValue = false,
65 bool isRequired = false);
66
67 virtual ~MultiArg();
68
70 ArgType type() const override
71 {
72 return ArgType::MultiArg;
73 }
74
76 const String &value() const override;
78 MultiArg &setValue(const String &v) override;
79
81 virtual const StringList &values() const;
82
88 size_t count() const;
89
91 const String &defaultValue() const override
92 {
93 if (!m_defaultValues.empty()) {
94 return m_defaultValues.front();
95 } else {
97 }
98 }
99
102 MultiArg &setDefaultValue(const String &v) override
103 {
104 m_defaultValues.push_back(v);
105 return *this;
106 }
107
110 {
111 return m_defaultValues;
112 }
113
116 {
117 m_defaultValues = v;
118 return *this;
119 }
120
122 void clear() override
123 {
124 setDefined(false);
125
126 m_values.clear();
127 }
128
129protected:
135 void process(
137 Context &context) override;
138
139private:
141
142
143 StringList m_values;
145 size_t m_count;
147 StringList m_defaultValues;
148}; // class MultiArg
149
150//
151// MultiArg
152//
153
154template<typename T>
156 T &&name,
157 bool isWithValue,
158 bool isRequired)
159 : Arg(flag,
160 std::forward<T>(name),
163 , m_count(0)
164{
165}
166
168 bool isWithValue,
169 bool isRequired)
170 : Arg(flag,
173 , m_count(0)
174{
175}
176
177template<typename T>
179 bool isWithValue,
180 bool isRequired)
181 : Arg(std::forward<T>(name),
184 , m_count(0)
185{
186}
187
189{
190}
191
192inline const String &MultiArg::value() const
193{
194 if (!m_values.empty()) {
195 return m_values.front();
196 } else if (!m_defaultValues.empty()) {
197 return m_defaultValues.front();
198 } else {
200 }
201}
202
204{
205 m_values.push_back(v);
206 return *this;
207}
208
209inline const StringList &MultiArg::values() const
210{
211 if (!m_values.empty()) {
212 return m_values;
213 } else {
214 return m_defaultValues;
215 }
216}
217
218inline size_t MultiArg::count() const
219{
220 if (!isWithValue()) {
221 return m_count;
222 } else if (!m_values.empty()) {
223 return m_values.size();
224 } else {
225 return m_defaultValues.size();
226 }
227}
228
229inline void MultiArg::process(Context &context)
230{
231 if (isWithValue()) {
232 setDefined(eatValues(context,
233 m_values,
234 String(SL("Argument \"")) + name() + SL("\" requires value that wasn't presented."),
235 cmdLine()));
236 } else {
237 setDefined(true);
238
239 ++m_count;
240 }
241}
242
243} /* namespace Args */
244
245#endif // ARGS__MULTI_ARG_HPP__INCLUDED
const String & flag() const override
Definition arg.hpp:416
String name() const override
Definition arg.hpp:304
bool isRequired() const override
Definition arg.hpp:379
Arg & setDefined(bool on=true)
Set defined.
Definition arg.hpp:395
bool isWithValue() const override
Definition arg.hpp:368
Arg(Char flag, T &&name, bool isWithValue=false, bool isRequired=false)
Construct argument with flag and name.
Definition arg.hpp:245
CmdLine * cmdLine() const
Definition arg_iface.hpp:86
Context is a list of words in the command line that user presented with interface for interacting wit...
Definition context.hpp:36
MultiArg is a class that presents argument in the command line taht can be presented more than once o...
Definition multi_arg.hpp:34
const String & defaultValue() const override
Definition multi_arg.hpp:91
ArgType type() const override
Definition multi_arg.hpp:70
const String & value() const override
MultiArg & setValue(const String &v) override
Set value.
void clear() override
Clear state of the argument.
MultiArg & setDefaultValue(const String &v) override
Set default value.
virtual const StringList & values() const
virtual ~MultiArg()
MultiArg & setDefaultValues(const StringList &v)
Set default values.
size_t count() const
void process(Context &context) override
Process argument's staff, for example take values from context.
const StringList & defaultValues() const
MultiArg(Char flag, T &&name, bool isWithValue=false, bool isRequired=false)
Construct argument with flag and name.
Definition api.hpp:18
std::string String
String type.
Definition types.hpp:314
ArgType
Type of the argument.
Definition enums.hpp:32
@ MultiArg
Multi argument.
Definition enums.hpp:38
String::value_type Char
Char type.
Definition types.hpp:317
bool eatValues(Ctx &context, Container &container, const String &errorDescription, Cmd *cmdLine)
Eat values in context.
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