kaycxx-cli
C++ CLI library
Loading...
Searching...
No Matches
command_handle.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 <iostream>
12#include <optional>
13#include <string>
14#include <string_view>
15
17
18namespace kaycxx::cli {
19
26public:
32 [[nodiscard]] std::string const& name() const noexcept {
33 return command_->name();
34 }
35
41 [[nodiscard]] std::optional<std::string> const& description() const noexcept {
42 return command_->description();
43 }
44
55 [[nodiscard]] command_handle command(std::string_view name, std::optional<std::string_view> description = std::nullopt) {
56 return command_->add_command(name, description);
57 }
58
69 [[nodiscard]] flag_handle flag(std::string_view name, std::optional<std::string_view> description = std::nullopt) {
70 return command_->flag(name, description);
71 }
72
84 [[nodiscard]] flag_handle flag(std::string_view name, char alias, std::optional<std::string_view> description = std::nullopt) {
85 return command_->flag(name, alias, description);
86 }
87
101 template <parseable_value T>
103 std::string_view name,
104 std::string_view value_name,
105 std::optional<std::string_view> description = std::nullopt
106 ) {
107 return command_->option<T>(name, value_name, description);
108 }
109
124 template <parseable_value T>
126 std::string_view name,
127 char alias,
128 std::string_view value_name,
129 std::optional<std::string_view> description = std::nullopt
130 ) {
131 return command_->option<T>(name, alias, value_name, description);
132 }
133
147 template <parseable_value T>
149 std::string_view name,
150 std::string_view value_name,
151 std::optional<std::string_view> description = std::nullopt
152 ) {
153 return command_->repeatable_option<T>(name, value_name, description);
154 }
155
170 template <parseable_value T>
172 std::string_view name,
173 char alias,
174 std::string_view value_name,
175 std::optional<std::string_view> description = std::nullopt
176 ) {
177 return command_->repeatable_option<T>(name, alias, value_name, description);
178 }
179
190 template <parseable_value T>
191 [[nodiscard]] parameter_handle<T> parameter(std::string_view name, std::optional<std::string_view> description = std::nullopt) {
192 return command_->parameter<T>(name, description);
193 }
194
205 template <parseable_value T>
206 [[nodiscard]] parameters_handle<T> parameters(std::string_view name, std::optional<std::string_view> description = std::nullopt) {
207 return command_->parameters<T>(name, description);
208 }
209
218 [[nodiscard]] args parse(int argc, char *argv[]) const {
219 return command_->parse(argc, argv);
220 }
221
229 int print_help(std::ostream& out = std::cout) const {
230 return command_->print_help(out);
231 }
232
240 int print_version(std::ostream& out = std::cout) const {
241 return command_->print_version(out);
242 }
243
244private:
245 friend class args;
246 friend class command;
247
253 explicit command_handle(kaycxx::cli::command& definition) noexcept
254 : command_(&definition)
255 {}
256
258 kaycxx::cli::command* command_;
259};
260
261} // namespace kaycxx::cli
Parsed command line arguments.
Definition args.hpp:41
Handle for a registered command.
Definition command_handle.hpp:25
std::optional< std::string > const & description() const noexcept
Returns this command's configured description.
Definition command_handle.hpp:41
args parse(int argc, char *argv[]) const
Parses command-line arguments from this command as the starting command.
Definition command_handle.hpp:218
option_handle< T > option(std::string_view name, char alias, std::string_view value_name, std::optional< std::string_view > description=std::nullopt)
Registers an option with a short alias.
Definition command_handle.hpp:125
parameter_handle< T > parameter(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Registers a single positional parameter.
Definition command_handle.hpp:191
std::string const & name() const noexcept
Returns the command name.
Definition command_handle.hpp:32
int print_help(std::ostream &out=std::cout) const
Writes generated help output.
Definition command_handle.hpp:229
repeatable_option_handle< T > repeatable_option(std::string_view name, std::string_view value_name, std::optional< std::string_view > description=std::nullopt)
Registers a repeatable option without a short alias.
Definition command_handle.hpp:148
repeatable_option_handle< T > repeatable_option(std::string_view name, char alias, std::string_view value_name, std::optional< std::string_view > description=std::nullopt)
Registers a repeatable option with a short alias.
Definition command_handle.hpp:171
flag_handle flag(std::string_view name, char alias, std::optional< std::string_view > description=std::nullopt)
Registers a flag with a short alias.
Definition command_handle.hpp:84
command_handle command(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Registers a child command.
Definition command_handle.hpp:55
flag_handle flag(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Registers a flag without a short alias.
Definition command_handle.hpp:69
option_handle< T > option(std::string_view name, std::string_view value_name, std::optional< std::string_view > description=std::nullopt)
Registers an option without a short alias.
Definition command_handle.hpp:102
int print_version(std::ostream &out=std::cout) const
Writes generated version output.
Definition command_handle.hpp:240
parameters_handle< T > parameters(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Registers a positional parameter list.
Definition command_handle.hpp:206
Common base class for an application and its commands.
Definition command.hpp:45
command_handle add_command(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Registers a child command.
cli::parameter_handle< T > parameter(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Registers a single positional parameter.
Definition command.hpp:212
int print_version(std::ostream &out=std::cout) const
Writes generated version output.
int print_help(std::ostream &out=std::cout) const
Writes generated help output.
cli::args parse(int argc, char *argv[]) const
Parses command line arguments.
std::string const & name() const noexcept
Returns the command name.
cli::parameters_handle< T > parameters(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Registers a positional parameter list.
Definition command.hpp:231
cli::option_handle< T > option(std::string_view name, std::string_view value_name, std::optional< std::string_view > description=std::nullopt)
Registers an option without a short alias.
Definition command.hpp:109
cli::repeatable_option_handle< T > repeatable_option(std::string_view name, std::string_view value_name, std::optional< std::string_view > description=std::nullopt)
Registers a repeatable option without a short alias.
Definition command.hpp:161
cli::flag_handle flag(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Registers a flag without a short alias.
std::optional< std::string > const & description() const noexcept
Returns this command's configured description.
Handle for a registered flag.
Definition flag_handle.hpp:22
Handle for a registered option.
Definition option_handle.hpp:25
Defines the common command base class.