koutil
Loading...
Searching...
No Matches
errors.h
Go to the documentation of this file.
1#ifndef KOUTIL_ARGS_ERRORS_H
2#define KOUTIL_ARGS_ERRORS_H
3
6#include <cstdint>
7#include <format>
8#include <string>
9#include <string_view>
10
12
13inline std::string make_argument_count(std::string_view name, std::uint32_t expected, std::uint32_t provided) {
14 return std::format(
15 "'{}' requires {} argument{}, but {} {} provided",
16 name,
17 expected,
18 expected == 1 ? "" : "s",
19 provided,
20 provided == 1 ? "was" : "were"
21 );
22}
23
24inline std::string make_command_argument_count(std::string_view name, std::uint32_t expected, std::uint32_t provided) {
25 return "command " + make_argument_count(name, expected, provided);
26}
27
28inline std::string make_unknown_argument(std::string_view name) { return std::format("unknown argument: {}", name); }
29
30inline std::string make_unknown_long_option(std::string_view name) {
31 return std::format("unknown option '--{}'", name);
32}
33
34inline std::string make_unknown_short_option(char name) { return std::format("unknown option '-{}'", name); }
35
36inline std::string make_invalid_short_option(std::string_view name) {
37 return std::format("Invalid short option '-{}': must be a single character", name);
38}
39
40inline std::string make_option_requires_value(std::string_view whole_name) {
41 return std::format("option '{}' requires a value", whole_name);
42}
43
44inline std::string make_unexpected_option_value(std::string_view whole_name) {
45 return std::format("option '{}' doesn't accept a value", whole_name);
46}
47
48template <extends_result Result> inline std::string make_missing_required_option(const option_t<Result>& option) {
49 // format option name
50 std::string display;
51 if (option.long_name()) {
52 display = std::format("--{}", *option.long_name());
53 if (option.short_name()) {
54 display += std::format(" (-{})", *option.short_name());
55 }
56 } else if (option.short_name()) {
57 display = std::format("-{}", *option.short_name());
58 }
59
60 return std::format("required option {} was not provided", display);
61}
62
63}
64
65#endif
Represents a command-line option.
Definition option.h:41
auto short_name() const
Gets the short option name.
Definition option.h:85
auto long_name() const
Gets the long option name.
Definition option.h:79
Definition errors.h:11
std::string make_invalid_short_option(std::string_view name)
Definition errors.h:36
std::string make_missing_required_option(const option_t< Result > &option)
Definition errors.h:48
std::string make_command_argument_count(std::string_view name, std::uint32_t expected, std::uint32_t provided)
Definition errors.h:24
std::string make_unknown_argument(std::string_view name)
Definition errors.h:28
std::string make_unexpected_option_value(std::string_view whole_name)
Definition errors.h:44
std::string make_unknown_short_option(char name)
Definition errors.h:34
std::string make_argument_count(std::string_view name, std::uint32_t expected, std::uint32_t provided)
Definition errors.h:13
std::string make_option_requires_value(std::string_view whole_name)
Definition errors.h:40
std::string make_unknown_long_option(std::string_view name)
Definition errors.h:30