cli20 0.1.2
A C++20 typed-schema command-line parser
Loading...
Searching...
No Matches
cli::Parser< T > Struct Template Reference

The main parsing engine for a typed argument specification. More...

#include <parser.hh>

Public Member Functions

auto parse (int argc, char *argv[]) -> ParseResult< T >
 Parses standard C-style argc/argv, using argv[0] as the program name.
auto parse (T initial, int argc, char *argv[]) -> ParseResult< T >
 Parses argc/argv starting from a pre-initialised argument struct.
auto parse (std::span< const std::string_view > args, std::size_t first_index=0) -> ParseResult< T >
 Parses a span of string views, skipping first_index elements.
auto parse (std::span< const std::string_view > args, std::string_view program_name, std::size_t first_index=0) -> ParseResult< T >
 Parses a span with an explicit program name for help output.
auto parse (T initial, std::span< const std::string_view > args, std::size_t first_index=0) -> ParseResult< T >
 Parses a span starting from a pre-initialised argument struct.
auto parse (T initial, std::span< const std::string_view > args, std::string_view program_name, std::size_t first_index=0) -> ParseResult< T >
 Parses a span with a pre-initialised struct and explicit program name.
auto program_name () const -> const std::string &
 Get the program name used in help output.
auto set_program_name (std::string name) -> void
 Set the program name used in help output.
auto format_help (ColorMode color_mode=ColorMode::detect, HelpPalette palette=default_help_palette) -> std::string
 Generates a help string for the argument specification.
auto format_help (RecurseHelpTag) -> std::string
 Generates a help string including recursive subcommand help.
auto format_help (HelpPalette palette) -> std::string
auto format_help (ColorMode color_mode, RecurseHelpTag) -> std::string
 Generates a help string with explicit color mode and recursive subcommand help.
auto format_help (ColorMode color_mode, HelpPalette palette, RecurseHelpTag) -> std::string

Friends

template<ArgumentSpec>
struct Parser

Detailed Description

template<ArgumentSpec T>
struct cli::Parser< T >

The main parsing engine for a typed argument specification.

Parser<T> owns a TokenizerConfig and the program name used in help output. It tokenizes the argument list, dispatches each token to the appropriate field of T, validates required arguments, and fires on_parse callbacks.

Entry points:

  • parse(argc, argv) — standard C-style interface; sets program name from argv[0].
  • parse(span, first_index) — span-based interface with optional offset.
  • parse(initial, ...) — pre-initialised variants; useful for BoundOption / on_parse.

On Windows, use cli::utf16_to_utf8(argc, argv) in wmain and pass the converted UTF-8 argv to the standard char* overload.

Template Parameters
TThe argument specification type (must satisfy ArgumentSpec).

Definition at line 736 of file parser.hh.

Member Function Documentation

◆ format_help() [1/5]

template<ArgumentSpec T>
auto cli::Parser< T >::format_help ( ColorMode color_mode,
HelpPalette palette,
RecurseHelpTag  ) -> std::string
inline

Definition at line 1009 of file parser.hh.

◆ format_help() [2/5]

template<ArgumentSpec T>
auto cli::Parser< T >::format_help ( ColorMode color_mode,
RecurseHelpTag  ) -> std::string
inline

Generates a help string with explicit color mode and recursive subcommand help.

Parameters
color_modeControls ANSI color output.
Returns
The formatted help string with all subcommand sections appended.

Definition at line 1005 of file parser.hh.

◆ format_help() [3/5]

template<ArgumentSpec T>
auto cli::Parser< T >::format_help ( ColorMode color_mode = ColorMode::detect,
HelpPalette palette = default_help_palette ) -> std::string
inline

Generates a help string for the argument specification.

Parameters
color_modeControls ANSI color output. Default: ColorMode::detect.
Returns
The formatted help string.

Definition at line 979 of file parser.hh.

◆ format_help() [4/5]

