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

Namespaces

namespace  conversion
 Actions that convert a raw std::string_view token to a typed value.
namespace  validation
 Actions that validate a typed value after conversion.
namespace  pack
 Terminal actions that write the converted (and validated) value into argument storage.
namespace  action
 Actions that control the parser's control flow (help, exit).
namespace  nargs
namespace  constraint
 Post-parse constraint helpers.

Classes

struct  ActionResult
 The result type produced and consumed at each step of an action pipeline. More...
struct  ActionResult< void >
 Specialisation of ActionResult for terminal (pack) actions that produce no value. More...
struct  ActionCtx
 Context passed to each action function during pipeline execution. More...
struct  ActionCtx< void >
 Partial specialisation of ActionCtx used for intermediate pipeline steps. More...
struct  Action
 A compile-time pipeline of action functions applied sequentially to a parsed token. More...
struct  Action<>
struct  Parser
 The main parsing engine for a typed argument specification. More...
struct  ArgParameter
 Parameter bag used when constructing ArgImpl or PositionalImpl with named fields. More...
struct  PositionalImpl
struct  Description
 A DescriptionTag field that carries the top-level CLI description. More...
struct  CommandParameter
 Parameter bag used when constructing a Command field. More...
struct  Command
 A named subcommand that nests another argument specification. More...
struct  SpecMemberTag
 Base tag for all members that may appear in an argument specification struct. More...
struct  OptionTag
 Tag base class for command-line option/flag fields. More...
struct  PositionalTag
 Tag base class for positional argument fields. More...
struct  CommandTag
 Tag base class for subcommand fields. More...
struct  DescriptionTag
 Tag base class for the Description field that provides the CLI's help text. More...
struct  Nargs
 Specifies the minimum and maximum number of values an option or positional accepts. More...
struct  HelpPalette
 ANSI palette used by generated help output. More...
struct  RecurseHelpTag
 Tag type passed to format_help() to request recursive sub-command output. More...
struct  ConstraintResult
 The result type returned by a constraints() post-parse hook. More...
struct  ParseError
 Carries all context about a single parse/validation failure. More...
struct  Token
 A single token produced by tokenize(). More...
struct  TokenizeResult
 The result returned by tokenize(). More...
struct  TokenizerConfig
 Controls how tokenize() recognises option and separator syntax. More...
struct  ParseResult
 The result returned by Parser::parse() and the free cli::parse() function. More...
struct  NameList
struct  GroupParams
struct  GroupRelation
struct  ConflictsRelation
struct  DependsOnRelation
struct  RelationSet
struct  IsRelationSet
struct  IsRelationSet< RelationSet< Relations... > >
struct  StringLiteral
 Compile-time string type usable as a non-type template parameter. More...

Concepts

concept  ArgumentSpec
 Concept that validates an aggregate type as a well-formed CLI argument specification.
concept  RelationSetLike

Typedefs

template<class Step, class Input>
using deduce_after_type
 Deduces after_type for a non-terminal step from its operator().
template<StringLiteral Name, auto... Args>
using Arg = typename detail::ArgAlias<Name, Args...>::type
 Defines a typed command-line argument specification.
template<StringLiteral Name, char ShortName = '\0'>
using Flag = Arg<Name, ShortName, pack::set_true, nargs::none>
 A boolean flag option that stores true when present.
template<class T, StringLiteral Name, char ShortName = '\0'>
using BoundOption = Arg<Name, ShortName, detail::ActionFor<T>::write_to>
 An option whose parsed value is written directly into an external variable.
template<StringLiteral Name, char ShortName = '\0'>
using BoundStringOption = BoundOption<std::string, Name, ShortName>
 BoundOption specialisation for std::string.
template<StringLiteral Name, char ShortName = '\0'>
using BoundIntOption = BoundOption<int, Name, ShortName>
 BoundOption specialisation for int.
template<StringLiteral Name, char ShortName = '\0'>
using BoundDoubleOption = BoundOption<double, Name, ShortName>
 BoundOption specialisation for double.
