Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Argv.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_UTIL_ARGV_HPP
18#define TOOLBOX_UTIL_ARGV_HPP
19
20#include <toolbox/Config.h>
21
22#include <optional>
23#include <string_view>
24
25namespace toolbox {
26inline namespace util {
27
30 public:
31 using ConstIterator = const char* const*;
32 constexpr ArgvSequence(int argc, const char* const argv[])
33 : begin_{argv}
34 , end_{argv + argc}
35 {
36 }
37 ~ArgvSequence() = default;
38
39 // Copy.
40 constexpr ArgvSequence(const ArgvSequence&) noexcept = default;
41 constexpr ArgvSequence& operator=(const ArgvSequence&) noexcept = default;
42
43 // Move.
46
48 constexpr ConstIterator end() const noexcept { return end_; }
49
50 private:
51 ConstIterator begin_, end_;
52};
53
55 public:
58 : it_{begin}
59 , end_{end}
60 {
61 next();
62 }
63 explicit ArgvLexer(ArgvSequence argv) noexcept
64 : ArgvLexer{argv.begin(), argv.end()}
65 {
66 }
67 ArgvLexer(int argc, const char* const argv[]) noexcept
68 : ArgvLexer{ArgvSequence{argc, argv}}
69 {
70 }
71 ~ArgvLexer() = default;
72
73 // Copy.
74 ArgvLexer(const ArgvLexer&) noexcept = default;
75 ArgvLexer& operator=(const ArgvLexer&) noexcept = default;
76
77 // Move.
78 ArgvLexer(ArgvLexer&&) noexcept = default;
79 ArgvLexer& operator=(ArgvLexer&&) noexcept = default;
80
81 bool empty() const noexcept { return it_ == end_; }
82
83 std::string_view arg() const noexcept { return *it_; }
84 std::string_view opt() const noexcept { return opt_; }
85
86 void pop() noexcept
87 {
88 ++it_;
89 next();
90 }
93 void pop_switch();
94
97 std::string_view pop_value();
98
99 private:
100 void next() noexcept;
101
102 ConstIterator it_, end_;
103 std::string_view opt_;
104 std::optional<std::string_view> val_;
105};
106
107} // namespace util
108} // namespace toolbox
109
110#endif // TOOLBOX_UTIL_ARGV_HPP
#define TOOLBOX_API
Definition Config.h:39
ArgvLexer(ConstIterator begin, ConstIterator end) noexcept
Definition Argv.hpp:57
ArgvLexer(int argc, const char *const argv[]) noexcept
Definition Argv.hpp:67
std::string_view opt() const noexcept
Definition Argv.hpp:84
ArgvLexer(const ArgvLexer &) noexcept=default
ArgvLexer(ArgvLexer &&) noexcept=default
ArgvSequence::ConstIterator ConstIterator
Definition Argv.hpp:56
std::string_view arg() const noexcept
Definition Argv.hpp:83
ArgvLexer(ArgvSequence argv) noexcept
Definition Argv.hpp:63
ArgvLexer & operator=(const ArgvLexer &) noexcept=default
void pop() noexcept
Definition Argv.hpp:86
Utility class that allows an argv array to be treated as a sequence.
Definition Argv.hpp:29
constexpr ArgvSequence(const ArgvSequence &) noexcept=default
constexpr ConstIterator begin() const noexcept
Definition Argv.hpp:47
constexpr ConstIterator end() const noexcept
Definition Argv.hpp:48
constexpr ArgvSequence & operator=(const ArgvSequence &) noexcept=default
const char *const * ConstIterator
Definition Argv.hpp:31
constexpr ArgvSequence(int argc, const char *const argv[])
Definition Argv.hpp:32
constexpr ArgvSequence(ArgvSequence &&) noexcept=default
STL namespace.
constexpr auto bind() noexcept
Definition Slot.hpp:92