koutil
Loading...
Searching...
No Matches
koutil::args::parser_t< Result > Class Template Reference

Command-line parser. More...

#include <parser.h>

Public Types

using result_t = Result
 
using command_t = command_t<result_t>
 
using option_t = option_t<result_t>
 
using argument_t = argument_t<result_t>
 

Public Member Functions

 parser_t (std::string_view name, std::string_view description)
 Constructs a parser with a name and description.
 
 parser_t (command_t cmd)
 Constructs a parser from command.
 
std::string_view name () const
 Gets the parser name.
 
std::string_view description () const
 Gets the parser description.
 
bool add_command (command_t &&command)
 Adds a subcommand.
 
bool add_option (const option_t &option)
 Adds an option to the parser.
 
void add_argument (const argument_t &argument)
 Adds a positional argument.
 
result_t parse (const char *const *args, std::uint32_t argc)
 Parses command-line arguments.
 
result_t parse (std::span< const char *const > args)
 Parses command-line arguments.
 
void show_help (std::ostream &out=std::cout, std::size_t terminal_size=80) const
 Displays help text.
 
void clear_used ()
 Clears state.
 

Private Attributes

std::string_view m_version
 
command_t m_main_command
 

Detailed Description

template<extends_result Result = result_base_t>
class koutil::args::parser_t< Result >

Command-line parser.

Template Parameters
ResultType of the result object. Must extend result_base_t. Defaults to result_base_t.

Member Typedef Documentation

◆ argument_t

template<extends_result Result = result_base_t>
using koutil::args::parser_t< Result >::argument_t = argument_t<result_t>

◆ command_t

template<extends_result Result = result_base_t>
using koutil::args::parser_t< Result >::command_t = command_t<result_t>

◆ option_t

template<extends_result Result = result_base_t>
using koutil::args::parser_t< Result >::option_t = option_t<result_t>

◆ result_t

template<extends_result Result = result_base_t>
using koutil::args::parser_t< Result >::result_t = Result

Constructor & Destructor Documentation

◆ parser_t() [1/2]

template<extends_result Result>
koutil::args::parser_t< Result >::parser_t ( std::string_view name,
std::string_view description )

Constructs a parser with a name and description.

Parameters
nameParser name.
descriptionParser description.

◆ parser_t() [2/2]

template<extends_result Result>
koutil::args::parser_t< Result >::parser_t ( command_t cmd)

Constructs a parser from command.

Parameters
mainCommand.

Member Function Documentation

◆ add_argument()

template<extends_result Result>
void koutil::args::parser_t< Result >::add_argument ( const argument_t & argument)

Adds a positional argument.

Parameters
argumentArgument to add.

◆ add_command()

template<extends_result Result>
bool koutil::args::parser_t< Result >::add_command ( command_t && command)

Adds a subcommand.

Parameters
commandCommand to add.
Returns
True if added successfully.

◆ add_option()

template<extends_result Result>
bool koutil::args::parser_t< Result >::add_option ( const option_t & option)

Adds an option to the parser.

Parameters
optionOption to add.
Returns
True if added successfully.

◆ clear_used()

template<extends_result Result = result_base_t>
void koutil::args::parser_t< Result >::clear_used ( )
inline

Clears state.

◆ description()

template<extends_result Result = result_base_t>
std::string_view koutil::args::parser_t< Result >::description ( ) const
inlinenodiscard

Gets the parser description.

Returns
Parser description

◆ name()

template<extends_result Result = result_base_t>
std::string_view koutil::args::parser_t< Result >::name ( ) const
inlinenodiscard

Gets the parser name.

Returns
Parser name

◆ parse() [1/2]

template<extends_result Result>
Result koutil::args::parser_t< Result >::parse ( const char *const * args,
std::uint32_t argc )

Parses command-line arguments.

Parameters
argsArgument array.
argcNumber of arguments.
Returns
Parsing result.

◆ parse() [2/2]

template<extends_result Result>
Result koutil::args::parser_t< Result >::parse ( std::span< const char *const > args)

Parses command-line arguments.

Parameters
argsArgument span.
Returns
Parsing result.

◆ show_help()

template<extends_result Result = result_base_t>
void koutil::args::parser_t< Result >::show_help ( std::ostream & out = std::cout,
std::size_t terminal_size = 80 ) const
inline

Displays help text.

Parameters
outOutput stream.
terminal_sizeWidth of the terminal.

Member Data Documentation

◆ m_main_command

template<extends_result Result = result_base_t>
command_t koutil::args::parser_t< Result >::m_main_command
private

◆ m_version

template<extends_result Result = result_base_t>
std::string_view koutil::args::parser_t< Result >::m_version
private

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