CPP-AP 2.7.0
Command-line argument parser for C++20
|
Main argument parser class. More...
#include <argument_parser.hpp>
Public Member Functions | |
argument_parser (const argument_parser &)=delete | |
argument_parser & | operator= (const argument_parser &)=delete |
argument_parser ()=default | |
argument_parser (argument_parser &&)=default | |
argument_parser & | operator= (argument_parser &&)=default |
~argument_parser ()=default | |
argument_parser & | program_name (std::string_view name) noexcept |
Set the program name. | |
argument_parser & | program_version (const version &version) noexcept |
Set the program version. | |
argument_parser & | program_version (std::string_view version) noexcept |
Set the program version. | |
argument_parser & | program_description (std::string_view description) noexcept |
Set the program description. | |
argument_parser & | verbose (const bool v=true) noexcept |
Set the verbosity mode. | |
template<detail::c_range_of< argument::default_positional > AR> | |
argument_parser & | default_positional_arguments (const AR &arg_discriminator_range) noexcept |
Set default positional arguments. | |
argument_parser & | default_positional_arguments (const std::initializer_list< argument::default_positional > arg_discriminator_list) noexcept |
Set default positional arguments. | |
template<detail::c_range_of< argument::default_optional > AR> | |
argument_parser & | default_optional_arguments (const AR &arg_discriminator_range) noexcept |
Set default optional arguments. | |
argument_parser & | default_optional_arguments (const std::initializer_list< argument::default_optional > arg_discriminator_list) noexcept |
Set default optional arguments. | |
template<detail::c_argument_value_type T = std::string> | |
argument::positional< T > & | add_positional_argument (const std::string_view primary_name) |
Adds a positional argument to the parser's configuration. | |
template<detail::c_argument_value_type T = std::string> | |
argument::positional< T > & | add_positional_argument (const std::string_view primary_name, const std::string_view secondary_name) |
Adds a positional argument to the parser's configuration. | |
template<detail::c_argument_value_type T = std::string> | |
argument::optional< T > & | add_optional_argument (const std::string_view name, const detail::argument_name_discriminator name_discr=n_primary) |
Adds a positional argument to the parser's configuration. | |
template<detail::c_argument_value_type T = std::string> | |
argument::optional< T > & | add_optional_argument (const std::string_view primary_name, const std::string_view secondary_name) |
Adds a positional argument to the parser's configuration. | |
template<bool StoreImplicitly = true> | |
argument::optional< bool > & | add_flag (const std::string_view name, const detail::argument_name_discriminator name_discr=n_primary) |
Adds a boolean flag argument (an optional argument with value_type = bool ) to the parser's configuration. | |
template<bool StoreImplicitly = true> | |
argument::optional< bool > & | add_flag (const std::string_view primary_name, const std::string_view secondary_name) |
Adds a boolean flag argument (an optional argument with value_type = bool ) to the parser's configuration. | |
void | parse_args (int argc, char *argv[]) |
Parses the command-line arguments. | |
template<detail::c_range_of< std::string, detail::type_validator::convertible > AR> | |
void | parse_args (const AR &argv_rng) |
Parses the command-line arguments. | |
void | try_parse_args (int argc, char *argv[]) |
Parses the command-line arguments and exits on error. | |
template<detail::c_range_of< std::string, detail::type_validator::convertible > AR> | |
void | try_parse_args (const AR &argv_rng) |
Parses the command-line arguments and exits on error. | |
std::vector< std::string > | parse_known_args (int argc, char *argv[]) |
Parses the known command-line arguments. | |
template<detail::c_range_of< std::string, detail::type_validator::convertible > AR> | |
std::vector< std::string > | parse_known_args (const AR &argv_rng) |
Parses the known command-line arguments. | |
std::vector< std::string > | try_parse_known_args (int argc, char *argv[]) |
Parses the known command-line arguments and exits on error. | |
template<detail::c_range_of< std::string, detail::type_validator::convertible > AR> | |
std::vector< std::string > | try_parse_known_args (const AR &argv_rng) |
Parses known the command-line arguments and exits on error. | |
void | handle_help_action () const noexcept |
Handles the help argument logic. | |
bool | has_value (std::string_view arg_name) const noexcept |
std::size_t | count (std::string_view arg_name) const noexcept |
template<detail::c_argument_value_type T = std::string> | |
T | value (std::string_view arg_name) const |
template<detail::c_argument_value_type T = std::string, std::convertible_to< T > U> | |
T | value_or (std::string_view arg_name, U &&fallback_value) const |
template<detail::c_argument_value_type T = std::string> | |
std::vector< T > | values (std::string_view arg_name) const |
void | print_config (const bool verbose, std::ostream &os=std::cout) const noexcept |
Prints the argument parser's details to an output stream. | |
Private Types | |
using | arg_ptr_t = std::unique_ptr< detail::argument_base > |
using | arg_ptr_list_t = std::vector< arg_ptr_t > |
using | arg_ptr_list_iter_t = typename arg_ptr_list_t::iterator |
using | arg_ptr_opt_t = detail::uptr_opt_t< detail::argument_base > |
using | arg_opt_t = std::optional< std::reference_wrapper< detail::argument_base > > |
using | const_arg_opt_t = std::optional< std::reference_wrapper< const detail::argument_base > > |
using | arg_token_list_t = std::vector< detail::argument_token > |
using | arg_token_list_iterator_t = typename arg_token_list_t::const_iterator |
Private Member Functions | |
void | _verify_arg_name_pattern (const std::string_view arg_name) const |
Verifies the pattern of an argument name and if it's invalid, an error is thrown. | |
auto | _name_match_predicate (const std::string_view arg_name, const detail::argument_name::match_type m_type=detail::argument_name::m_any) const noexcept |
Returns a unary predicate function which checks if the given name matches the argument's name. | |
auto | _name_match_predicate (const detail::argument_name &arg_name, const detail::argument_name::match_type m_type=detail::argument_name::m_any) const noexcept |
Returns a unary predicate function which checks if the given name matches the argument's name. | |
bool | _is_arg_name_used (const detail::argument_name &arg_name, const detail::argument_name::match_type m_type=detail::argument_name::m_any) const noexcept |
Check if an argument name is already used. | |
void | _validate_argument_configuration () const |
Validate whether the definition/configuration of the parser's arguments is correct. | |
template<detail::c_sized_range_of< std::string_view, detail::type_validator::convertible > AR> | |
arg_token_list_t | _tokenize (const AR &arg_range) |
Converts the command-line arguments into a list of tokens. | |
void | _tokenize_arg (arg_token_list_t &toks, const std::string_view arg_value) |
Appends an argument token(s) created from arg_value to the toks vector. | |
detail::argument_token | _build_token (const std::string_view arg_value) const noexcept |
Builds an argument token from the given value. | |
bool | _validate_flag_token (detail::argument_token &tok) noexcept |
Check if a flag token is valid based on its value. | |
std::vector< detail::argument_token > | _try_split_compound_flag (const detail::argument_token &tok) noexcept |
Tries to split a secondary flag token into separate flag token (one for each character of the token's value). | |
std::string | _unstripped_token_value (const detail::argument_token &tok) const noexcept |
Get the unstripped token value (including the flag prefix). | |
void | _parse_args_impl (const arg_token_list_t &arg_tokens, std::vector< std::string > &unknown_args, const bool handle_unknown=true) |
Implementation of parsing command-line arguments. | |
void | _parse_positional_args (arg_token_list_iterator_t &token_it, const arg_token_list_iterator_t &tokens_end) noexcept |
Parse positional arguments based on command-line input. | |
void | _parse_optional_args (arg_token_list_iterator_t &token_it, const arg_token_list_iterator_t &tokens_end, std::vector< std::string > &unknown_args, const bool handle_unknown=true) |
Parse optional arguments based on command-line input. | |
bool | _are_required_args_bypassed () const noexcept |
Check whether required argument bypassing is enabled. | |
void | _verify_required_args () const |
Check if all required positional and optional arguments are used. | |
void | _verify_nvalues () const |
Check if the number of argument values is within the specified range. | |
arg_opt_t | _get_argument (std::string_view arg_name) const noexcept |
Get the argument with the specified name. | |
arg_ptr_list_iter_t | _find_opt_arg (const detail::argument_token &flag_tok) noexcept |
Find an optional argument based on a flag token. | |
void | _print (std::ostream &os, const arg_ptr_list_t &args, const bool verbose) const noexcept |
Print the given argument list to an output stream. | |
Private Attributes | |
std::optional< std::string > | _program_name |
std::optional< std::string > | _program_version |
std::optional< std::string > | _program_description |
bool | _verbose = false |
arg_ptr_list_t | _positional_args |
arg_ptr_list_t | _optional_args |
Static Private Attributes | |
static constexpr uint8_t | _primary_flag_prefix_length = 2u |
static constexpr uint8_t | _secondary_flag_prefix_length = 1u |
static constexpr char | _flag_prefix_char = '-' |
static constexpr std::string_view | _flag_prefix = "--" |
static constexpr uint8_t | _indent_width = 2 |
Friends | |
std::ostream & | operator<< (std::ostream &os, const argument_parser &parser) noexcept |
Prints the argument parser's details to an output stream. | |
Main argument parser class.
Definition at line 45 of file argument_parser.hpp.
|
private |
Definition at line 641 of file argument_parser.hpp.
|
private |
Definition at line 639 of file argument_parser.hpp.
|
private |
Definition at line 638 of file argument_parser.hpp.
|
private |
Definition at line 640 of file argument_parser.hpp.
|
private |
Definition at line 637 of file argument_parser.hpp.
|
private |
Definition at line 645 of file argument_parser.hpp.
|
private |
Definition at line 644 of file argument_parser.hpp.
|
private |
Definition at line 642 of file argument_parser.hpp.
|
delete |
|
default |
|
default |
|
default |
|
inlineprivatenoexcept |
Check whether required argument bypassing is enabled.
Definition at line 985 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Builds an argument token from the given value.
arg_value | The command-line argument's value to be processed. |
Definition at line 800 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Find an optional argument based on a flag token.
flag_tok | An argument_token instance, the value of which will be used to find the argument. |
flag_tok.type
is not a valid flag token, then the end iterator will be returned. Definition at line 1055 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Get the argument with the specified name.
arg_name | The name of the argument. |
Definition at line 1033 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Check if an argument name is already used.
arg_name | The name of the argument. |
m_type | The match type used to find the argument. |
Definition at line 711 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Returns a unary predicate function which checks if the given name matches the argument's name.
arg_name | The name of the argument. |
m_type | The match type used within the predicate. |
Definition at line 696 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Returns a unary predicate function which checks if the given name matches the argument's name.
arg_name | The name of the argument. |
m_type | The match type used within the predicate. |
Definition at line 683 of file argument_parser.hpp.
|
inlineprivate |
Implementation of parsing command-line arguments.
arg_tokens | The list of command-line argument tokens. |
ap::parsing_failure |
c_range_of<argument_token>
instead of arg_token_list_t
directly. Definition at line 894 of file argument_parser.hpp.
|
inlineprivate |
Parse optional arguments based on command-line input.
token_it | Iterator for iterating through command-line argument tokens. |
tokens_end | The token list end iterator. |
unknown_args | Reference to the vector into which the dangling values shall be collected. |
ap::parsing_failure |
Definition at line 931 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Parse positional arguments based on command-line input.
token_it | Iterator for iterating through command-line argument tokens. |
tokens_end | The token list end iterator. |
Definition at line 909 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Print the given argument list to an output stream.
os | The output stream to print to. |
args | The argument list to print. |
Definition at line 1076 of file argument_parser.hpp.
|
inlineprivate |
Converts the command-line arguments into a list of tokens.
AR | The command-line argument value range type. |
arg_range | The command-line argument value range. |
Definition at line 755 of file argument_parser.hpp.
|
inlineprivate |
Appends an argument token(s) created from arg_value
to the toks
vector.
toks | The argument token list to which the processed token(s) will be appended. |
arg_value | The command-line argument's value to be processed. |
Definition at line 773 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Tries to split a secondary flag token into separate flag token (one for each character of the token's value).
tok | The token to be processed. |
Definition at line 841 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Get the unstripped token value (including the flag prefix).
Given an argument token, this function reconstructs and returns the original argument string, including any flag prefix that may have been stripped during tokenization.
tok | An argument token, the value of which will be processed. |
t_flag_primary
, returns the value prefixed with "--".t_flag_secondary
, returns the value prefixed with "-".Definition at line 876 of file argument_parser.hpp.
|
inlineprivate |
Validate whether the definition/configuration of the parser's arguments is correct.
What is verified:
Definition at line 732 of file argument_parser.hpp.
|
inlineprivatenoexcept |
Check if a flag token is valid based on its value.
arg
member of the token if an argument with the given name (token's value) is present. tok | The argument token to validate. |
Definition at line 826 of file argument_parser.hpp.
|
inlineprivate |
Verifies the pattern of an argument name and if it's invalid, an error is thrown.
ap::invalid_configuration |
Definition at line 651 of file argument_parser.hpp.
|
inlineprivate |
Check if the number of argument values is within the specified range.
ap::parsing_failure |
Definition at line 1017 of file argument_parser.hpp.
|
inlineprivate |
Check if all required positional and optional arguments are used.
ap::parsing_failure |
Definition at line 1002 of file argument_parser.hpp.
|
inline |
Adds a boolean flag argument (an optional argument with value_type = bool
) to the parser's configuration.
StoreImplicitly | A boolean value used as the implicit_value parameter of the argument. |
name | The primary name of the flag. |
name_discr | The discriminator value specifying whether the given name should be treated as primary or secondary. |
Definition at line 274 of file argument_parser.hpp.
|
inline |
Adds a boolean flag argument (an optional argument with value_type = bool
) to the parser's configuration.
StoreImplicitly | A boolean value used as the implicit_value parameter of the argument. |
primary_name | The primary name of the flag. |
secondary_name | The secondary name of the flag. |
Definition at line 292 of file argument_parser.hpp.
|
inline |
Adds a positional argument to the parser's configuration.
T | Type of the argument value. |
name | The name of the argument. |
name_discr | The discriminator value specifying whether the given name should be treated as primary or secondary. |
ap::invalid_configuration |
Definition at line 216 of file argument_parser.hpp.
|
inline |
Adds a positional argument to the parser's configuration.
T | Type of the argument value. |
primary_name | The primary name of the argument. |
secondary_name | The secondary name of the argument. |
ap::invalid_configuration |
Definition at line 248 of file argument_parser.hpp.
|
inline |
Adds a positional argument to the parser's configuration.
T | Type of the argument value. |
primary_name | The primary name of the argument. |
ap::invalid_configuration |
Definition at line 166 of file argument_parser.hpp.
|
inline |
Adds a positional argument to the parser's configuration.
T | Type of the argument value. |
primary_name | The primary name of the argument. |
secondary_name | The secondary name of the argument. |
ap::invalid_configuration |
Definition at line 188 of file argument_parser.hpp.
|
inlinenoexcept |
arg_name | The name of the argument. |
Definition at line 500 of file argument_parser.hpp.
|
inlinenoexcept |
Set default optional arguments.
AR | Type of the optional argument discriminator range. |
arg_discriminator_range | A range of default optional argument discriminators. |
Definition at line 139 of file argument_parser.hpp.
|
inlinenoexcept |
Set default optional arguments.
arg_discriminator_list | A list of default optional argument discriminators. |
Definition at line 150 of file argument_parser.hpp.
|
inlinenoexcept |
Set default positional arguments.
AR | Type of the positional argument discriminator range. |
arg_discriminator_range | A range of default positional argument discriminators. |
Definition at line 115 of file argument_parser.hpp.
|
inlinenoexcept |
Set default positional arguments.
arg_discriminator_list | A list of default positional argument discriminators. |
Definition at line 126 of file argument_parser.hpp.
|
inlinenoexcept |
Handles the help
argument logic.
Checks the value of the help
boolean flag argument and if the value is
true, prints the parser to std::cout
anb exists with EXIT_SUCCESS
status.
Definition at line 478 of file argument_parser.hpp.
|
inlinenoexcept |
arg_name | The name of the argument. |
Definition at line 491 of file argument_parser.hpp.
|
default |
|
delete |
|
inline |
Parses the command-line arguments.
AR | The argument range type. |
argv_rng | A range of command-line argument values. |
ap::invalid_configuration,ap::parsing_failure |
parse_args
function assumes that the program name argument has already been discarded. Definition at line 326 of file argument_parser.hpp.
|
inline |
Parses the command-line arguments.
argc | Number of command-line arguments. |
argv | Array of command-line argument values. |
ap::invalid_configuration,ap::parsing_failure |
Definition at line 314 of file argument_parser.hpp.
|
inline |
Parses the known command-line arguments.
add_positional_argument
add_optional_argument
add_flag
AR | The argument range type. |
argv_rng | A range of command-line argument values. |
ap::invalid_configuration,ap::parsing_failure |
parse_known_args
function assumes that the program name argument already been discarded. Definition at line 415 of file argument_parser.hpp.
|
inline |
Parses the known command-line arguments.
Equivalent to:
An argument is considered "known" if it was defined using the parser's argument declaraion methods:
add_positional_argument
add_optional_argument
add_flag
argc | Number of command-line arguments. |
argv | Array of command-line argument values. |
ap::invalid_configuration,ap::parsing_failure |
Definition at line 397 of file argument_parser.hpp.
|
inlinenoexcept |
Prints the argument parser's details to an output stream.
verbose | The verbosity mode value. |
os | Output stream. |
Definition at line 592 of file argument_parser.hpp.
|
inlinenoexcept |
Set the program description.
description | The description of the program. |
Definition at line 92 of file argument_parser.hpp.
|
inlinenoexcept |
Set the program name.
name | The name of the program. |
Definition at line 62 of file argument_parser.hpp.
|
inlinenoexcept |
Set the program version.
version | The version of the program. |
Definition at line 72 of file argument_parser.hpp.
|
inlinenoexcept |
Set the program version.
version | The version of the program. |
Definition at line 82 of file argument_parser.hpp.
|
inline |
Parses the command-line arguments and exits on error.
Calls parse_args(argv_rng)
in a try-catch block. If an error is thrown, then its message and the parser are printed to std::cerr
and the function exists with EXIT_FAILURE
status.
AR | The argument range type. |
argv_rng | A range of command-line argument values. |
try_parse_args
function assumes that the program name argument has already been discarded. Definition at line 369 of file argument_parser.hpp.
|
inline |
Parses the command-line arguments and exits on error.
Equivalent to:
argc | Number of command-line arguments. |
argv | Array of command-line argument values. |
Definition at line 353 of file argument_parser.hpp.
|
inline |
Parses known the command-line arguments and exits on error.
Calls parse_known_args(argv_rng)
in a try-catch block. If an error is thrown, then its message and the parser are printed to std::cerr
and the function exists with EXIT_FAILURE
status. Otherwise the result of parse_known_args(argv_rng)
is returned.
AR | The argument range type. |
argv_rng | A range of command-line argument values. |
try_parse_known_args
function assumes that the program name argument has already been discarded. Definition at line 459 of file argument_parser.hpp.
|
inline |
Parses the known command-line arguments and exits on error.
Equivalent to:
argc | Number of command-line arguments. |
argv | Array of command-line argument values. |
Definition at line 442 of file argument_parser.hpp.
|
inline |
T | Type of the argument value. |
arg_name | The name of the argument. |
ap::lookup_failure,ap::type_error |
Definition at line 512 of file argument_parser.hpp.
|
inline |
T | Type of the argument value. |
U | The default value type. |
arg_name | The name of the argument. |
fallback_value | The fallback value. |
ap::lookup_failure,ap::type_error |
Definition at line 535 of file argument_parser.hpp.
|
inline |
T | Type of the argument values. |
arg_name | The name of the argument. |
ap::lookup_failure,ap::type_error |
Definition at line 561 of file argument_parser.hpp.
|
inlinenoexcept |
Set the verbosity mode.
false
. v | The verbosity mode value. |
Definition at line 103 of file argument_parser.hpp.
|
friend |
Prints the argument parser's details to an output stream.
An os << parser
operation is equivalent to a parser.print_config(_verbose, os)
call, where _verbose
is the inner verbosity mode, which can be set with the verbose function.
os | Output stream. |
parser | The argument parser to print. |
Definition at line 626 of file argument_parser.hpp.
|
staticconstexprprivate |
Definition at line 1113 of file argument_parser.hpp.
|
staticconstexprprivate |
Definition at line 1112 of file argument_parser.hpp.
|
staticconstexprprivate |
Definition at line 1114 of file argument_parser.hpp.
|
private |
Definition at line 1108 of file argument_parser.hpp.
|
private |
Definition at line 1107 of file argument_parser.hpp.
|
staticconstexprprivate |
Definition at line 1110 of file argument_parser.hpp.
|
private |
Definition at line 1104 of file argument_parser.hpp.
|
private |
Definition at line 1102 of file argument_parser.hpp.
|
private |
Definition at line 1103 of file argument_parser.hpp.
|
staticconstexprprivate |
Definition at line 1111 of file argument_parser.hpp.
|
private |
Definition at line 1105 of file argument_parser.hpp.