args-parser 6.3.3
Loading...
Searching...
No Matches
group_iface.hpp
Go to the documentation of this file.
1
31#ifndef ARGS__GROUP_IFACE_HPP__INCLUDED
32#define ARGS__GROUP_IFACE_HPP__INCLUDED
33
34// Args include.
35#include "arg_iface.hpp"
36#include "utils.hpp"
37#include "exceptions.hpp"
38#include "types.hpp"
39#include "utils.hpp"
40
41// C++ include.
42#include <vector>
43#include <type_traits>
44#include <algorithm>
45#include <utility>
46#include <memory>
47
48
49namespace Args {
50
51class Command;
52
53
54//
55// GroupIface
56//
57
60 : public ArgIface
61{
62public:
64 using ArgPtr = std::unique_ptr< ArgIface, details::Deleter< ArgIface > >;
66 using Arguments = std::vector< ArgPtr >;
67
68 template< typename T >
69 explicit GroupIface( T && name,
70 bool required = false )
71 : m_name( std::forward< T > ( name ) )
72 , m_required( required )
73 {
74 }
75
76 virtual ~GroupIface()
77 {
78 }
79
81 const Arguments & children() const
82 {
83 return m_children;
84 }
85
87 void addArg( ArgIface & arg )
88 {
89 addArg( ArgPtr( &arg, details::Deleter< ArgIface > ( false ) ) );
90 }
91
93 void addArg( ArgIface * arg )
94 {
95 addArg( ArgPtr( arg, details::Deleter< ArgIface > ( false ) ) );
96 }
97
99 virtual void addArg( ArgPtr arg )
100 {
101 if( std::find( m_children.cbegin(), m_children.cend(), arg ) ==
102 m_children.cend() )
103 {
104 if( cmdLine() )
105 arg->setCmdLine( cmdLine() );
106
107 m_children.push_back( std::move( arg ) );
108 }
109 }
110
117 String name() const override
118 {
119 return m_name;
120 }
121
123 bool isWithValue() const override
124 {
125 return false;
126 }
127
129 bool isRequired() const override
130 {
131 return m_required;
132 }
133
135 virtual void setRequired( bool on = true )
136 {
137 m_required = on;
138 }
139
141 const String & flag() const override
142 {
144 }
145
147 const String & argumentName() const override
148 {
150 }
151
153 const String & valueSpecifier() const override
154 {
156 }
157
159 const String & description() const override
160 {
162 }
163
165 const String & longDescription() const override
166 {
168 }
169
171 void clear() override
172 {
173 std::for_each( children().begin(), children().end(),
174 [ & ] ( const auto & ch ) { ch->clear(); } );
175 }
176
177protected:
193 const String & name ) override
194 {
195 for( const auto & arg : details::asConst( m_children ) )
196 {
197 ArgIface * tmp = arg->findArgument( name );
198
199 if( tmp != nullptr )
200 return tmp;
201 }
202
203 return nullptr;
204 }
205
213 Context & ) override
214 {
215 }
216
225 StringList & flags,
227 StringList & names ) const override
228 {
229 for( const auto & arg : details::asConst( m_children ) )
230 arg->checkCorrectnessBeforeParsing( flags, names );
231 }
232
234 void checkCorrectnessAfterParsing() const override
235 {
236 for( const auto & arg : details::asConst( m_children ) )
237 arg->checkCorrectnessAfterParsing();
238
239 if( isRequired() && !isDefined() )
240 throw BaseException(
241 String( SL( "Not defined required argument \"" ) ) +
242 name() + SL( "\"" ) );
243 }
244
246 void setCmdLine( CmdLine * cmdLine ) override
247 {
249
250 for( const auto & arg : details::asConst( m_children ) )
251 arg->setCmdLine( cmdLine );
252 }
253
257 const String & name,
259 StringList & possibleNames ) const override
260 {
261 bool ret = false;
262
263 std::for_each( children().cbegin(), children().cend(),
264 [ & ] ( const auto & ch )
265 {
266 if( ch->isMisspelledName( name, possibleNames ) )
267 ret = true;
268 } );
269
270 return ret;
271 }
272
273protected:
276
277private:
279
280
281 String m_name;
283 bool m_required;
284}; // class GroupIface
285
286} /* namespace Args */
287
288#endif // ARGS__GROUP_IFACE_HPP__INCLUDED
Interface for arguments.
Definition arg_iface.hpp:51
virtual ArgIface * findArgument(const String &name)=0
virtual void setCmdLine(CmdLine *cmdLine)
Set command line parser.
CmdLine * cmdLine() const
virtual bool isDefined() const =0
Base exception of the library.
CmdLine is class that holds all rguments and parse command line arguments in the correspondence with ...
Definition cmd_line.hpp:123
Context is a list of words in the command line that user presented with interface for interacting wit...
Definition context.hpp:60
Interface for the groups.
const String & description() const override
void setCmdLine(CmdLine *cmdLine) override
Set command line parser.
String name() const override
bool isRequired() const override
std::unique_ptr< ArgIface, details::Deleter< ArgIface > > ArgPtr
Smart pointer to the argument.
virtual void addArg(ArgPtr arg)
Add argument.
const String & flag() const override
const String & valueSpecifier() const override
virtual void setRequired(bool on=true)
Set required flag.
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
void addArg(ArgIface &arg)
Add argument.
const String & argumentName() const override
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.
void addArg(ArgIface *arg)
Add argument.
virtual ~GroupIface()
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
@ Command
Command.
#define SL(str)
Definition types.hpp:338
#define DISABLE_COPY(Class)
Macro for disabling copy.
Definition utils.hpp:50