template<StringLiteral Name, char ShortName = '\0'>
using BoundPathOption = BoundOption<std::filesystem::path, Name, ShortName>
 BoundOption specialisation for std::filesystem::path.
template<StringLiteral Name = "help", char ShortName = 'h'>
using Help
 A built-in help flag that prints help and exits with code 0.
template<class T, StringLiteral Name, char ShortName = '\0'>
using Option = Arg<Name, ShortName, detail::ActionFor<T>::set_once>
 A single-value option that stores the parsed result in T.
template<class T, StringLiteral Name, char ShortName = '\0', Nargs N = nargs::one_or_more>
using ListOption = Arg<Name, ShortName, detail::ActionFor<T>::push, N>
 A multi-value option that appends each occurrence to a std::vector<T>.
template<class T, Nargs N = nargs::one>
using Positional = PositionalImpl<N, detail::PositionalActionFor<T, N>::value>
 A typed positional argument.
template<StringLiteral Name, char ShortName = '\0'>
using StringOption = Option<std::string, Name, ShortName>
 Option<std::string, Name, ShortName>.
template<StringLiteral Name, char ShortName = '\0'>
using IntOption = Option<int, Name, ShortName>
 Option<int, Name, ShortName>.
template<StringLiteral Name, char ShortName = '\0'>
using DoubleOption = Option<double, Name, ShortName>
 Option<double, Name, ShortName>.
template<StringLiteral Name, char ShortName = '\0'>
using PathOption = Option<std::filesystem::path, Name, ShortName>
 Option<std::filesystem::path, Name, ShortName>.

Enumerations

enum class  Presence { required , optional }
 Controls whether an option or positional argument is mandatory. More...
enum class  ColorMode { detect , never , always }
 Controls whether ANSI escape codes are emitted by format_help(). More...
enum class  ErrorCode {
  unknown_option , unknown_command , unexpected_argument , missing_value ,
  invalid_value , duplicate_argument , conversion_error , out_of_range ,
  missing_required , mutually_exclusive , dependency_missing , invalid_choice ,
  validation_failed , help_requested , exit_success , unknown_error
}
 Identifies the specific failure that occurred during parsing or validation. More...
enum class  ErrorKind { parse , conversion , validation }
 Broad classification of an error for structured error handling. More...
enum class  TokenType : uint8_t { option , value , positional , command }
 Classifies a single token produced by tokenize(). More...

Functions

template<class T>
constexpr auto ok (T &&value) -> ActionResult< std::remove_cvref_t< T > >
 Helper functions for constructing ActionResult objects with type deduction.
constexpr auto ok () -> ActionResult< void >
 Helper function for constructing a successful ActionResult<void>.
template<class T>
constexpr auto fail (ParseError error) -> ActionResult< T >
 Helper function for constructing a failure ActionResult with type deduction.
template<class T>
constexpr auto ignore () -> ActionResult< T >
 Helper function for constructing an ignore ActionResult with type deduction.
template<auto Fn, auto... Fns>
constexpr auto operator| (Action< Fns... >, Action< Fn >)
constexpr auto to_string (ErrorCode code) noexcept -> std::string_view
 Returns a human-readable description of an ErrorCode.
template<ArgumentSpec T>
auto format_help (T &value, std::string_view program_name="program", ColorMode color_mode=ColorMode::detect, bool recurse=false, HelpPalette palette=default_help_palette) -> std::string
 Generates a complete help string for an argument specification.
template<class T>
constexpr auto as_tuple (T &t)
 Converts an aggregate value to a std::tuple of references to its members.
template<class T>
constexpr auto as_tuple (const T &t)
 Const overload of as_tuple for the fallback implementation.
static constexpr auto no_prefix () -> std::uint8_t
static constexpr auto short_prefix () -> std::uint8_t
static constexpr auto long_prefix (std::uint8_t i) -> std::uint8_t
static constexpr auto has_prefix (std::uint8_t p) -> bool
static constexpr auto is_short_prefix (std::uint8_t p) -> bool
auto token_prefix_view (const Token &tok, const TokenizerConfig &cfg) -> std::string_view
 Retrieves the matched prefix string for an option token.
