args-parser 6.3.3
Loading...
Searching...
No Matches
groups.hpp
Go to the documentation of this file.
1
37#ifndef ARGS__GROUPS_HPP__INCLUDED
38#define ARGS__GROUPS_HPP__INCLUDED
39
40// Args include.
41#include "group_iface.hpp"
42#include "types.hpp"
43
44// C++ include.
45#include <algorithm>
46
47
48namespace Args {
49
50//
51// OnlyOneGroup
52//
53
55class OnlyOneGroup final
56 : public GroupIface
57{
58public:
59 template< typename T >
60 explicit OnlyOneGroup( T && name,
61 bool required = false )
62 : GroupIface( std::forward< T > ( name ), required )
63 {
64 }
65
66 virtual ~OnlyOneGroup()
67 {
68 }
69
71 ArgType type() const override
72 {
74 }
75
77 bool isDefined() const override
78 {
79 return std::any_of( children().cbegin(), children().cend(),
80 [] ( const auto & arg ) { return arg->isDefined(); } );
81 }
82
83protected:
92 StringList & flags,
94 StringList & names ) const override
95 {
97
98 for( const auto & arg : details::asConst( children() ) )
99 {
100 if( arg->isRequired() )
101 throw BaseException( String( SL( "Required argument \"" ) ) +
102 arg->name() +
103 SL( "\" is not allowed to be in OnlyOne group \"" ) +
104 name() + SL( "\"." ) );
105 }
106 }
107
109 void checkCorrectnessAfterParsing() const override
110 {
112
113 ArgIface * defined = nullptr;
114
115 for( const auto & arg : details::asConst( children() ) )
116 {
117 if( arg->isDefined() )
118 {
119 if( defined )
120 throw BaseException( String( SL( "Only one argument can "
121 "be defined in OnlyOne group \"" ) ) + name() + SL( "\". " ) +
122 SL( "Whereas defined \"" ) + defined->name() +
123 SL( "\" and \"" ) + arg->name() + SL( "\"." ) );
124 else
125 defined = arg.get();
126 }
127 }
128 }
129}; // class OnlyOneGroup
130
131
132//
133// AllOfGroup
134//
135
137class AllOfGroup final
138 : public GroupIface
139{
140public:
141 template< typename T >
142 explicit AllOfGroup( T && name,
143 bool required = false )
144 : GroupIface( std::forward< T > ( name ), required )
145 {
146 }
147
148 virtual ~AllOfGroup()
149 {
150 }
151
153 ArgType type() const override
154 {
155 return ArgType::AllOfGroup;
156 }
157
159 bool isDefined() const override
160 {
161 return !std::any_of( children().cbegin(), children().cend(),
162 [] ( const auto & arg ) { return !arg->isDefined(); } );
163 }
164
165protected:
174 StringList & flags,
176 StringList & names ) const override
177 {
179
180 for( const auto & arg : details::asConst( children() ) )
181 {
182 if( arg->isRequired() )
183 throw BaseException( String( SL( "Required argument \"" ) ) +
184 arg->name() +
185 SL( "\" is not allowed to " ) +
186 SL( "be in AllOf group \"" ) + name() + SL( "\"." ) );
187 }
188 }
189
191 void checkCorrectnessAfterParsing() const override
192 {
194
195 const bool defined = std::any_of( children().cbegin(), children().cend(),
196 [] ( const auto & arg ) { return arg->isDefined(); } );
197
198 const bool all = std::all_of( children().cbegin(), children().cend(),
199 [] ( const auto & arg ) { return arg->isDefined(); } );
200
201 if( defined && !all )
202 throw BaseException( String( SL( "All arguments in "
203 "AllOf group \"" ) ) + name() + SL( "\" should be defined." ) );
204 }
205}; // class AllOfGroup
206
207
208//
209// AtLeastOneGroup
210//
211
214 : public GroupIface
215{
216public:
217 template< typename T >
218 explicit AtLeastOneGroup( T && name,
219 bool required = false )
220 : GroupIface( std::forward< T > ( name ), required )
221 {
222 }
223
225 {
226 }
227
229 ArgType type() const override
230 {
232 }
233
235 bool isDefined() const override
236 {
237 return std::any_of( children().cbegin(), children().cend(),
238 [] ( const auto & arg ) { return arg->isDefined(); } );
239 }
240
241protected:
250 StringList & flags,
252 StringList & names ) const override
253 {
255
256 for( const auto & arg : details::asConst( children() ) )
257 {
258 if( arg->isRequired() )
259 throw BaseException( String( SL( "Required argument \"" ) ) +
260 arg->name() +
261 SL( "\" is not allowed to " ) +
262 SL( "be in AtLeastOne group \"" ) + name() + SL( "\"." ) );
263 }
264 }
265}; // class AtLeastOneGroup
266
267} /* namespace Args */
268
269#endif // ARGS__GROUPS_HPP__INCLUDED
Group of args where all arguments should be defined.
Definition groups.hpp:139
bool isDefined() const override
Definition groups.hpp:159
AllOfGroup(T &&name, bool required=false)
Definition groups.hpp:142
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
Definition groups.hpp:172
void checkCorrectnessAfterParsing() const override
Check correctness of the argument after parsing.
Definition groups.hpp:191
ArgType type() const override
Definition groups.hpp:153
virtual ~AllOfGroup()
Definition groups.hpp:148
Interface for arguments.
Definition arg_iface.hpp:51
virtual String name() const =0
Group of args where at least one argument should be defined.
Definition groups.hpp:215
AtLeastOneGroup(T &&name, bool required=false)
Definition groups.hpp:218
virtual ~AtLeastOneGroup()
Definition groups.hpp:224
bool isDefined() const override
Definition groups.hpp:235
ArgType type() const override
Definition groups.hpp:229
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
Definition groups.hpp:248
Base exception of the library.
Interface for the groups.
String name() const override
const Arguments & children() const
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
void checkCorrectnessAfterParsing() const override
Check correctness of the argument after parsing.
Group of args where only one argument can be defined.
Definition groups.hpp:57
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
Definition groups.hpp:90
OnlyOneGroup(T &&name, bool required=false)
Definition groups.hpp:60
bool isDefined() const override
Definition groups.hpp:77
virtual ~OnlyOneGroup()
Definition groups.hpp:66
ArgType type() const override
Definition groups.hpp:71
void checkCorrectnessAfterParsing() const override
Check correctness of the argument after parsing.
Definition groups.hpp:109
constexpr std::add_const< T >::type & asConst(T &t) noexcept
Adds const to non-const objects.
Definition utils.hpp:72
Definition api.hpp:42
std::string String
String type.
Definition types.hpp:324
std::vector< String > StringList
List of strings.
Definition types.hpp:346
ArgType
Type of the argument.
Definition enums.hpp:56
@ AllOfGroup
"All of" group.
@ AtLeastOneGroup
"At least one" group.
@ OnlyOneGroup
"Only one" group.
#define SL(str)
Definition types.hpp:338