CPP-AP 2.2.6
Command-line argument parser for C++20
Loading...
Searching...
No Matches
ap::argument_parser Class Reference

Main argument parser class. More...

#include <argument_parser.hpp>

Public Member Functions

 argument_parser (const argument_parser &)=delete
 
argument_parseroperator= (const argument_parser &)=delete
 
 argument_parser (argument_parser &&)=default
 
argument_parseroperator= (argument_parser &&)=default
 
argument_parserprogram_name (std::string_view name) noexcept
 Set the program name.
 
argument_parserprogram_description (std::string_view description) noexcept
 Set the program description.
 
argument_parserverbose (const bool v=true) noexcept
 Set the verbosity mode.
 
template<detail::c_range_of< argument::default_positional > AR>
argument_parserdefault_positional_arguments (const AR &arg_discriminator_range) noexcept
 Set default positional arguments.
 
argument_parserdefault_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_parserdefault_optional_arguments (const AR &arg_discriminator_range) noexcept
 Set default optional arguments.
 
argument_parserdefault_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 (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 (std::string_view primary_name, 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 (std::string_view primary_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 (std::string_view primary_name, std::string_view secondary_name)
 Adds a positional argument to the parser's configuration.
 
template<bool StoreImplicitly = true>
argument::optional< bool > & add_flag (std::string_view primary_name)
 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 (std::string_view primary_name, 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)
 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)
 Parses 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>
value (std::string_view arg_name) const
 
template<detail::c_argument_value_type T = std::string, std::convertible_to< T > U>
value_or (std::string_view arg_name, U &&default_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.
 

Friends

std::ostream & operator<< (std::ostream &os, const argument_parser &parser) noexcept
 Prints the argument parser's details to an output stream.
 

Detailed Description

Main argument parser class.

Definition at line 45 of file argument_parser.hpp.

Member Function Documentation

◆ add_flag() [1/2]

template<bool StoreImplicitly = true>
argument::optional< bool > & ap::argument_parser::add_flag ( std::string_view  primary_name)
inline

Adds a boolean flag argument (an optional argument with value_type = bool) to the parser's configuration.

Template Parameters
StoreImplicitlyA boolean value used as the implicit_value parameter of the argument.
Parameters
primary_nameThe primary name of the flag.
Returns
Reference to the added boolean flag argument.

Definition at line 235 of file argument_parser.hpp.

◆ add_flag() [2/2]

template<bool StoreImplicitly = true>
argument::optional< bool > & ap::argument_parser::add_flag ( std::string_view  primary_name,
std::string_view  secondary_name 
)
inline

Adds a boolean flag argument (an optional argument with value_type = bool) to the parser's configuration.

Template Parameters
StoreImplicitlyA boolean value used as the implicit_value parameter of the argument.
Parameters
primary_nameThe primary name of the flag.
secondary_nameThe secondary name of the flag.
Returns
Reference to the added boolean flag argument.

Definition at line 250 of file argument_parser.hpp.

◆ add_optional_argument() [1/2]

template<detail::c_argument_value_type T = std::string>
argument::optional< T > & ap::argument_parser::add_optional_argument ( std::string_view  primary_name)
inline

Adds a positional argument to the parser's configuration.

Template Parameters
TType of the argument value.
Parameters
primary_nameThe primary name of the argument.
Returns
Reference to the added optional argument.
Exceptions
ap::invalid_configuration
Todo:
Check forbidden characters (after adding the assignment character).

Definition at line 192 of file argument_parser.hpp.

◆ add_optional_argument() [2/2]

template<detail::c_argument_value_type T = std::string>
argument::optional< T > & ap::argument_parser::add_optional_argument ( std::string_view  primary_name,
std::string_view  secondary_name 
)
inline

Adds a positional argument to the parser's configuration.

Template Parameters
TType of the argument value.
Parameters
primary_nameThe primary name of the argument.
secondary_nameThe secondary name of the argument.
Returns
Reference to the added optional argument.
Exceptions
ap::invalid_configuration
Todo:
Check forbidden characters (after adding the assignment character).

Definition at line 214 of file argument_parser.hpp.

◆ add_positional_argument() [1/2]

template<detail::c_argument_value_type T = std::string>
argument::positional< T > & ap::argument_parser::add_positional_argument ( std::string_view  primary_name)
inline

Adds a positional argument to the parser's configuration.

Template Parameters
TType of the argument value.
Parameters
primary_nameThe primary name of the argument.
Returns
Reference to the added positional argument.
Exceptions
ap::invalid_configuration
Todo:
Check forbidden characters (after adding the assignment character).

Definition at line 146 of file argument_parser.hpp.

◆ add_positional_argument() [2/2]

template<detail::c_argument_value_type T = std::string>
argument::positional< T > & ap::argument_parser::add_positional_argument ( std::string_view  primary_name,
std::string_view  secondary_name 
)
inline

Adds a positional argument to the parser's configuration.

Template Parameters
TType of the argument value.
Parameters
primary_nameThe primary name of the argument.
secondary_nameThe secondary name of the argument.
Returns
Reference to the added positional argument.
Exceptions
ap::invalid_configuration
Todo:
Check forbidden characters (after adding the assignment character).

Definition at line 168 of file argument_parser.hpp.

◆ count()

std::size_t ap::argument_parser::count ( std::string_view  arg_name) const
inlinenoexcept
Parameters
arg_nameThe name of the argument.
Returns
The count of times the argument has been used.

Definition at line 346 of file argument_parser.hpp.

◆ default_optional_arguments() [1/2]

template<detail::c_range_of< argument::default_optional > AR>
argument_parser & ap::argument_parser::default_optional_arguments ( const AR &  arg_discriminator_range)
inlinenoexcept

Set default optional arguments.

Template Parameters
ARType of the optional argument discriminator range.
Parameters
arg_discriminator_rangeA range of default optional argument discriminators.
Returns
Reference to the argument parser.

Definition at line 119 of file argument_parser.hpp.

◆ default_optional_arguments() [2/2]

argument_parser & ap::argument_parser::default_optional_arguments ( const std::initializer_list< argument::default_optional arg_discriminator_list)
inlinenoexcept

Set default optional arguments.

Parameters
arg_discriminator_listA list of default optional argument discriminators.
Returns
Reference to the argument parser.

Definition at line 130 of file argument_parser.hpp.

◆ default_positional_arguments() [1/2]

template<detail::c_range_of< argument::default_positional > AR>
argument_parser & ap::argument_parser::default_positional_arguments ( const AR &  arg_discriminator_range)
inlinenoexcept

Set default positional arguments.

Template Parameters
ARType of the positional argument discriminator range.
Parameters
arg_discriminator_rangeA range of default positional argument discriminators.
Returns
Reference to the argument parser.

Definition at line 95 of file argument_parser.hpp.

◆ default_positional_arguments() [2/2]

argument_parser & ap::argument_parser::default_positional_arguments ( const std::initializer_list< argument::default_positional arg_discriminator_list)
inlinenoexcept

Set default positional arguments.

Parameters
arg_discriminator_listA list of default positional argument discriminators.
Returns
Reference to the argument parser.

Definition at line 106 of file argument_parser.hpp.

◆ handle_help_action()

void ap::argument_parser::handle_help_action ( ) const
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 324 of file argument_parser.hpp.

◆ has_value()

bool ap::argument_parser::has_value ( std::string_view  arg_name) const
inlinenoexcept
Parameters
arg_nameThe name of the argument.
Returns
True if the argument has a value, false otherwise.

Definition at line 337 of file argument_parser.hpp.

◆ parse_args() [1/2]

template<detail::c_range_of< std::string, detail::type_validator::convertible > AR>
void ap::argument_parser::parse_args ( const AR &  argv)
inline

Parses the command-line arguments.

Template Parameters
ARThe argument range type.
Parameters
argvA range of command-line argument values.

Definition at line 275 of file argument_parser.hpp.

◆ parse_args() [2/2]

void ap::argument_parser::parse_args ( int  argc,
char *  argv[] 
)
inline

Parses the command-line arguments.

Parameters
argcNumber of command-line arguments.
argvArray of command-line argument values.
Note
The first argument (the program name) is ignored.

Definition at line 265 of file argument_parser.hpp.

◆ print_config()

void ap::argument_parser::print_config ( const bool  verbose,
std::ostream &  os = std::cout 
) const
inlinenoexcept

Prints the argument parser's details to an output stream.

Parameters
verboseThe verbosity mode value.
osOutput stream.

Definition at line 438 of file argument_parser.hpp.

◆ program_description()

argument_parser & ap::argument_parser::program_description ( std::string_view  description)
inlinenoexcept

Set the program description.

Parameters
descriptionThe description of the program.
Returns
Reference to the argument parser.

Definition at line 72 of file argument_parser.hpp.

◆ program_name()

argument_parser & ap::argument_parser::program_name ( std::string_view  name)
inlinenoexcept

Set the program name.

Parameters
nameThe name of the program.
Returns
Reference to the argument parser.

Definition at line 62 of file argument_parser.hpp.

◆ try_parse_args() [1/2]

template<detail::c_range_of< std::string, detail::type_validator::convertible > AR>
void ap::argument_parser::try_parse_args ( const AR &  argv)
inline

Parses the command-line arguments and exits on error.

Calls parse_args(argv) 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.

Template Parameters
ARThe argument range type.
Parameters
argvA range of command-line argument values.

Definition at line 306 of file argument_parser.hpp.

◆ try_parse_args() [2/2]

void ap::argument_parser::try_parse_args ( int  argc,
char *  argv[] 
)
inline

Parses the command-line arguments and exits on error.

Parameters
argcNumber of command-line arguments.
argvArray of command-line argument values.
Note
The first argument (the program name) is ignored.

Definition at line 291 of file argument_parser.hpp.

◆ value()

template<detail::c_argument_value_type T = std::string>
T ap::argument_parser::value ( std::string_view  arg_name) const
inline
Template Parameters
TType of the argument value.
Parameters
arg_nameThe name of the argument.
Returns
The value of the argument.
Exceptions
ap::lookup_failure,ap::type_error

Definition at line 358 of file argument_parser.hpp.

◆ value_or()

template<detail::c_argument_value_type T = std::string, std::convertible_to< T > U>
T ap::argument_parser::value_or ( std::string_view  arg_name,
U &&  default_value 
) const
inline
Template Parameters
TType of the argument value.
UThe default value type.
Parameters
arg_nameThe name of the argument.
default_valueThe default value.
Returns
The value of the argument.
Exceptions
ap::lookup_failure,ap::type_error

Definition at line 381 of file argument_parser.hpp.

◆ values()

template<detail::c_argument_value_type T = std::string>
std::vector< T > ap::argument_parser::values ( std::string_view  arg_name) const
inline
Template Parameters
TType of the argument values.
Parameters
arg_nameThe name of the argument.
Returns
The values of the argument as a vector.
Exceptions
ap::lookup_failure,ap::type_error

Definition at line 407 of file argument_parser.hpp.

◆ verbose()

argument_parser & ap::argument_parser::verbose ( const bool  v = true)
inlinenoexcept

Set the verbosity mode.

Note
The default verbosity mode value is false.
Parameters
vThe verbosity mode value.
Returns
Reference to the argument parser.

Definition at line 83 of file argument_parser.hpp.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
const argument_parser parser 
)
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.

Parameters
osOutput stream.
parserThe argument parser to print.
Returns
The modified output stream.

Definition at line 468 of file argument_parser.hpp.


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