template<class OptionLookup, class CommandLookup>
auto tokenize (std::span< const std::string_view > args, OptionLookup option_nargs, CommandLookup is_command, const TokenizerConfig &cfg={}) -> TokenizeResult
 Converts a raw argument span into an ordered sequence of typed tokens.
template<ArgumentSpec T>
auto parse (int argc, char *argv[]) -> ParseResult< T >
 Convenience free function that parses argc/argv into type T.
template<ArgumentSpec T>
auto parse_or_exit (int argc, char *argv[]) -> T
 Parses argc/argv and exits the process on failure.
template<ArgumentSpec T>
auto parse_or_exit (int argc, char *argv[], std::ostream &out, std::ostream &err) -> T
 Parses argc/argv and exits the process on failure.
template<ArgumentSpec T>
auto parse_or_exit (int argc, char *argv[], FILE *out, FILE *err) -> T
 Parses argc/argv and exits the process on failure.
constexpr auto group (GroupParams params) -> GroupRelation
constexpr auto conflicts (std::string_view left, std::string_view right) -> ConflictsRelation
constexpr auto depends_on (std::string_view source, std::string_view target) -> DependsOnRelation
template<class... Relations>
constexpr auto relations (Relations... rels) -> RelationSet< Relations... >
template<std::size_t N>
 StringLiteral (const char(&)[N]) -> StringLiteral< N >
 Deduction guide: StringLiteral("hello") deduces StringLiteral<6>.

Variables

template<class Step, class Input>
constexpr bool deduce_accepts_input
 Deduces accepts_input for a non-terminal step from its operator().
constexpr auto required = Presence::required
 Convenience constant for Presence::required.
constexpr auto optional = Presence::optional
 Convenience constant for Presence::optional.
constexpr HelpPalette default_help_palette {}
constexpr RecurseHelpTag recurse_help {}
 Convenience instance of RecurseHelpTag.
constexpr std::size_t token_stack_capacity = CLI20_TOKEN_STACK_CAPACITY
 Number of tokenizer tokens kept in inline stack storage before falling back to heap allocation.
static constexpr std::uint8_t kNoPrefix = 0xff
static constexpr std::uint8_t kShortPrefixBit = 0x80
static constexpr std::uint8_t kPrefixIndexMask = 0x7f

Typedef Documentation

◆ Arg

template<StringLiteral Name, auto... Args>
using cli::Arg = typename detail::ArgAlias<Name, Args...>::type

Defines a typed command-line argument specification.

Arg constructs a command-line option or flag from a compile-time specification. The supported parameter sequences after Name are:

Form Effect
<Name, Action> No short name; nargs::one.
<Name, Action, Nargs> No short name; custom arity.
<Name, ShortName, Action> With short name; nargs::one.
<Name, ShortName, Action, Nargs> With short name; custom arity.

The short name character (if present) must appear before the action, and the Nargs value (if present) must appear after the action.

Most users should prefer higher-level aliases such as Flag, Option, and ListOption for common argument kinds.

Template Parameters
NameLong option name without the leading --.
ArgsConfiguration parameters: [ShortName,] Action [, Nargs].

Definition at line 526 of file argument.hh.

◆ BoundDoubleOption

template<StringLiteral Name, char ShortName = '\0'>
using cli::BoundDoubleOption = BoundOption<double, Name, ShortName>

BoundOption specialisation for double.

Definition at line 565 of file argument.hh.

◆ BoundIntOption

template<StringLiteral Name, char ShortName = '\0'>
using cli::BoundIntOption = BoundOption<int, Name, ShortName>

BoundOption specialisation for int.

Definition at line 561 of file argument.hh.

◆ BoundOption

template<class T, StringLiteral Name, char ShortName = '\0'>
using cli::BoundOption = Arg<Name, ShortName, detail::ActionFor<T>::write_to>

An option whose parsed value is written directly into an external variable.

