kaycxx-cli
C++ CLI library
Loading...
Searching...
No Matches
args.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Klaus Reimer <k@ailis.de>
2// SPDX-License-Identifier: MIT
3
9#pragma once
10
11#include <any>
12#include <string>
13#include <unordered_map>
14#include <unordered_set>
15#include <vector>
16
24
25namespace kaycxx::cli {
26
27class command;
28class command_handle;
29
41class args {
42public:
44 args() = default;
45
47 args(args const&) = delete;
48
50 args& operator=(args const&) = delete;
51
57 args(args&& other) noexcept = default;
58
66 args& operator=(args&& other) noexcept = default;
67
75 void validate() const;
76
87 [[nodiscard]] command const& selected_command() const;
88
96 [[nodiscard]] bool is_selected(command const& command) const noexcept;
97
105 [[nodiscard]] bool is_selected(command_handle const& command) const noexcept;
106
114 [[nodiscard]] bool get(flag_handle const& flag) const;
115
125 template <parseable_value T>
126 [[nodiscard]] T const& get(option_handle<T> const& option) const {
127 return std::any_cast<T const&>(values_.at(&option.definition()));
128 }
129
139 template <parseable_value T>
140 [[nodiscard]] bool has(option_handle<T> const& option) const {
141 return values_.contains(&option.definition());
142 }
143
152 template <parseable_value T>
153 [[nodiscard]] std::vector<T> const& get(repeatable_option_handle<T> const& option) const {
154 return std::any_cast<std::vector<T> const&>(values_.at(&option.definition()));
155 }
156
165 template <parseable_value T>
166 [[nodiscard]] bool has(repeatable_option_handle<T> const& option) const {
167 return values_.contains(&option.definition());
168 }
169
181 template <parseable_value T>
182 [[nodiscard]] T const& get(parameter_handle<T> const& parameter) const {
183 validate();
184 return std::any_cast<T const&>(parameters_.at(&parameter.definition()));
185 }
186
198 template <parseable_value T>
199 [[nodiscard]] bool has(parameter_handle<T> const& parameter) const {
200 validate();
201 return parameters_.contains(&parameter.definition());
202 }
203
215 template <parseable_value T>
216 [[nodiscard]] std::vector<T> const& get(parameters_handle<T> const& parameters) const {
217 validate();
218 return std::any_cast<std::vector<T> const&>(parameters_.at(&parameters.definition()));
219 }
220
232 template <parseable_value T>
233 [[nodiscard]] bool has(parameters_handle<T> const& parameters) const {
234 validate();
235 return parameters_.contains(&parameters.definition());
236 }
237
238private:
239 friend class command;
240
242 command const* selected_command_ = nullptr;
243
245 bool missing_subcommand_ = false;
246
248 std::unordered_set<switch_base const*> flags_;
249
251 std::unordered_map<switch_base const*, std::any> values_;
252
254 std::vector<option_base const*> required_option_definitions_;
255
257 std::vector<parameter_base const*> parameter_definitions_;
258
260 std::vector<std::string> positional_values_;
261
263 mutable std::unordered_map<parameter_base const*, std::any> parameters_;
264
266 mutable bool validated_ = false;
267};
268
269} // namespace kaycxx::cli
Parsed command line arguments.
Definition args.hpp:41
bool has(parameter_handle< T > const &parameter) const
Checks whether a positional parameter value is available.
Definition args.hpp:199
std::vector< T > const & get(repeatable_option_handle< T > const &option) const
Returns all parsed values of a repeatable option.
Definition args.hpp:153
args & operator=(args &&other) noexcept=default
Move-assigns parsed arguments.
T const & get(parameter_handle< T > const &parameter) const
Returns a parsed positional parameter value.
Definition args.hpp:182
bool is_selected(command const &command) const noexcept
Checks whether a command is the deepest selected command.
command const & selected_command() const
Returns the deepest command selected by the parsed command line.
bool has(repeatable_option_handle< T > const &option) const
Checks whether values for a repeatable option are available.
Definition args.hpp:166
args & operator=(args const &)=delete
Prevents copy-assigning parsed arguments.
args(args &&other) noexcept=default
Moves parsed arguments.
bool get(flag_handle const &flag) const
Checks whether a flag was set.
args(args const &)=delete
Prevents copying parsed arguments.
bool is_selected(command_handle const &command) const noexcept
Checks whether a command handle identifies the deepest selected command.
std::vector< T > const & get(parameters_handle< T > const &parameters) const
Returns parsed positional parameter list values.
Definition args.hpp:216
void validate() const
Validates required options and subcommands and converts all positional parameters.
args()=default
Creates an empty argument collection.
T const & get(option_handle< T > const &option) const
Returns a parsed option value.
Definition args.hpp:126
bool has(parameters_handle< T > const &parameters) const
Checks whether positional parameter list values are available.
Definition args.hpp:233
bool has(option_handle< T > const &option) const
Checks whether an option value is available.
Definition args.hpp:140
Handle for a registered command.
Definition command_handle.hpp:25
Common base class for an application and its commands.
Definition command.hpp:45
Handle for a registered flag.
Definition flag_handle.hpp:22
Command line flag definition.
Definition flag.hpp:22
Handle for a registered option.
Definition option_handle.hpp:25
Typed command line option definition.
Definition option.hpp:156
Handle for a registered positional parameter.
Definition parameter_handle.hpp:25
Typed single positional parameter definition.
Definition parameter.hpp:33
Handle for a registered positional parameter list.
Definition parameters_handle.hpp:28
Typed positional parameter list definition.
Definition parameters.hpp:38
Handle for a registered repeatable option.
Definition repeatable_option_handle.hpp:25
Defines the flag handle type.
Defines the option handle type.
Defines the common base class for positional parameters.
Defines the positional parameter handle type.
Defines the positional parameter list handle type.
Defines the repeatable option handle type.
Defines the common base class for flags and options.