ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
cli::CliContext Class Reference

Represents the context of a command-line interface (CLI) invocation and as such contains the parsed values (if present for all Arguments) More...

#include <cli_context.h>

Public Member Functions

 CliContext (std::unique_ptr< std::unordered_map< std::string, std::any > > posArgs, std::unique_ptr< std::unordered_map< std::string, std::any > > optArgs, std::unique_ptr< std::unordered_set< std::string > > flagArgs, cli::logging::AbstractLogger &logger)
 Constructs a new CliContext object from the passed argument maps.
 
 CliContext (const CliContext &)=delete
 
CliContextoperator= (const CliContext &)=delete
 
bool isArgPresent (const std::string &argName) const
 Checks if an argument with the given name is present in the context.
 
bool isOptionArgPresent (const std::string &argName) const
 Checks if an optional argument with the given name is present in the context.
 
bool isPositionalArgPresent (const std::string &argName) const
 Checks if a positional argument with the given name is present in the context.
 
bool isFlagPresent (const std::string &argName) const
 Checks if a flag with the given name is present in the context.
 
template<typename T >
getPositionalArg (const std::string &argName) const
 Gets the value of a positional argument.
 
template<typename T >
void getPositionalArg (const std::string &argName, T &out) const
 Gets the value of a positional argument and stores it in the provided output variable.
 
template<typename T >
getOptionArg (const std::string &argName) const
 Gets the value of an optional argument.
 
template<typename T >
void getOptionArg (const std::string &argName, T &out) const
 Gets the value of an optional argument and stores it in the provided output variable.
 
template<typename T >
std::vector< T > getRepeatableOptionArg (const std::string &argName) const
 Gets all values of a repeatable option argument.
 
template<typename T >
std::vector< T > getRepeatablePositionalArg (const std::string &argName) const
 Gets all values of a repeatable positional argument.
 
template<typename T >
getArg (const std::string &argName) const
 Gets the value of an argument.
 
template<typename T >
auto getRepeatableArg (const std::string &argName) const
 Gets all values of a repeatable argument.
 
logging::AbstractLoggerLogger () const
 

Detailed Description

Represents the context of a command-line interface (CLI) invocation and as such contains the parsed values (if present for all Arguments)

Constructor & Destructor Documentation

◆ CliContext()

cli::CliContext::CliContext ( std::unique_ptr< std::unordered_map< std::string, std::any > >  posArgs,
std::unique_ptr< std::unordered_map< std::string, std::any > >  optArgs,
std::unique_ptr< std::unordered_set< std::string > >  flagArgs,
cli::logging::AbstractLogger logger 
)
inlineexplicit

Constructs a new CliContext object from the passed argument maps.

Parameters
posArgsa map of positional argument names to their values
optArgsa map of optional argument names to their values
flagArgsa set of flag argument names
loggera logger instance to use in the methods this object is passed to

Member Function Documentation

◆ getArg()

template<typename T >
T cli::CliContext::getArg ( const std::string &  argName) const
inline

Gets the value of an argument.

Template Parameters
Tthe type to cast the argument value to (should be the same as the one used in defining the Argument)
Parameters
argNamethe name of the argument to retrieve
Returns
the value of the argument cast to the specified type

◆ getOptionArg() [1/2]

template<typename T >
T cli::CliContext::getOptionArg ( const std::string &  argName) const
inline

Gets the value of an optional argument.

Template Parameters
Tthe type to cast the argument value to (should be the same as the one used in defining the Argument)
Parameters
argNamethe name of the optional argument to retrieve
Returns
the value of the optional argument cast to the specified type

◆ getOptionArg() [2/2]

template<typename T >
void cli::CliContext::getOptionArg ( const std::string &  argName,
T &  out 
) const
inline

Gets the value of an optional argument and stores it in the provided output variable.

Template Parameters
Tthe type to cast the argument value to (should be the same as the one used in defining the Argument)
Parameters
argNamethe name of the optional argument to retrieve
outthe output variable to store the argument value in

◆ getPositionalArg() [1/2]

template<typename T >
T cli::CliContext::getPositionalArg ( const std::string &  argName) const
inline

Gets the value of a positional argument.

Template Parameters
Tthe type to cast the argument value to (should be the same as the one used in defining the Argument)
Parameters
argNamethe name of the positional argument to retrieve
Returns
the value of the positional argument cast to the specified type

◆ getPositionalArg() [2/2]

template<typename T >
void cli::CliContext::getPositionalArg ( const std::string &  argName,
T &  out 
) const
inline

Gets the value of a positional argument and stores it in the provided output variable.

Template Parameters
Tthe type to cast the argument value to (should be the same as the one used in defining the Argument)
Parameters
argNamethe name of the positional argument to retrieve
outthe output variable to store the argument value in

◆ getRepeatableArg()

template<typename T >
auto cli::CliContext::getRepeatableArg ( const std::string &  argName) const
inline

Gets all values of a repeatable argument.

Template Parameters
Tthe type to cast the argument values to (should be the same as the one used in defining the Argument)
Parameters
argNamethe name of the repeatable argument to retrieve
Returns
a vector of all values of the repeatable argument cast to the specified type

◆ getRepeatableOptionArg()

template<typename T >
std::vector< T > cli::CliContext::getRepeatableOptionArg ( const std::string &  argName) const
inline

Gets all values of a repeatable option argument.

Template Parameters
Tthe type to cast the single argument values to (should be the same as the one used in defining the Argument)
Parameters
argNamethe name of the repeatable option argument to retrieve
Returns
a vector of all values of the repeatable option argument cast to the specified type

◆ getRepeatablePositionalArg()

template<typename T >
std::vector< T > cli::CliContext::getRepeatablePositionalArg ( const std::string &  argName) const
inline

Gets all values of a repeatable positional argument.

Template Parameters
Tthe type to cast the single argument values to (should be the same as the one used in defining the Argument)
Parameters
argNamethe name of the repeatable positional argument to retrieve
Returns
a vector of all values of the repeatable positional argument cast to the specified type

◆ isArgPresent()

inline_t bool cli::CliContext::isArgPresent ( const std::string &  argName) const

Checks if an argument with the given name is present in the context.

Parameters
argNamethe name of the argument to check
Returns
true if the argument is present, false otherwise

◆ isFlagPresent()

inline_t bool cli::CliContext::isFlagPresent ( const std::string &  argName) const

Checks if a flag with the given name is present in the context.

Parameters
argNamethe name of the argument to check
Returns
true if the argument is present, false otherwise

◆ isOptionArgPresent()

inline_t bool cli::CliContext::isOptionArgPresent ( const std::string &  argName) const

Checks if an optional argument with the given name is present in the context.

Parameters
argNamethe name of the argument to check
Returns
true if the argument is present, false otherwise

◆ isPositionalArgPresent()

inline_t bool cli::CliContext::isPositionalArgPresent ( const std::string &  argName) const

Checks if a positional argument with the given name is present in the context.

Parameters
argNamethe name of the argument to check
Returns
true if the argument is present, false otherwise

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