The field stores a pointer T*; use the bind(var) method or the reference constructor to point it at the target variable before parsing.

Storage type: T*.

Template Parameters
TThe value type of the external variable.
NameLong option name.
ShortNameShort option character, or '\0'.

Definition at line 553 of file argument.hh.

◆ BoundPathOption

template<StringLiteral Name, char ShortName = '\0'>
using cli::BoundPathOption = BoundOption<std::filesystem::path, Name, ShortName>

BoundOption specialisation for std::filesystem::path.

Definition at line 569 of file argument.hh.

◆ BoundStringOption

template<StringLiteral Name, char ShortName = '\0'>
using cli::BoundStringOption = BoundOption<std::string, Name, ShortName>

BoundOption specialisation for std::string.

Definition at line 557 of file argument.hh.

◆ deduce_after_type

template<class Step, class Input>
using cli::deduce_after_type
Initial value:
typename std::invoke_result_t<const Step&, ActionCtx<void>,
ActionResult<Input>>::value_type
The result type produced and consumed at each step of an action pipeline.
Definition action.hh:48

Deduces after_type for a non-terminal step from its operator().

Extracts the value type T from the ActionResult<T> returned by Step::operator()(ActionCtx<void>, ActionResult<Input>).

This alias is ill-formed when deduce_accepts_input<Step, Input> is false, i.e. operator() is not callable with ActionResult<Input>. This is intentional: after_type is only meaningful for compatible inputs, and a compile error at the alias instantiation is clearer than a silent fallback. Because validate_impl always checks accepts_input before accessing after_type, the ill-formed case is never reached inside a well-formed pipeline.

struct MyStep {
template <class Input>
// ...
};
typename std::invoke_result_t< const Step &, ActionCtx< void >, ActionResult< Input > >::value_type deduce_after_type
Deduces after_type for a non-terminal step from its operator().
Definition action.hh:618
Template Parameters
StepThe action step type.
InputThe pipeline value type.

Definition at line 618 of file action.hh.

◆ DoubleOption

template<StringLiteral Name, char ShortName = '\0'>
using cli::DoubleOption = Option<double, Name, ShortName>

Option<double, Name, ShortName>.

Definition at line 634 of file argument.hh.

◆ Flag

template<StringLiteral Name, char ShortName = '\0'>
using cli::Flag = Arg<Name, ShortName, pack::set_true, nargs::none>

A boolean flag option that stores true when present.

Storage type: bool. Default: false.

Template Parameters
NameLong option name (e.g. "verbose").
ShortNameShort option character (e.g. 'v'), or '\0' for none.

Definition at line 537 of file argument.hh.

◆ Help

template<StringLiteral Name = "help", char ShortName = 'h'>
using cli::Help
Initial value:
typename detail::ArgAlias< Name, Args... >::type Arg
Defines a typed command-line argument specification.
Definition argument.hh:526

A built-in help flag that prints help and exits with code 0.

Default names: --help / -h. Override via template parameters.

Template Parameters
NameLong option name. Default: "help".
ShortNameShort option character. Default: 'h'.

Definition at line 580 of file argument.hh.

◆ IntOption

template<StringLiteral Name, char ShortName = '\0'>
using cli::IntOption = Option<int, Name, ShortName>

Option<int, Name, ShortName>.

Definition at line 630 of file argument.hh.

◆ ListOption

template<class T, StringLiteral Name, char ShortName = '\0', Nargs N = nargs::one_or_more>
using cli::ListOption = Arg<Name, ShortName, detail::ActionFor<T>::push, N>

A multi-value option that appends each occurrence to a std::vector<T>.

Storage type: std::vector<T>.

Template Parameters
TThe element type.
NameLong option name.
ShortNameShort option character, or '\0'.
NNargs descriptor. Default: nargs::one_or_more.

Definition at line 609 of file argument.hh.

◆ Option

template<class T, StringLiteral Name, char ShortName = '\0'>
using cli::Option = Arg<Name, ShortName, detail::ActionFor<T>::set_once>

