args-parser 6.3.3
Loading...
Searching...
No Matches
multi_arg.hpp
Go to the documentation of this file.
1
31#ifndef ARGS__MULTI_ARG_HPP__INCLUDED
32#define ARGS__MULTI_ARG_HPP__INCLUDED
33
34// Args include.
35#include "arg.hpp"
36#include "context.hpp"
37#include "exceptions.hpp"
38#include "value_utils.hpp"
39#include "utils.hpp"
40#include "types.hpp"
41
42// C++ include.
43#include <utility>
44
45
46namespace Args {
47
48//
49// MultiArg
50//
51
58 : public Arg
59{
60public:
62 template< typename T >
65 Char flag,
67 T && name,
69 bool isWithValue = false,
71 bool isRequired = false );
72
74 explicit MultiArg(
76 Char flag,
78 bool isWithValue = false,
80 bool isRequired = false );
81
83 template< typename T >
84 explicit MultiArg(
86 T && name,
88 bool isWithValue = false,
90 bool isRequired = false );
91
92 virtual ~MultiArg();
93
95 ArgType type() const override
96 {
97 return ArgType::MultiArg;
98 }
99
101 const String & value() const override;
103 void setValue( const String & v ) override;
104
106 virtual const StringList & values() const;
107
113 size_t count() const;
114
116 const String & defaultValue() const override
117 {
118 if( !m_defaultValues.empty() )
119 return m_defaultValues.front();
120 else
122 }
123
126 void setDefaultValue( const String & v ) override
127 {
128 m_defaultValues.push_back( v );
129 }
130
132 const StringList & defaultValues() const
133 {
134 return m_defaultValues;
135 }
136
138 void setDefaultValues( const StringList & v )
139 {
140 m_defaultValues = v;
141 }
142
144 void clear() override
145 {
146 setDefined( false );
147
148 m_values.clear();
149 }
150
151protected:
157 void process(
159 Context & context ) override;
160
161private:
163
164
165 StringList m_values;
167 size_t m_count;
169 StringList m_defaultValues;
170}; // class MultiArg
171
172
173//
174// MultiArg
175//
176
177template< typename T >
178MultiArg::MultiArg( Char flag, T && name,
179 bool isWithValue, bool isRequired )
180 : Arg( flag, std::forward< T > ( name ), isWithValue, isRequired )
181 , m_count( 0 )
182{
183}
184
185inline
187 bool isWithValue, bool isRequired )
188 : Arg( flag, isWithValue, isRequired )
189 , m_count( 0 )
190{
191}
192
193template< typename T >
195 bool isWithValue, bool isRequired )
196 : Arg( std::forward< T > ( name ), isWithValue, isRequired )
197 , m_count( 0 )
198{
199}
200
201inline
205
206inline const String &
208{
209 if( !m_values.empty() )
210 return m_values.front();
211 else if( !m_defaultValues.empty() )
212 return m_defaultValues.front();
213 else
215}
216
217inline void
219{
220 m_values.push_back( v );
221}
222
223inline const StringList &
225{
226 if( !m_values.empty() )
227 return m_values;
228 else
229 return m_defaultValues;
230}
231
232inline size_t
234{
235 if( !isWithValue() )
236 return m_count;
237 else if( !m_values.empty() )
238 return m_values.size();
239 else
240 return m_defaultValues.size();
241}
242
243inline void
245{
246 if( isWithValue() )
247 {
248 setDefined( eatValues( context, m_values,
249 String( SL( "Argument \"" ) ) +
250 name() + SL( "\" requires value that wasn't presented." ),
251 cmdLine() ) );
252 }
253 else
254 {
255 setDefined( true );
256
257 ++m_count;
258 }
259}
260
261} /* namespace Args */
262
263#endif // ARGS__MULTI_ARG_HPP__INCLUDED
264
Argument with one value that can be present only once in the command line.
Definition arg.hpp:58
const String & flag() const override
Definition arg.hpp:448
String name() const override
Definition arg.hpp:333
bool isRequired() const override
Definition arg.hpp:409
bool isWithValue() const override
Definition arg.hpp:397
void setDefined(bool on=true)
Set defined.
Definition arg.hpp:427
CmdLine * cmdLine() const
Context is a list of words in the command line that user presented with interface for interacting wit...
Definition context.hpp:60
MultiArg is a class that presents argument in the command line taht can be presented more than once o...
Definition multi_arg.hpp:59
void setDefaultValue(const String &v) override
const String & defaultValue() const override
ArgType type() const override
Definition multi_arg.hpp:95
const String & value() const override
void setValue(const String &v) override
Set value.
void setDefaultValues(const StringList &v)
Set default values.
void clear() override
Clear state of the argument.
virtual const StringList & values() const
virtual ~MultiArg()
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: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
@ MultiArg
Multi argument.
bool eatValues(Ctx &context, Container &container, const String &errorDescription, Cmd *cmdLine)
Eat values in context.
String::value_type Char
Char type.
Definition types.hpp:327
#define SL(str)
Definition types.hpp:338
#define DISABLE_COPY(Class)
Macro for disabling copy.
Definition utils.hpp:50