args-parser 6.3.3
Loading...
Searching...
No Matches
arg.hpp
Go to the documentation of this file.
1
31#ifndef ARGS__ARG_HPP__INCLUDED
32#define ARGS__ARG_HPP__INCLUDED
33
34// Args include.
35#include "arg_iface.hpp"
36#include "utils.hpp"
37#include "context.hpp"
38#include "exceptions.hpp"
39#include "value_utils.hpp"
40#include "types.hpp"
41
42// C++ include.
43#include <algorithm>
44
45
46namespace Args {
47
48//
49// Arg
50//
51
56class Arg
57 : public ArgIface
58{
59public:
61 template< typename T >
62 Arg(
64 Char flag,
66 T && name,
68 bool isWithValue = false,
70 bool isRequired = false );
71
73 explicit Arg(
75 Char flag,
77 bool isWithValue = false,
79 bool isRequired = false );
80
82 template< typename T >
83 explicit Arg(
85 T && name,
87 bool isWithValue = false,
89 bool isRequired = false );
90
91 virtual ~Arg();
92
94 ArgType type() const override
95 {
96 return ArgType::Arg;
97 }
98
105 String name() const override;
106
108 bool isWithValue() const override;
110 void setWithValue( bool on = true );
111
113 bool isRequired() const override;
115 void setRequired( bool on = true );
116
118 bool isDefined() const override;
120 void setDefined( bool on = true );
121
123 virtual const String & value() const;
125 virtual void setValue( const String & v );
126
128 const String & flag() const override;
130 void setFlag( Char f );
131
133 const String & argumentName() const override;
135 void setArgumentName( const String & name );
136
138 const String & valueSpecifier() const override;
140 void setValueSpecifier( const String & vs );
141
143 const String & description() const override;
145 void setDescription( const String & desc );
146
148 const String & longDescription() const override;
150 void setLongDescription( const String & desc );
151
153 virtual const String & defaultValue() const
154 {
155 return m_defaultValue;
156 }
157
159 virtual void setDefaultValue( const String & v )
160 {
161 m_defaultValue = v;
162 }
163
167 const String & name,
169 StringList & possibleNames ) const override
170 {
171 if( !argumentName().empty() )
172 {
173 if( details::isMisspelledName( name,
174 String( SL( "--" ) ) + argumentName() ) )
175 {
176 possibleNames.push_back( String( SL( "--" ) ) + argumentName() );
177
178 return true;
179 }
180 }
181
182 return false;
183 }
184
186 void clear() override
187 {
188 setDefined( false );
189
190 m_value.clear();
191 }
192
193protected:
207 const String & name ) override
208 {
209 if( details::isArgument( name ) && name.substr( 2 ) == m_name )
210 return this;
211 else if( details::isFlag( name ) && name.substr( 1 ) == m_flag )
212 return this;
213 else
214 return nullptr;
215 }
216
222 void process(
224 Context & context ) override;
225
234 StringList & flags,
236 StringList & names ) const override;
237
239 void checkCorrectnessAfterParsing() const override;
240
241private:
243
244
245 bool m_isWithValue;
247 bool m_isRequired;
249 bool m_isDefined;
251 String m_value;
253 String m_flag;
255 String m_name;
257 String m_valueSpecifier;
259 String m_description;
261 String m_longDescription;
263 String m_defaultValue;
264}; // class Arg
265
266
267//
268// Arg
269//
270
271template< typename T >
272Arg::Arg( Char flag, T && name,
273 bool isWithValue, bool isRequired )
274 : m_isWithValue( isWithValue )
275 , m_isRequired( isRequired )
276 , m_isDefined( false )
277 , m_flag( 1, flag )
278 , m_name( std::forward< T > ( name ) )
279 , m_valueSpecifier( SL( "arg" ) )
280{
281}
282
283inline
285 bool isWithValue, bool isRequired )
286 : m_isWithValue( isWithValue )
287 , m_isRequired( isRequired )
288 , m_isDefined( false )
289 , m_flag( 1, flag )
290 , m_valueSpecifier( SL( "arg" ) )
291{
292}
293
294template< typename T >
295Arg::Arg( T && name,
296 bool isWithValue, bool isRequired )
297 : m_isWithValue( isWithValue )
298 , m_isRequired( isRequired )
299 , m_isDefined( false )
300 , m_name( std::forward< T > ( name ) )
301 , m_valueSpecifier( SL( "arg" ) )
302{
303}
304
305inline
307{
308}
309
310inline void
312{
313 if( !isDefined() )
314 {
315 if( !isWithValue() )
316 setDefined( true );
317 else
318 {
319 setValue( eatOneValue( context,
320 String( SL( "Argument \"" ) ) + name() +
321 SL( "\" requires value that wasn't presented." ),
322 cmdLine() ) );
323
324 setDefined( true );
325 }
326 }
327 else
328 throw BaseException( String( SL( "Argument \"" ) ) +
329 name() + SL( "\" already defined." ) );
330}
331
332inline String
334{
335 if( !m_name.empty() )
336 return SL( "--" ) + m_name;
337 else
338 return SL( "-" ) + m_flag;
339}
340
341inline void
343 StringList & names ) const
344{
345 if( !m_flag.empty() )
346 {
347 if( details::isCorrectFlag( m_flag ) )
348 {
349 const String flag = String( SL( "-" ) ) + m_flag;
350
351 auto it = std::find( flags.begin(), flags.end(), flag );
352
353 if( it != flags.end() )
354 throw BaseException( String( SL( "Redefinition of argument "
355 "with flag \"" ) ) + flag + SL( "\"." ) );
356 else
357 flags.push_back( flag );
358 }
359 else
360 throw BaseException( String( SL( "Disallowed flag \"-" ) ) +
361 m_flag + SL( "\"." ) );
362 }
363
364 if( !m_name.empty() )
365 {
366 if( details::isCorrectName( m_name ) )
367 {
368 const String name = String( SL( "--" ) ) + m_name;
369
370 auto it = std::find( names.begin(), names.end(), name );
371
372 if( it != names.end() )
373 throw BaseException( String( SL( "Redefinition of argument "
374 "with name \"" ) ) + name + SL( "\"." ) );
375 else
376 names.push_back( name );
377 }
378 else
379 throw BaseException( String( SL( "Disallowed name \"--" ) ) +
380 m_name + SL( "\"." ) );
381 }
382
383 if( m_flag.empty() && m_name.empty() )
384 throw BaseException( String( SL( "Arguments with empty flag and name "
385 "are disallowed." ) ) );
386}
387
388inline void
390{
391 if( isRequired() && !isDefined() )
392 throw BaseException( String( SL( "Undefined required argument \"" ) ) +
393 name() + SL( "\"." ) );
394}
395
396inline bool
398{
399 return m_isWithValue;
400}
401
402inline void
404{
405 m_isWithValue = on;
406}
407
408inline bool
410{
411 return m_isRequired;
412}
413
414inline void
416{
417 m_isRequired = on;
418}
419
420inline bool
422{
423 return m_isDefined;
424}
425
426inline void
428{
429 m_isDefined = on;
430}
431
432inline const String &
434{
435 if( !m_value.empty() )
436 return m_value;
437 else
438 return m_defaultValue;
439}
440
441inline void
443{
444 m_value = v;
445}
446
447inline const String &
449{
450 return m_flag;
451}
452
453inline void
455{
456 m_flag = String( 1, f );
457}
458
459inline const String &
461{
462 return m_name;
463}
464
465inline void
467{
468 m_name = name;
469}
470
471inline const String &
473{
474 return m_valueSpecifier;
475}
476
477inline void
479{
480 m_valueSpecifier = vs;
481}
482
483inline const String &
485{
486 return m_description;
487}
488
489inline void
491{
492 m_description = desc;
493}
494
495inline const String &
497{
498 if( !m_longDescription.empty() )
499 return m_longDescription;
500 else
501 return m_description;
502}
503
504inline void
506{
507 m_longDescription = desc;
508}
509
510} /* namespace Args */
511
512#endif // ARGS__ARG_HPP__INCLUDED
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
virtual const String & value() const
Definition arg.hpp:433
ArgType type() const override
Definition arg.hpp:94
const String & valueSpecifier() const override
Definition arg.hpp:472
String name() const override
Definition arg.hpp:333
bool isDefined() const override
Definition arg.hpp:421
bool isRequired() const override
Definition arg.hpp:409
virtual void setValue(const String &v)
Set value.
Definition arg.hpp:442
void setValueSpecifier(const String &vs)
Set value specifier.
Definition arg.hpp:478
virtual const String & defaultValue() const
Definition arg.hpp:153
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
Definition arg.hpp:342
bool isMisspelledName(const String &name, StringList &possibleNames) const override
Definition arg.hpp:165
void setRequired(bool on=true)
Set required.
Definition arg.hpp:415
void process(Context &context) override
Process argument's staff, for example take values from context.
Definition arg.hpp:311
bool isWithValue() const override
Definition arg.hpp:397
void setDefined(bool on=true)
Set defined.
Definition arg.hpp:427
void setFlag(Char f)
Set flag.
Definition arg.hpp:454
void setArgumentName(const String &name)
Set argument name.
Definition arg.hpp:466
Arg(Char flag, T &&name, bool isWithValue=false, bool isRequired=false)
Construct argument with flag and name.
Definition arg.hpp:272
const String & argumentName() const override
Definition arg.hpp:460
void setLongDescription(const String &desc)
Set long description.
Definition arg.hpp:505
const String & longDescription() const override
Definition arg.hpp:496
void clear() override
Clear state of the argument.
Definition arg.hpp:186
void setWithValue(bool on=true)
Set is this argument with value.
Definition arg.hpp:403
void setDescription(const String &desc)
Set description.
Definition arg.hpp:490
void checkCorrectnessAfterParsing() const override
Check correctness of the argument after parsing.
Definition arg.hpp:389
const String & description() const override
Definition arg.hpp:484
ArgIface * findArgument(const String &name) override
Definition arg.hpp:202
virtual void setDefaultValue(const String &v)
Set default value.
Definition arg.hpp:159
virtual ~Arg()
Definition arg.hpp:306
Interface for arguments.
Definition arg_iface.hpp:51
CmdLine * cmdLine() const
Base exception of the library.
Context is a list of words in the command line that user presented with interface for interacting wit...
Definition context.hpp:60
Definition api.hpp:42
std::string String
String type.
Definition types.hpp:324
std::vector< String > StringList
List of strings.
Definition types.hpp:346
String eatOneValue(Ctx &context, const String &errorDescription, Cmd *cmdLine)
Eat one value.
ArgType
Type of the argument.
Definition enums.hpp:56
@ Arg
Argument.
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