A single-value option that stores the parsed result in T.

Storage type: T. Use operator bool() to check whether a value is present and provided() / occurrences() to check command-line occurrences.

Template Parameters
TThe value type to parse (e.g. int, std::string).
NameLong option name.
ShortNameShort option character, or '\0'.

Definition at line 594 of file argument.hh.

◆ PathOption

template<StringLiteral Name, char ShortName = '\0'>
using cli::PathOption = Option<std::filesystem::path, Name, ShortName>

Option<std::filesystem::path, Name, ShortName>.

Definition at line 638 of file argument.hh.

◆ Positional

template<class T, Nargs N = nargs::one>
using cli::Positional = PositionalImpl<N, detail::PositionalActionFor<T, N>::value>

A typed positional argument.

Storage type: T for N.max == 1, std::vector<T> for multi-value.

Template Parameters
TThe value type to parse.
NNargs descriptor. Default: nargs::one.

Definition at line 620 of file argument.hh.

◆ StringOption

template<StringLiteral Name, char ShortName = '\0'>
using cli::StringOption = Option<std::string, Name, ShortName>

Option<std::string, Name, ShortName>.

Definition at line 626 of file argument.hh.

Enumeration Type Documentation

◆ ColorMode

enum class cli::ColorMode
strong

Controls whether ANSI escape codes are emitted by format_help().

  • detect : emit codes only when stdout is connected to a TTY (default).
  • never : always produce plain text.
  • always : always emit ANSI codes regardless of terminal type.
Enumerator
detect 
never 
always 

Definition at line 20 of file color.hh.

◆ ErrorCode

enum class cli::ErrorCode
strong

Identifies the specific failure that occurred during parsing or validation.

Codes are grouped by category:

  • Parse errors (unknown_option … duplicate_argument): problems tokenising or dispatching command-line tokens.
  • Conversion/validation errors (conversion_error … validation_failed): problems converting a raw string to the target type or failing a constraint.
  • Informational codes (help_requested, exit_success): not true errors; the parser uses these to signal that the program should print help and exit 0.
Enumerator
unknown_option 

An unrecognised option name was encountered.

unknown_command 

An unrecognised subcommand name was encountered.

unexpected_argument 

A positional argument was supplied when none was expected.

missing_value 

An option that requires a value was given none.

invalid_value 

A value could not be interpreted as the required type.

duplicate_argument 

An option or positional that forbids repetition was seen more than once.

conversion_error 

Generic failure during string-to-type conversion.

out_of_range 

A numeric value fell outside the representable range of the target type.

missing_required 

A required option or positional was not provided.

mutually_exclusive 

Two mutually-exclusive options were both supplied.

dependency_missing 

An option that depends on another option was given without it.

invalid_choice 

A value was not among the allowed choices.

validation_failed 

A user-defined or built-in validation predicate rejected the value.

help_requested 

The --help flag (or equivalent) was seen; print help and exit 0.

exit_success 

The action pipeline requested a clean exit with code 0.

unknown_error 

Sentinel/default; indicates no error has been set.

Definition at line 20 of file error.hh.

◆ ErrorKind

enum class cli::ErrorKind
strong

Broad classification of an error for structured error handling.

  • parse : The error arose during tokenisation or dispatch.
  • conversion : The error arose while converting a string to a typed value.
  • validation : The error arose while validating a converted value.
Enumerator
parse 
conversion 
validation 

Definition at line 53 of file error.hh.

◆ Presence

enum class cli::Presence
strong

Controls whether an option or positional argument is mandatory.

Enumerator
required 
optional 

Definition at line 36 of file argument_base.hh.

◆ TokenType

enum class cli::TokenType : uint8_t
strong

Classifies a single token produced by tokenize().

  • option : An option name token (e.g. "--verbose", "-v").
  • value : A value token associated with the preceding option.
  • positional : A bare positional argument.
  • command : A subcommand name; subsequent tokens belong to the subcommand.
Enumerator
option 
value 
positional 
command 

Definition at line 144 of file parser.hh.

Function Documentation

