cli20 0.1.2
A C++20 typed-schema command-line parser
Loading...
Searching...
No Matches
error.hh
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
5namespace cli {
6
45
58
65[[nodiscard]] constexpr auto to_string(ErrorCode code) noexcept
66 -> std::string_view {
67 switch (code) {
69 return "unknown option";
71 return "unknown command";
73 return "unexpected argument";
75 return "missing value";
77 return "invalid value";
79 return "duplicate argument";
81 return "conversion error";
83 return "out of range";
85 return "missing required argument";
87 return "mutually exclusive argument";
89 return "dependency missing";
91 return "invalid choice";
93 return "validation failed";
95 return "help requested";
97 return "exit success";
99 return "unknown error";
100 }
101
102 return "unknown error";
103}
104
126
129
132 int position = -1;
133
135 std::string subject{};
136
139 std::string detail{};
140
146 [[nodiscard]] constexpr auto has_position() const noexcept -> bool {
147 return position >= 0;
148 }
149
155 [[nodiscard]] constexpr auto exit_code() const noexcept -> int {
157 ? 0
158 : 1;
159 }
160
169 [[nodiscard]] constexpr auto use_stdout() const noexcept -> bool {
171 }
172
182 [[nodiscard]] constexpr auto message() const -> std::string {
184 return detail;
185 }
186
188 return detail;
189 }
190
191 std::string out{to_string(code)};
192 if (!subject.empty()) {
193 out += ": ";
194 out += subject;
195 }
196
197 if (!detail.empty()) {
198 out += " (";
199 out += detail;
200 out += ")";
201 }
202
203 if (has_position()) {
204 out += " at argv[";
205 out += std::to_string(position);
206 out += "]";
207 }
208
209 return out;
210 }
211
217 [[nodiscard]] constexpr auto has_error() const noexcept -> bool {
219 }
220};
221
222} // namespace cli
Actions that convert a raw std::string_view token to a typed value.
Definition action.hh:635
Actions that validate a typed value after conversion.
Definition action.hh:1046
Definition action.hh:29
auto parse(int argc, char *argv[]) -> ParseResult< T >
Convenience free function that parses argc/argv into type T.
Definition parser.hh:1464
ErrorKind
Broad classification of an error for structured error handling.
Definition error.hh:53
constexpr auto to_string(ErrorCode code) noexcept -> std::string_view
Returns a human-readable description of an ErrorCode.
Definition error.hh:65
ErrorCode
Identifies the specific failure that occurred during parsing or validation.
Definition error.hh:20
@ invalid_value
A value could not be interpreted as the required type.
Definition error.hh:26
@ unknown_option
An unrecognised option name was encountered.
Definition error.hh:21
@ conversion_error
Generic failure during string-to-type conversion.
Definition error.hh:30
@ unknown_command
An unrecognised subcommand name was encountered.
Definition error.hh:22
@ unknown_error
Sentinel/default; indicates no error has been set.
Definition error.hh:43
@ out_of_range
A numeric value fell outside the representable range of the target type.
Definition error.hh:31
@ duplicate_argument
An option or positional that forbids repetition was seen more than once.
Definition error.hh:27
@ validation_failed
A user-defined or built-in validation predicate rejected the value.
Definition error.hh:38
@ missing_value
An option that requires a value was given none.
Definition error.hh:25
@ invalid_choice
A value was not among the allowed choices.
Definition error.hh:37
@ exit_success
The action pipeline requested a clean exit with code 0.
Definition error.hh:42
@ help_requested
The --help flag (or equivalent) was seen; print help and exit 0.
Definition error.hh:40
@ dependency_missing
An option that depends on another option was given without it.
Definition error.hh:35
@ mutually_exclusive
Two mutually-exclusive options were both supplied.
Definition error.hh:34
@ missing_required
A required option or positional was not provided.
Definition error.hh:33
@ unexpected_argument
A positional argument was supplied when none was expected.
Definition error.hh:23
Carries all context about a single parse/validation failure.
Definition error.hh:123
ErrorCode code
The specific error code; unknown_error means no error.
Definition error.hh:125
std::string subject
The token or option name that caused the error (may be empty).
Definition error.hh:135
std::string detail
Additional human-readable context about the failure (may be empty).
Definition error.hh:139
constexpr auto exit_code() const noexcept -> int
Returns the process exit code appropriate for this error.
Definition error.hh:155
ErrorKind kind
Broad category of the error.
Definition error.hh:128
constexpr auto has_position() const noexcept -> bool
Returns true when position contains a valid argv index.
Definition error.hh:146
constexpr auto use_stdout() const noexcept -> bool
Returns true when the error message should be written to stdout.
Definition error.hh:169
int position
Zero-based index into argv where the error was detected, or -1 if unknown.
Definition error.hh:132
constexpr auto has_error() const noexcept -> bool
Returns true when an actual error is present.
Definition error.hh:217
constexpr auto message() const -> std::string
Formats a human-readable error message.
Definition error.hh:182