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 <memory>
13#include <optional>
14#include <ostream>
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
40 std::optional<std::string_view> version = std::nullopt;
41
43 std::optional<std::string_view> description = std::nullopt;
44
46 std::optional<std::string_view> author = std::nullopt;
47
49 std::optional<std::string_view> email = std::nullopt;
50
52 std::optional<std::string_view> bugs = std::nullopt;
53
55 std::optional<std::string_view> copyright = std::nullopt;
56
58 std::optional<std::string_view> license = std::nullopt;
59};
60
68class command {
69public:
76 explicit command(std::string_view name, command_options options = command_options());
77
79 command(command const&) = delete;
80
82 command& operator=(command const&) = delete;
83
89 command(command&& other) noexcept = default;
90
98 command& operator=(command&& other) noexcept = default;
99
105 [[nodiscard]] std::string const& name() const noexcept;
106
112 [[nodiscard]] std::optional<std::string> const& version() const noexcept;
113
119 [[nodiscard]] std::optional<std::string> const& author() const noexcept;
120
126 [[nodiscard]] std::optional<std::string> const& email() const noexcept;
127
133 [[nodiscard]] std::optional<std::string> const& bugs() const noexcept;
134
140 [[nodiscard]] std::optional<std::string> const& copyright() const noexcept;
141
147 [[nodiscard]] std::optional<std::string> const& license() const noexcept;
148
154 [[nodiscard]] std::optional<std::string> const& description() const noexcept;
155
166 [[nodiscard]] cli::flag_handle flag(std::string_view name, std::optional<std::string_view> description = std::nullopt);
167
179 [[nodiscard]] cli::flag_handle flag(std::string_view name, char alias, std::optional<std::string_view> description = std::nullopt);
180
194 template <parseable_value T>
195 [[nodiscard]] cli::option_handle<T> option(
196 std::string_view name,
197 std::string_view value_name,
198 std::optional<std::string_view> description = std::nullopt
199 ) {
200 auto item = std::make_unique<cli::option<T>>(name, value_name, description);
201 auto& definition = *item;
202 add_switch(std::move(item));
203 return cli::option_handle<T>(definition);
204 }
205
220 template <parseable_value T>
222 std::string_view name,
223 char alias,
224 std::string_view value_name,
225 std::optional<std::string_view> description = std::nullopt
226 ) {
227 auto item = std::make_unique<cli::option<T>>(name, alias, value_name, description);
228 auto& definition = *item;
229 add_switch(std::move(item));
230 return cli::option_handle<T>(definition);
231 }
232
246 template <parseable_value T>
248 std::string_view name,
249 std::string_view value_name,
250 std::optional<std::string_view> description = std::nullopt
251 ) {
252 auto item = std::make_unique<cli::option<T>>(name, value_name, description);
253 item->mark_as_repeatable();
254 auto& definition = *item;
255 add_switch(std::move(item));
256 return cli::repeatable_option_handle<T>(definition);
257 }
258
273 template <parseable_value T>
275 std::string_view name,
276 char alias,
277 std::string_view value_name,
278 std::optional<std::string_view> description = std::nullopt
279 ) {
280 auto item = std::make_unique<cli::option<T>>(name, alias, value_name, description);
281 item->mark_as_repeatable();
282 auto& definition = *item;
283 add_switch(std::move(item));
284 return cli::repeatable_option_handle<T>(definition);
285 }
286
297 template <parseable_value T>
298 [[nodiscard]] cli::parameter_handle<T> parameter(std::string_view name, std::optional<std::string_view> description = std::nullopt) {
299 auto item = std::make_unique<cli::parameter<T>>(name, description);
300 auto& definition = *item;
301 parameters_.push_back(std::move(item));
302 return cli::parameter_handle<T>(definition);
303 }
304
315 template <parseable_value T>
316 [[nodiscard]] cli::parameters_handle<T> parameters(std::string_view name, std::optional<std::string_view> description = std::nullopt) {
317 auto item = std::make_unique<cli::parameters<T>>(name, description);
318 auto& definition = *item;
319 parameters_.push_back(std::move(item));
320 return cli::parameters_handle<T>(definition);
321 }
322
336 [[nodiscard]] cli::args parse(int argc, char *argv[]) const;
337
343 [[nodiscard]] int print_help() const;
344
352 [[nodiscard]] int print_help(std::ostream& out) const;
353
359 [[nodiscard]] int print_version() const;
360
368 [[nodiscard]] int print_version(std::ostream& out) const;
369
370private:
378 void add_switch(std::unique_ptr<switch_base> item);
379
387 [[nodiscard]] switch_base const* find_switch(std::string_view name) const noexcept;
388
396 [[nodiscard]] switch_base const* find_switch(char alias) const noexcept;
397
403 [[nodiscard]] bool has_described_switches() const noexcept;
404
410 [[nodiscard]] bool has_described_parameters() const noexcept;
411
417 [[nodiscard]] std::size_t max_switch_usage_length() const;
418
424 [[nodiscard]] std::size_t max_parameter_usage_length() const;
425
431 void apply_option_defaults(args& result) const;
432
434 std::string name_;
435
437 std::optional<std::string> version_;
438
440 std::optional<std::string> author_;
441
443 std::optional<std::string> email_;
444
446 std::optional<std::string> bugs_;
447
449 std::optional<std::string> copyright_;
450
452 std::optional<std::string> license_;
453
455 std::optional<std::string> description_;
456
458 std::vector<std::unique_ptr<switch_base>> switches_;
459
461 std::vector<std::unique_ptr<parameter_base>> parameters_;
462};
463
464} // namespace kaycxx::cli
Defines access to parsed command line arguments.
Parsed command line arguments.
Definition args.hpp:38
Defines and parses a command line interface.
Definition command.hpp:68
int print_version() const
Writes generated version output to standard output.
command(std::string_view name, command_options options=command_options())
Creates a command definition.
std::optional< std::string > const & copyright() const noexcept
Returns the configured copyright notice.
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:298
std::optional< std::string > const & license() const noexcept
Returns the configured license text.
command(command const &)=delete
Prevents copying command definitions.
command & operator=(command &&other) noexcept=default
Move-assigns a command definition.
std::optional< std::string > const & author() const noexcept
Returns the configured author name.
int print_version(std::ostream &out) const
Writes generated version output.
int print_help(std::ostream &out) const
Writes generated help output.
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:274
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:221
int print_help() const
Writes generated help output to standard output.
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:316
command & operator=(command const &)=delete
Prevents copy-assigning command definitions.
command(command &&other) noexcept=default
Moves a command definition.
std::optional< std::string > const & bugs() const noexcept
Returns the email address or URL to which to report bugs to.
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:247
std::optional< std::string > const & version() const noexcept
Returns the configured command version.
std::optional< std::string > const & description() const noexcept
Returns the configured command description.
std::optional< std::string > const & email() const noexcept
Returns the configured author email address.
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:93
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.
Optional metadata for a command.
Definition command.hpp:38
std::optional< std::string_view > bugs
Email address or website URL to which to report bugs to.
Definition command.hpp:52
std::optional< std::string_view > version
Command version string used by version output.
Definition command.hpp:40
std::optional< std::string_view > email
Author contact address used by generated version output.
Definition command.hpp:49
std::optional< std::string_view > author
Author name used by generated version output.
Definition command.hpp:46
std::optional< std::string_view > license
License text used by generated version output.
Definition command.hpp:58
std::optional< std::string_view > copyright
Copyright notice used by generated version output.
Definition command.hpp:55
std::optional< std::string_view > description
Short command description used by generated help output.
Definition command.hpp:43
Defines the common base class for flags and options.