LibCppCmdLine v1.0
A cross-platform C++ library for parsing command line arguments
Loading...
Searching...
No Matches
CmdLine::PosParam Class Reference

A Positional command line ArgParam. More...

#include <PosParam.h>

Inheritance diagram for CmdLine::PosParam:
CmdLine::ArgParam CmdLine::Param

Classes

struct  Definition
 This definition is used to construct a PosParam. More...

Public Member Functions

 PosParam (Definition d)
 Constructs a new PosParam.
std::string Value () const
 Gets the value of the PosParam.
virtual std::string Name () const override
 Gets the name of the Param.
virtual std::string Description () const override
 Gets the description of the Param.
virtual std::string HelpInfo () const override
 Gets help information for the Param.
virtual bool IsSpecified () const override
 Indicates whether the Param has been specified.
virtual bool IsMandatory () const override
 Indicates whether the Param is mandatory.
bool Populate (std::deque< std::string > &args) override
 Populates this PosParam from an argument queue.
bool CanPopulate (const std::deque< std::string > &args) const override
 Determines if the specified args can populate the ArgParam.
std::size_t Consumes (const std::deque< std::string > &args) const override
 Gets the number of arguments the PosParam consumes.
Public Member Functions inherited from CmdLine::ArgParam
virtual ~ArgParam ()=default
 Destructs an ArgParam.
Public Member Functions inherited from CmdLine::Param
virtual ~Param ()=default
 Destructs a Param.

Detailed Description

A Positional command line ArgParam.

A PosParam is a command line ArgParam that is populated based on the position of the command line arguments. Exactly one argument populates a PosParam by both specifying it and populating its value. By default, PosParams are populated after all Options have been parsed. However, if a MultiPosParam is also set on the Parser with a MultiPosParam::ParsingOrder of AfterOptions, then PosParams will be populated at the end. An example of when to use a PosParam is a copy program that has multiple source file paths and a single desitination path. The single destination path would populate a PosParam while the multile source paths would be captured by a MultiPosParam.

Invariant
Value can only be set upon successful population.

Constructor & Destructor Documentation

◆ PosParam()

CmdLine::PosParam::PosParam ( Definition d)

Constructs a new PosParam.

Parameters
dThe definition to construct the PosParam with.
Exceptions
Param::InvalidDefinitionDoesn't satisfy invariants.

Member Function Documentation

◆ CanPopulate()

bool CmdLine::PosParam::CanPopulate ( const std::deque< std::string > & args) const
overridevirtual

Determines if the specified args can populate the ArgParam.

Accepts a reference to an argument queue (deque) and determines if the next n arguments in the queue can populate the ArgParam.

Parameters
argsThe argument queue to evaluate.
Returns
True if the arguments can populate, otherwise false.

Implements CmdLine::ArgParam.

◆ Consumes()

std::size_t CmdLine::PosParam::Consumes ( const std::deque< std::string > & args) const
inlineoverridevirtual

Gets the number of arguments the PosParam consumes.

Each Positional consumes exactly 1 argument.

Parameters
argsThe argument queue to evaluate.
Returns
The number of arguments the PosParam will consume.

Implements CmdLine::ArgParam.

◆ Description()

virtual std::string CmdLine::PosParam::Description ( ) const
inlineoverridevirtual

Gets the description of the Param.

The description of the Param is used for generating help.

Returns
The description of the Param.

Implements CmdLine::Param.

◆ HelpInfo()

virtual std::string CmdLine::PosParam::HelpInfo ( ) const
overridevirtual

Gets help information for the Param.

The help info will include the Name() and Description() of the parameter when generating help.

Returns
Help information for the parameter.

Implements CmdLine::Param.

◆ IsMandatory()

virtual bool CmdLine::PosParam::IsMandatory ( ) const
inlineoverridevirtual

Indicates whether the Param is mandatory.

The command line Parser uses this to determine if a parameter must be specified at the command line for parsing to be successful. If a mandatory parameter is not specified, parsing will fail indicating the program was supplied with bad command line arguments.

Returns
True if the parameter is mandatory, otherwise false.

Implements CmdLine::Param.

◆ IsSpecified()

virtual bool CmdLine::PosParam::IsSpecified ( ) const
inlineoverridevirtual

Indicates whether the Param has been specified.

A Param will only indicate it was specified if it has been successfully populated by data from a command line argument. The command line Parser also uses this to determine if a mandatory parameter has been specified.

Returns
True if the parameter is specified, otherwise false.

Implements CmdLine::Param.

◆ Name()

virtual std::string CmdLine::PosParam::Name ( ) const
inlineoverridevirtual

Gets the name of the Param.

The name of the Param is used for generating usage and help.

Returns
The name of the Param.

Implements CmdLine::Param.

◆ Populate()

bool CmdLine::PosParam::Populate ( std::deque< std::string > & args)
overridevirtual

Populates this PosParam from an argument queue.

Accepts a reference to an argument queue (deque) and attempts to populate the PosParam from the next argument in the queue. Populating the MultiPosParam marks it as specified and populates it with value from the command line argument. Successful population also consumes the argument from the queue that populated the PosParam.

Parameters
argsThe arguments to populate the PosParam with.
Returns
True if population is successful, otherwise false.
Precondition
Size of arguments >= 1.
Postcondition
The PosParam is marked specified.
The the next argument in the argument queue is removed.
The argument removed populates the the value.

Implements CmdLine::ArgParam.

◆ Value()

std::string CmdLine::PosParam::Value ( ) const
inline

Gets the value of the PosParam.

The value of a PosParam always equals the argument that populated it.

Returns
The value of the PosParam.

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