cli20 0.1.2
A C++20 typed-schema command-line parser
Loading...
Searching...
No Matches
cli::ParseError Struct Reference

Carries all context about a single parse/validation failure. More...

#include <error.hh>

Public Member Functions

constexpr auto has_position () const noexcept -> bool
 Returns true when position contains a valid argv index.
constexpr auto exit_code () const noexcept -> int
 Returns the process exit code appropriate for this error.
constexpr auto use_stdout () const noexcept -> bool
 Returns true when the error message should be written to stdout.
constexpr auto message () const -> std::string
 Formats a human-readable error message.
constexpr auto has_error () const noexcept -> bool
 Returns true when an actual error is present.

Public Attributes

ErrorCode code = ErrorCode::unknown_error
 The specific error code; unknown_error means no error.
ErrorKind kind = ErrorKind::parse
 Broad category of the error.
int position = -1
 Zero-based index into argv where the error was detected, or -1 if unknown.
std::string subject {}
 The token or option name that caused the error (may be empty).
std::string detail {}
 Additional human-readable context about the failure (may be empty).

Detailed Description

Carries all context about a single parse/validation failure.

A ParseError is embedded in both ParseResult<T> and ActionResult<T>. When code == ErrorCode::unknown_error the struct represents the absence of an error (i.e. success).

Typical usage after parsing:

auto result = cli::parse<MyArgs>(argc, argv);
if (!result) {
auto& e = result.error;
auto& out = e.use_stdout() ? std::cout : std::cerr;
out << e.message() << '\n';
std::exit(e.exit_code());
}

Definition at line 123 of file error.hh.

Member Function Documentation

◆ exit_code()

auto cli::ParseError::exit_code ( ) const -> int
inlinenodiscardconstexprnoexcept

Returns the process exit code appropriate for this error.

Returns
0 for help_requested or exit_success, 1 for all other codes.

Definition at line 155 of file error.hh.

◆ has_error()

auto cli::ParseError::has_error ( ) const -> bool
inlinenodiscardconstexprnoexcept

Returns true when an actual error is present.

Returns
true if code != unknown_error.

Definition at line 217 of file error.hh.

◆ has_position()

auto cli::ParseError::has_position ( ) const -> bool
inlinenodiscardconstexprnoexcept

Returns true when position contains a valid argv index.

Returns
true if position >= 0.

Definition at line 146 of file error.hh.

◆ message()

auto cli::ParseError::message ( ) const -> std::string
inlinenodiscardconstexpr

Formats a human-readable error message.

  • For help_requested or exit_success: returns detail verbatim.
  • Otherwise: combines to_string(code), subject, detail, and (if available) the argv position into a single string.
Returns
A formatted error string.

Definition at line 182 of file error.hh.

◆ use_stdout()

auto cli::ParseError::use_stdout ( ) const -> bool
inlinenodiscardconstexprnoexcept

Returns true when the error message should be written to stdout.

Help text and exit-success messages go to stdout; all other errors go to stderr.

Returns
true for help_requested or exit_success.

Definition at line 169 of file error.hh.

Member Data Documentation

◆ code

ErrorCode cli::ParseError::code = ErrorCode::unknown_error

The specific error code; unknown_error means no error.

Definition at line 125 of file error.hh.

◆ detail

std::string cli::ParseError::detail {}

Additional human-readable context about the failure (may be empty).

Definition at line 139 of file error.hh.

◆ kind

ErrorKind cli::ParseError::kind = ErrorKind::parse

Broad category of the error.

Definition at line 128 of file error.hh.

◆ position

int cli::ParseError::position = -1

Zero-based index into argv where the error was detected, or -1 if unknown.

Definition at line 132 of file error.hh.

◆ subject

std::string cli::ParseError::subject {}

The token or option name that caused the error (may be empty).

Definition at line 135 of file error.hh.


The documentation for this struct was generated from the following file: