kaycxx-cli
C++ CLI library
Loading...
Searching...
No Matches
command.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 <cstddef>
12#include <iostream>
13#include <memory>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <utility>
18#include <vector>
19
20#include <kaycxx/cli/args.hpp>
21#include <kaycxx/cli/flag.hpp>
23#include <kaycxx/cli/option.hpp>
32
33namespace kaycxx::cli {
34
35class app;
36class command_handle;
37
45class command {
46public:
48 command(command const&) = delete;
49
51 command& operator=(command const&) = delete;
52
54 virtual ~command() = default;
55
61 [[nodiscard]] std::string const& name() const noexcept;
62
68 [[nodiscard]] std::optional<std::string> const& description() const noexcept;
69
80 [[nodiscard]] cli::flag_handle flag(std::string_view name, std::optional<std::string_view> description = std::nullopt);
81
93 [[nodiscard]] cli::flag_handle flag(std::string_view name, char alias, std::optional<std::string_view> description = std::nullopt);
94
108 template <parseable_value T>
109 [[nodiscard]] cli::option_handle<T> option(
110 std::string_view name,
111 std::string_view value_name,
112 std::optional<std::string_view> description = std::nullopt
113 ) {
114 auto item = std::make_unique<cli::option<T>>(name, value_name, description);
115 auto& definition = *item;
116 add_switch(std::move(item));
117 return cli::option_handle<T>(definition);
118 }
119
134 template <parseable_value T>
136 std::string_view name,
137 char alias,
138 std::string_view value_name,
139 std::optional<std::string_view> description = std::nullopt
140 ) {
141 auto item = std::make_unique<cli::option<T>>(name, alias, value_name, description);
142 auto& definition = *item;
143 add_switch(std::move(item));
144 return cli::option_handle<T>(definition);
145 }
146
160 template <parseable_value T>
162 std::string_view name,
163 std::string_view value_name,
164 std::optional<std::string_view> description = std::nullopt
165 ) {
166 auto item = std::make_unique<cli::option<T>>(name, value_name, description);
167 item->mark_as_repeatable();
168 auto& definition = *item;
169 add_switch(std::move(item));
170 return cli::repeatable_option_handle<T>(definition);
171 }
172
187 template <parseable_value T>
189 std::string_view name,
190 char alias,
191 std::string_view value_name,
192 std::optional<std::string_view> description = std::nullopt
193 ) {
194 auto item = std::make_unique<cli::option<T>>(name, alias, value_name, description);
195 item->mark_as_repeatable();
196 auto& definition = *item;
197 add_switch(std::move(item));
198 return cli::repeatable_option_handle<T>(definition);
199 }
200
211 template <parseable_value T>
212 [[nodiscard]] cli::parameter_handle<T> parameter(std::string_view name, std::optional<std::string_view> description = std::nullopt) {
213 ensure_parameter_allowed();
214 auto item = std::make_unique<cli::parameter<T>>(name, description);
215 auto& definition = *item;
216 parameters_.push_back(std::move(item));
217 return cli::parameter_handle<T>(definition);
218 }
219
230 template <parseable_value T>
231 [[nodiscard]] cli::parameters_handle<T> parameters(std::string_view name, std::optional<std::string_view> description = std::nullopt) {
232 ensure_parameter_allowed();
233 auto item = std::make_unique<cli::parameters<T>>(name, description);
234 auto& definition = *item;
235 parameters_.push_back(std::move(item));
236 return cli::parameters_handle<T>(definition);
237 }
238
252 [[nodiscard]] cli::args parse(int argc, char *argv[]) const;
253
261 int print_help(std::ostream& out = std::cout) const;
262
270 int print_version(std::ostream& out = std::cout) const;
271
272protected:
280 command(std::string_view name, std::optional<std::string_view> description, command* parent);
281
287 command(command&& other) noexcept;
288
299 [[nodiscard]] command_handle add_command(std::string_view name, std::optional<std::string_view> description = std::nullopt);
300
301private:
303 class subcommand;
304
305 friend class command_handle;
306
312 [[nodiscard]] virtual app const& root() const noexcept = 0;
313
319 void ensure_parameter_allowed() const;
320
328 void add_switch(std::unique_ptr<switch_base> item);
329
340
350 [[nodiscard]] switch_base const* find_switch(char alias) const noexcept;
351
359 [[nodiscard]] switch_base const* find_switch_in_descendants(std::string_view name) const noexcept;
360
368 [[nodiscard]] switch_base const* find_switch_in_descendants(char alias) const noexcept;
369
377 [[nodiscard]] command const* find_command(std::string_view name) const noexcept;
378
384 [[nodiscard]] std::vector<switch_base const*> switches() const;
385
391 [[nodiscard]] std::string usage_invocation() const;
392
398 [[nodiscard]] std::size_t max_switch_usage_length() const;
399
405 [[nodiscard]] std::size_t max_parameter_usage_length() const;
406
412 [[nodiscard]] std::size_t max_command_name_length() const;
413
419 void apply_option_defaults(args& result) const;
420
426 void collect_required_options(args& result) const;
427
429 std::string name_;
430
432 std::string invocation_;
433
435 std::optional<std::string> description_;
436
438 command* parent_ = nullptr;
439
441 std::vector<std::unique_ptr<switch_base>> switches_;
442
444 std::vector<std::unique_ptr<parameter_base>> parameters_;
445
447 std::vector<std::unique_ptr<command>> commands_;
448};
449
450} // namespace kaycxx::cli
451
452#include <kaycxx/cli/command_handle.hpp>
Defines access to parsed command line arguments.
Defines and parses a command-line application.
Definition app.hpp:50
Parsed command line arguments.
Definition args.hpp:41
Handle for a registered command.
Definition command_handle.hpp:25
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
command(command const &)=delete
Prevents copying command definitions.
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.
command(std::string_view name, std::optional< std::string_view > description, command *parent)
Creates a command definition.
cli::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.hpp:188
cli::args parse(int argc, char *argv[]) const
Parses command line arguments.
std::string const & name() const noexcept
Returns the command name.
cli::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.hpp:135
virtual ~command()=default
Destroys this command and all definitions owned by it.
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
command & operator=(command const &)=delete
Prevents copy-assigning command definitions.
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
command(command &&other) noexcept
Moves a command definition.
std::optional< std::string > const & description() const noexcept
Returns this command's configured description.
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
Common base class for positional parameter definitions.
Definition parameter_base.hpp:23
Common base class for command line switches.
Definition switch_base.hpp:22
Checks whether a type can be used as a command line value.
Definition parse_value.hpp:63
Defines command line flags.
Defines the flag handle type.
Defines command line options.
Defines the option handle type.
Defines single positional parameters.
Defines the common base class for positional parameters.
Defines the positional parameter handle type.
Defines positional parameter lists.
Defines the positional parameter list handle type.
Defines the repeatable option handle type.
Defines the common base class for flags and options.