◆ as_tuple() [1/2]

template<class T>
auto cli::as_tuple ( const T & t)
constexpr

Const overload of as_tuple for the fallback implementation.

Template Parameters
TAn aggregate type with up to 64 members.
Parameters
tThe const aggregate instance to decompose.
Returns
A std::tuple of const lvalue references to each field of t.

Definition at line 398 of file meta.hh.

◆ as_tuple() [2/2]

template<class T>
auto cli::as_tuple ( T & t)
constexpr

Converts an aggregate value to a std::tuple of references to its members.

Fallback implementation for compilers that do not support pack-expansion structured bindings. Uses aggregate_initializable<T>() to determine the field count at compile time and selects the corresponding explicit structured-binding branch (supports 0–64 members).

Template Parameters
TAn aggregate type with up to 64 members.
Parameters
tThe aggregate instance to decompose.
Returns
A std::tuple of lvalue references to each field of t.

Definition at line 125 of file meta.hh.

◆ conflicts()

auto cli::conflicts ( std::string_view left,
std::string_view right ) -> ConflictsRelation
nodiscardconstexpr

Definition at line 71 of file relation.hh.

◆ depends_on()

auto cli::depends_on ( std::string_view source,
std::string_view target ) -> DependsOnRelation
nodiscardconstexpr

Definition at line 77 of file relation.hh.

◆ fail()

template<class T>
auto cli::fail ( ParseError error) -> ActionResult< T >
nodiscardconstexpr

Helper function for constructing a failure ActionResult with type deduction.

Template Parameters
TThe value type of the ActionResult to construct.
Parameters
errorThe error to store in the failure result.
Returns
A failed ActionResult<T> carrying error.

Definition at line 218 of file action.hh.

◆ format_help()

template<ArgumentSpec T>
auto cli::format_help ( T & value,
std::string_view program_name = "program",
ColorMode color_mode = ColorMode::detect,
bool recurse = false,
HelpPalette palette = default_help_palette ) -> std::string

Generates a complete help string for an argument specification.

This is the primary public API for help text generation. It delegates to detail::format_help_impl with an empty command_path.

Template Parameters
TThe argument specification type (must satisfy ArgumentSpec).
Parameters
valueAn instance of the argument specification.
program_nameThe program name to display in the usage line. Default: "program".
color_modeControls ANSI color output. Default: ColorMode::detect.
recurseIf true, also append help for each subcommand. Default: false.
Returns
The formatted help string.

Definition at line 859 of file help.hh.

◆ group()

auto cli::group ( GroupParams params) -> GroupRelation
nodiscardconstexpr

Definition at line 67 of file relation.hh.

◆ has_prefix()

constexpr auto cli::has_prefix ( std::uint8_t p) -> bool
staticconstexpr

Definition at line 163 of file parser.hh.

◆ ignore()

template<class T>
auto cli::ignore ( ) -> ActionResult< T >
nodiscardconstexpr

Helper function for constructing an ignore ActionResult with type deduction.

Template Parameters
TThe value type of the ActionResult to construct.
Returns
An ignored ActionResult<T>.

Definition at line 230 of file action.hh.

◆ is_short_prefix()

constexpr auto cli::is_short_prefix ( std::uint8_t p) -> bool
staticconstexpr

Definition at line 167 of file parser.hh.

◆ long_prefix()

constexpr auto cli::long_prefix ( std::uint8_t i) -> std::uint8_t
staticconstexpr

Definition at line 159 of file parser.hh.

◆ no_prefix()

constexpr auto cli::no_prefix ( ) -> std::uint8_t
staticconstexpr

Definition at line 155 of file parser.hh.

◆ ok() [1/2]

auto cli::ok ( ) -> ActionResult< void >
nodiscardconstexpr

Helper function for constructing a successful ActionResult<void>.

Returns
A successful ActionResult<void>.

Definition at line 205 of file action.hh.

◆ ok() [2/2]

template<class T>
auto cli::ok ( T && value) -> ActionResult< std::remove_cvref_t< T > >
nodiscardconstexpr

