C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
arg_parser.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../abi/map.hpp"
26#include "../abi/string.hpp"
27#include "../compat.hpp"
28#include "../meta/runtime/struct.hpp"
29#include "abstract/option.hpp"
30#include "common_types.hpp"
31#include "option_result.hpp"
32
33#include <concepts>
34#include <cstdint>
35#include <functional>
36#include <memory>
37#include <optional>
38#include <span>
39#include <string_view>
40
41namespace essence::cli {
45 class arg_parser {
46 public:
47 using parse_result_type = abi::map<abi::string, option_result, std::less<>>;
48
52 ES_API(CPPESSENCE) arg_parser();
53 ES_API(CPPESSENCE) arg_parser(arg_parser&&) noexcept;
54 ES_API(CPPESSENCE) ~arg_parser();
55 ES_API(CPPESSENCE) arg_parser& operator=(arg_parser&&) noexcept;
56
61 ES_API(CPPESSENCE) explicit operator bool() const noexcept;
62
67 [[nodiscard]] ES_API(CPPESSENCE) std::span<const abstract::option> options() const noexcept;
68
73 [[nodiscard]] ES_API(CPPESSENCE) const parse_result_type& cached_result() const noexcept;
74
79 [[nodiscard]] ES_API(CPPESSENCE) std::span<const abi::string> unmatched_args() const noexcept;
80
85 ES_API(CPPESSENCE) void add_option(abstract::option option) const; // NOLINT(*-use-nodiscard)
86
90 ES_API(CPPESSENCE) void parse() const;
91
97 ES_API(CPPESSENCE) void parse(std::int32_t argc, char* argv[]) const;
98
103 ES_API(CPPESSENCE) void parse(std::span<const abi::string> args) const;
104
108 ES_API(CPPESSENCE) void show_help() const;
109
114 ES_API(CPPESSENCE) void on_error(const output_handler& handler) const;
115
120 ES_API(CPPESSENCE) void on_output(const output_handler& handler) const;
121
127 template <std::default_initializable T>
128 requires std::is_class_v<T>
129 std::optional<T> to_model() const {
130 T result{};
131 auto&& mapping = cached_result();
132
133 if (mapping.empty()) {
134 return std::nullopt;
135 }
136
137 // Sets the data members of the entity.
138 auto handler = [&](const auto& item) {
139 if (const auto iter = mapping.find(std::string_view{item.name}); iter != mapping.end()) {
140 iter->second.option.set_target_from_cache(item.reference);
141 }
142 };
143
144 meta::runtime::enumerate_data_members(result, [&](const auto&... members) { (handler(members), ...); });
145
146 return result;
147 }
148
149 private:
150 class impl;
151
152 std::unique_ptr<impl> impl_;
153 };
154} // namespace essence::cli
A parser to handle the command line arguments.
Definition arg_parser.hpp:45
ES_API(CPPESSENCE) arg_parser()
Creates an instance.
std::optional< T > to_model() const
Fill a data model with the parsed result.
Definition arg_parser.hpp:129
Creates a base internal implementation of a CLI option class.
Definition option.hpp:66