|
cli20 0.1.2
A C++20 typed-schema command-line parser
|
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 |
| 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.
| Name | Long option name without the leading --. |
| Args | Configuration parameters: [ShortName,] Action [, Nargs]. |
Definition at line 526 of file argument.hh.
| using cli::BoundDoubleOption = BoundOption<double, Name, ShortName> |
BoundOption specialisation for double.
Definition at line 565 of file argument.hh.
| using cli::BoundIntOption = BoundOption<int, Name, ShortName> |
BoundOption specialisation for int.
Definition at line 561 of file argument.hh.
| 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*.
| T | The value type of the external variable. |
| Name | Long option name. |
| ShortName | Short option character, or '\0'. |
Definition at line 553 of file argument.hh.
| using cli::BoundPathOption = BoundOption<std::filesystem::path, Name, ShortName> |
BoundOption specialisation for std::filesystem::path.
Definition at line 569 of file argument.hh.
| using cli::BoundStringOption = BoundOption<std::string, Name, ShortName> |
BoundOption specialisation for std::string.
Definition at line 557 of file argument.hh.
| using cli::deduce_after_type |
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.
| Step | The action step type. |
| Input | The pipeline value type. |
| using cli::DoubleOption = Option<double, Name, ShortName> |
Option<double, Name, ShortName>.
Definition at line 634 of file argument.hh.
| 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.
| Name | Long option name (e.g. "verbose"). |
| ShortName | Short option character (e.g. 'v'), or '\0' for none. |
Definition at line 537 of file argument.hh.
| using cli::Help |
A built-in help flag that prints help and exits with code 0.
Default names: --help / -h. Override via template parameters.
| Name | Long option name. Default: "help". |
| ShortName | Short option character. Default: 'h'. |
Definition at line 580 of file argument.hh.
| using cli::IntOption = Option<int, Name, ShortName> |
Option<int, Name, ShortName>.
Definition at line 630 of file argument.hh.
| 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>.
| T | The element type. |
| Name | Long option name. |
| ShortName | Short option character, or '\0'. |
| N | Nargs descriptor. Default: nargs::one_or_more. |
Definition at line 609 of file argument.hh.
| 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.
| T | The value type to parse (e.g. int, std::string). |
| Name | Long option name. |
| ShortName | Short option character, or '\0'. |
Definition at line 594 of file argument.hh.
| using cli::PathOption = Option<std::filesystem::path, Name, ShortName> |
Option<std::filesystem::path, Name, ShortName>.
Definition at line 638 of file argument.hh.
| 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.
| T | The value type to parse. |
| N | Nargs descriptor. Default: nargs::one. |
Definition at line 620 of file argument.hh.
| using cli::StringOption = Option<std::string, Name, ShortName> |
Option<std::string, Name, ShortName>.
Definition at line 626 of file argument.hh.
|
strong |
Controls whether ANSI escape codes are emitted by format_help().
| Enumerator | |
|---|---|
| detect | |
| never | |
| always | |
|
strong |
Identifies the specific failure that occurred during parsing or validation.
Codes are grouped by category:
|
strong |
Broad classification of an error for structured error handling.
| Enumerator | |
|---|---|
| parse | |
| conversion | |
| validation | |
|
strong |
Controls whether an option or positional argument is mandatory.
| Enumerator | |
|---|---|
| required | |
| optional | |
Definition at line 36 of file argument_base.hh.
|
strong |
Classifies a single token produced by tokenize().
| Enumerator | |
|---|---|
| option | |
| value | |
| positional | |
| command | |
|
constexpr |
|
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).
| T | An aggregate type with up to 64 members. |
| t | The aggregate instance to decompose. |
|
nodiscardconstexpr |
Definition at line 71 of file relation.hh.
|
nodiscardconstexpr |
Definition at line 77 of file relation.hh.
|
nodiscardconstexpr |
Helper function for constructing a failure ActionResult with type deduction.
| T | The value type of the ActionResult to construct. |
| error | The error to store in the failure result. |
| 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.
| T | The argument specification type (must satisfy ArgumentSpec). |
| value | An instance of the argument specification. |
| program_name | The program name to display in the usage line. Default: "program". |
| color_mode | Controls ANSI color output. Default: ColorMode::detect. |
| recurse | If true, also append help for each subcommand. Default: false. |
|
nodiscardconstexpr |
Definition at line 67 of file relation.hh.
|
staticconstexpr |
|
nodiscardconstexpr |
Helper function for constructing an ignore ActionResult with type deduction.
| T | The value type of the ActionResult to construct. |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
nodiscardconstexpr |
Helper function for constructing a successful ActionResult<void>.
|
nodiscardconstexpr |
Helper functions for constructing ActionResult objects with type deduction.
| T | The value type of the ActionResult to construct. |
| value | The value to store in a success result. |
| 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.
| T | The argument specification type. |
| argc | Argument count (includes program name at index 0). |
| argv | Argument vector. |
| 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:
| T | The argument specification type. |
| argc | Argument count (includes program name at index 0). |
| argv | Argument vector. |
| 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.
| 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.
|
nodiscardconstexpr |
Definition at line 84 of file relation.hh.
|
staticconstexpr |
| cli::StringLiteral | ( | const | char(&)[N] | ) | -> StringLiteral< N > |
Deduction guide: StringLiteral("hello") deduces StringLiteral<6>.
|
nodiscardconstexprnoexcept |
|
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.
| tok | The token whose prefix to retrieve; must be of type option. |
| cfg | The tokenizer configuration that was used to parse tok. |
|
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:
| args | The argument span to tokenize (typically argv[1:]). |
| option_nargs | Callable 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_command | Callable that accepts a bare token and returns whether it is a subcommand name. |
| cfg | Tokenizer configuration (prefixes, separator character, etc.). |
|
inlineconstexpr |
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:
| Step | The action step type. |
| Input | The pipeline value type to test. |
|
inlineconstexpr |
|
inlineconstexpr |
Convenience constant for Presence::optional.
Definition at line 42 of file argument_base.hh.
|
inlineconstexpr |
Convenience instance of RecurseHelpTag.
|
inlineconstexpr |
Convenience constant for Presence::required.
Definition at line 39 of file argument_base.hh.
|
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.