Helper functions for constructing ActionResult objects with type deduction.

Template Parameters
TThe value type of the ActionResult to construct.
Parameters
valueThe value to store in a success result.
Returns
A successful ActionResult<T> holding value.

Definition at line 195 of file action.hh.

◆ operator|()

template<auto Fn, auto... Fns>
auto cli::operator| ( Action< Fns... > ,
Action< Fn >  )
constexpr

Definition at line 563 of file action.hh.

◆ parse()

template<ArgumentSpec T>
auto cli::parse ( int argc,
char * argv[] ) -> ParseResult< T >

Convenience free function that parses argc/argv into type T.

Constructs a default Parser<T> and delegates to Parser::parse.

Template Parameters
TThe argument specification type.
Parameters
argcArgument count (includes program name at index 0).
argvArgument vector.
Returns
A ParseResult<T> containing the parsed struct or an error.

Definition at line 1464 of file parser.hh.

◆ parse_or_exit() [1/3]

template<ArgumentSpec T>
auto cli::parse_or_exit ( int argc,
char * argv[] ) -> T

Parses argc/argv and exits the process on failure.

If parsing fails, writes the error message directly to stderr (or stdout for help/exit-success) using stdio and calls std::exit with the appropriate exit code. Include cli/parser_stdio.hh or cli/parser_iostream.hh when the output destination must be customized.

This is the typical one-liner entry point for command-line tools:

auto args = cli::parse_or_exit<MyArgs>(argc, argv);
auto parse_or_exit(int argc, char *argv[]) -> T
Parses argc/argv and exits the process on failure.
Definition parser.hh:1488
Template Parameters
TThe argument specification type.
Parameters
argcArgument count (includes program name at index 0).
argvArgument vector.
Returns
The parsed argument struct (never returns on failure).

Definition at line 1488 of file parser.hh.

◆ parse_or_exit() [2/3]

template<ArgumentSpec T>
auto cli::parse_or_exit ( int argc,
char * argv[],
FILE * out,
FILE * err ) -> T

Parses argc/argv and exits the process on failure.

This overload writes help/exit-success messages to out and errors to err using stdio. Include this header when the FILE destinations must be customized; parser.hh provides the simpler stdout/stderr fixed version.

Definition at line 19 of file parser_stdio.hh.

◆ parse_or_exit() [3/3]

template<ArgumentSpec T>
auto cli::parse_or_exit ( int argc,
char * argv[],
std::ostream & out,
std::ostream & err ) -> T

Parses argc/argv and exits the process on failure.

This overload writes help/exit-success messages to out and errors to err using iostreams. Include this header only when stream-based output customization is needed.

Definition at line 19 of file parser_iostream.hh.

◆ relations()

template<class... Relations>
auto cli::relations ( Relations... rels) -> RelationSet< Relations... >
nodiscardconstexpr

Definition at line 84 of file relation.hh.

◆ short_prefix()

constexpr auto cli::short_prefix ( ) -> std::uint8_t
staticconstexpr

Definition at line 157 of file parser.hh.

◆ StringLiteral()

template<std::size_t N>
cli::StringLiteral ( const char(&)[N]) -> StringLiteral< N >

Deduction guide: StringLiteral("hello") deduces StringLiteral<6>.

◆ to_string()

auto cli::to_string ( ErrorCode code) -> std::string_view
nodiscardconstexprnoexcept

Returns a human-readable description of an ErrorCode.

Parameters
codeThe error code to describe.
Returns
A non-owning string view of a static description string.

Definition at line 65 of file error.hh.

◆ token_prefix_view()

auto cli::token_prefix_view ( const Token & tok,
const TokenizerConfig & cfg ) -> std::string_view
inline

Retrieves the matched prefix string for an option token.

For a token with prefix == kNoPrefix, returns an empty view. For a short prefix, returns cfg.short_option_prefix. For a long prefix, returns the corresponding entry from cfg.option_prefixes.

