args-parser 6.3.6
Loading...
Searching...
No Matches
value_utils.hpp
Go to the documentation of this file.
1
2/*
3 SPDX-FileCopyrightText: 2026 Igor Mironchik <igor.mironchik@gmail.com>
4 SPDX-License-Identifier: MIT
5*/
6
7#ifndef ARGS__VALUE_UTILS_HPP__INCLUDED
8#define ARGS__VALUE_UTILS_HPP__INCLUDED
9
10// Args include.
11#include "exceptions.hpp"
12#include "types.hpp"
13#include "utils.hpp"
14
15// C++ include.
16#include <algorithm>
17
18namespace Args
19{
20
21//
22// eatValues
23//
24
26template<typename Container,
27 typename Cmd,
28 typename Ctx>
29bool eatValues(Ctx &context,
30 Container &container,
31 const String &errorDescription,
32 Cmd *cmdLine)
33{
34 if (!context.atEnd()) {
35 auto begin = context.begin();
36
37 auto last = std::find_if(context.begin(), context.end(), [&](const String &v) -> bool {
38 return (cmdLine->findArgument(v) != nullptr);
39 });
40
41 if (last != begin) {
42 begin = context.next();
43
44 while (begin != last) {
45 container.push_back(*begin);
46
47 begin = context.next();
48 }
49
50 if (last != context.end()) {
51 context.putBack();
52 }
53
54 return true;
55 }
56 }
57
58 throw BaseException(errorDescription);
59}
60
61//
62// eatOneValue
63//
64
66template<typename Cmd,
67 typename Ctx>
68String eatOneValue(Ctx &context,
69 const String &errorDescription,
70 Cmd *cmdLine)
71{
72 if (!context.atEnd()) {
73 auto val = context.next();
74
75 if (!cmdLine->findArgument(*val)) {
76 return *val;
77 }
78
79 context.putBack();
80 }
81
82 throw BaseException(errorDescription);
83}
84
85} /* namespace Args */
86
87#endif // ARGS__VALUE_UTILS_HPP__INCLUDED
Base exception of the library.
Definition api.hpp:18
String eatOneValue(Ctx &context, const String &errorDescription, Cmd *cmdLine)
Eat one value.
std::string String
String type.
Definition types.hpp:314
bool eatValues(Ctx &context, Container &container, const String &errorDescription, Cmd *cmdLine)
Eat values in context.