template<ArgumentSpec T>
auto cli::Parser< T >::format_help ( HelpPalette palette) -> std::string
inline

Definition at line 994 of file parser.hh.

◆ format_help() [5/5]

template<ArgumentSpec T>
auto cli::Parser< T >::format_help ( RecurseHelpTag ) -> std::string
inline

Generates a help string including recursive subcommand help.

Returns
The formatted help string with all subcommand sections appended.

Definition at line 990 of file parser.hh.

◆ parse() [1/6]

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

Parses standard C-style argc/argv, using argv[0] as the program name.

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 749 of file parser.hh.

◆ parse() [2/6]

template<ArgumentSpec T>
auto cli::Parser< T >::parse ( std::span< const std::string_view > args,
std::size_t first_index = 0 ) -> ParseResult< T >
inline

Parses a span of string views, skipping first_index elements.

Parameters
argsThe full argument span (may include program name at index 0).
first_indexIndex of the first argument to parse (default: 0).
Returns
A ParseResult<T> containing the parsed struct or an error.

Definition at line 794 of file parser.hh.

◆ parse() [3/6]

template<ArgumentSpec T>
auto cli::Parser< T >::parse ( std::span< const std::string_view > args,
std::string_view program_name,
std::size_t first_index = 0 ) -> ParseResult< T >
inline

Parses a span with an explicit program name for help output.

Parameters
argsThe full argument span.
program_nameThe name to display in usage/help text.
first_indexIndex of the first argument to parse (default: 0).
Returns
A ParseResult<T> containing the parsed struct or an error.

Definition at line 809 of file parser.hh.

◆ parse() [4/6]

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

Parses argc/argv starting from a pre-initialised argument struct.

Useful when fields of T need to be configured before parsing (e.g. BoundOption pointer targets or on_parse callbacks set on the struct).

Parameters
initialThe pre-initialised argument struct.
argcArgument count (includes program name at index 0).
argvArgument vector.
Returns
A ParseResult<T> containing the parsed struct or an error.

Definition at line 773 of file parser.hh.

◆ parse() [5/6]

template<ArgumentSpec T>
auto cli::Parser< T >::parse ( T initial,
std::span< const std::string_view > args,
std::size_t first_index = 0 ) -> ParseResult< T >
inline

Parses a span starting from a pre-initialised argument struct.

Useful with BoundOption / on_parse so that pointer targets and callbacks set before this call are preserved.

Parameters
initialThe pre-initialised argument struct.
argsThe full argument span.
first_indexIndex of the first argument to parse (default: 0).
Returns
A ParseResult<T> containing the parsed struct or an error.

Definition at line 829 of file parser.hh.

◆ parse() [6/6]

template<ArgumentSpec T>
auto cli::Parser< T >::parse ( T initial,
std::span< const std::string_view > args,
std::string_view program_name,
std::size_t first_index = 0 ) -> ParseResult< T >
inline

Parses a span with a pre-initialised struct and explicit program name.

Parameters
initialThe pre-initialised argument struct.
argsThe full argument span.
program_nameThe name to display in usage/help text.
first_indexIndex of the first argument to parse (default: 0).
Returns
A ParseResult<T> containing the parsed struct or an error.

Definition at line 847 of file parser.hh.

◆ program_name()

template<ArgumentSpec T>
auto cli::Parser< T >::program_name ( ) const -> const std::string &
inlinenodiscard

Get the program name used in help output.

Returns
The program name string.

Definition at line 861 of file parser.hh.

◆ set_program_name()

template<ArgumentSpec T>
auto cli::Parser< T >::set_program_name ( std::string name) -> void
inline

Set the program name used in help output.

Parameters
nameThe program name string to set.

Definition at line 869 of file parser.hh.

◆ Parser

template<ArgumentSpec T>
template<ArgumentSpec>
friend struct Parser
friend

Definition at line 738 of file parser.hh.


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