Parameters
tokThe token whose prefix to retrieve; must be of type option.
cfgThe tokenizer configuration that was used to parse tok.
Returns
The matched prefix string as a view into cfg, or empty if no prefix.

Definition at line 376 of file parser.hh.

◆ tokenize()

template<class OptionLookup, class CommandLookup>
auto cli::tokenize ( std::span< const std::string_view > args,
OptionLookup option_nargs,
CommandLookup is_command,
const TokenizerConfig & cfg = {} ) -> TokenizeResult
inline

Converts a raw argument span into an ordered sequence of typed tokens.

This is a pure lexical operation; it does not access or modify any parsed result struct. It is used internally by Parser::parse_body but can also be called standalone for custom parsing scenarios.

Token sequence rules:

  • An option token ({option, "--opt", i}) is followed by zero or more {value, "...", j} tokens greedily consumed up to nargs.max.
  • --opt=val is split into {option, "--opt", i} + {value, "val", i} at the same position.
  • Bare tokens that match a command name produce {command, "cmd", i} and tokenization stops immediately; args[i+1:] are the subcommand's tokens.
  • After the end-of-options separator (--), all remaining tokens become {positional, "...", i} regardless of prefix.
Parameters
argsThe argument span to tokenize (typically argv[1:]).
option_nargsCallable that accepts a full option spelling (e.g. "--verbose", "-v") and returns its Nargs, or std::nullopt when unknown. Must NOT match command names.
is_commandCallable that accepts a bare token and returns whether it is a subcommand name.
cfgTokenizer configuration (prefixes, separator character, etc.).
Returns
A TokenizeResult containing the token list on success, or a ParseError on failure.

Definition at line 413 of file parser.hh.

Variable Documentation

◆ deduce_accepts_input

template<class Step, class Input>
bool cli::deduce_accepts_input
inlineconstexpr
Initial value:
=
std::invocable<const Step&, ActionCtx<void>, ActionResult<Input>>

Deduces accepts_input for a non-terminal step from its operator().

Evaluates to true when Step::operator() is callable with ActionCtx<void> and ActionResult<Input>, i.e. the step accepts a pipeline value of type Input. Use this to avoid writing the accepts_input trait by hand for conversion and validation steps:

struct MyStep {
template <class Input>
static constexpr bool accepts_input =
// ...
};
constexpr bool deduce_accepts_input
Deduces accepts_input for a non-terminal step from its operator().
Definition action.hh:588
Template Parameters
StepThe action step type.
InputThe pipeline value type to test.

Definition at line 588 of file action.hh.

◆ default_help_palette

HelpPalette cli::default_help_palette {}
inlineconstexpr

Definition at line 39 of file color.hh.

◆ kNoPrefix

std::uint8_t cli::kNoPrefix = 0xff
staticconstexpr

Definition at line 151 of file parser.hh.

◆ kPrefixIndexMask

std::uint8_t cli::kPrefixIndexMask = 0x7f
staticconstexpr

Definition at line 153 of file parser.hh.

◆ kShortPrefixBit

std::uint8_t cli::kShortPrefixBit = 0x80
staticconstexpr

Definition at line 152 of file parser.hh.

◆ optional

auto cli::optional = Presence::optional
inlineconstexpr

Convenience constant for Presence::optional.

Definition at line 42 of file argument_base.hh.

◆ recurse_help

RecurseHelpTag cli::recurse_help {}
inlineconstexpr

Convenience instance of RecurseHelpTag.

Definition at line 54 of file color.hh.

◆ required

auto cli::required = Presence::required
inlineconstexpr

Convenience constant for Presence::required.

Definition at line 39 of file argument_base.hh.

◆ token_stack_capacity

std::size_t cli::token_stack_capacity = CLI20_TOKEN_STACK_CAPACITY
inlineconstexpr

Number of tokenizer tokens kept in inline stack storage before falling back to heap allocation.

Define CLI20_TOKEN_STACK_CAPACITY before including cli/parser.hh to override the default. Because this value changes header-defined types, use one value consistently across all translation units in a program.

Definition at line 45 of file parser.hh.