args-parser 6.3.3
Loading...
Searching...
No Matches
value_utils.hpp
Go to the documentation of this file.
1
31#ifndef ARGS__VALUE_UTILS_HPP__INCLUDED
32#define ARGS__VALUE_UTILS_HPP__INCLUDED
33
34// Args include.
35#include "utils.hpp"
36#include "exceptions.hpp"
37#include "types.hpp"
38
39// C++ include.
40#include <algorithm>
41
42
43namespace Args {
44
45//
46// eatValues
47//
48
50template< typename Container, typename Cmd, typename Ctx >
51bool eatValues( Ctx & context, Container & container,
52 const String & errorDescription, Cmd * cmdLine )
53{
54 if( !context.atEnd() )
55 {
56 auto begin = context.begin();
57
58 auto last = std::find_if( context.begin(), context.end(),
59 [ & ] ( const String & v ) -> bool
60 {
61 return( cmdLine->findArgument( v ) != nullptr );
62 }
63 );
64
65 if( last != begin )
66 {
67 begin = context.next();
68
69 while( begin != last )
70 {
71 container.push_back( *begin );
72
73 begin = context.next();
74 }
75
76 if( last != context.end() )
77 context.putBack();
78
79 return true;
80 }
81 }
82
83 throw BaseException( errorDescription );
84}
85
86
87//
88// eatOneValue
89//
90
92template< typename Cmd, typename Ctx >
93String eatOneValue( Ctx & context,
94 const String & errorDescription, Cmd * cmdLine )
95{
96 if( !context.atEnd() )
97 {
98 auto val = context.next();
99
100 if( !cmdLine->findArgument( *val ) )
101 return *val;
102
103 context.putBack();
104 }
105
106 throw BaseException( errorDescription );
107}
108
109} /* namespace Args */
110
111#endif // ARGS__VALUE_UTILS_HPP__INCLUDED
Base exception of the library.
Definition api.hpp:42
std::string String
String type.
Definition types.hpp:324
String eatOneValue(Ctx &context, const String &errorDescription, Cmd *cmdLine)
Eat one value.
bool eatValues(Ctx &context, Container &container, const String &errorDescription, Cmd *cmdLine)
Eat values in context.