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

Provides parameters for ValueOption values. More...

#include <OptionParam.h>

Inheritance diagram for CmdLine::OptionParam:
CmdLine::Param

Classes

struct  Definition
 This definition is used to construct an OptionParam. More...

Public Member Functions

 OptionParam (Definition definition)
 Constructs a new OptionParam.
std::string Name () const override
 Gets the name of the Param.
std::string Description () const override
 Gets the description of the Param.
std::string HelpInfo () const override
 Gets help information for the Param.
bool IsSpecified () const override
 Indicates whether the OptionParam has been specified.
bool IsMandatory () const override
 Indicates whether the Param is mandatory.
std::string Value () const
 Gets the value the OptionParam was populated with.
bool Populate (NameValuePair p)
 Populates the OptionParam from a NameValuePair.
bool CanPopulate (NameValuePair p) const
 Checks if the NameValuePair can populate OptionParam.
Public Member Functions inherited from CmdLine::Param
virtual ~Param ()=default
 Destructs a Param.

Detailed Description

Provides parameters for ValueOption values.

An OptionParam provides a way for a program to capture specific values passed to a ValueOption. Option parameters can be added to value options for this purpose. This is useful for value options have a specific list of possible values when the program needs to check that one or more of those possible values was specified. The name of the option parameter defines the possible option value. If the value passed to the ValueOption at the command line is a NameValuePair, the the option parameter will not only indicate that the name was specified as a possible value, it will also have its own value (the value of the NameValuePair). For example, assume a ValueOption "--print" instructs a program to print metadata values for a song. Assume the possible values for the "--print" ValueOption are "song", "artist", and "album". In this case you would add three option parameters to the "--print" ValueOption. Assume then that the user specifies "--print song" at the command line. The option parameter "song" would be specified but have no value of its own. Now assume the same program has an "--edit" ValueOption to change those same metadata properties. If the users specified "--edit 'song=Happy Birthday'", the "song" option parameter would be specified and have the value "Happy Birthday".

Invariant
Value is only set by successful population.

Constructor & Destructor Documentation

◆ OptionParam()

CmdLine::OptionParam::OptionParam ( Definition definition)

Constructs a new OptionParam.

Parameters
definitionThe definition to construct the OptionParam.
Exceptions
Param::InvalidDefinitionDoesn't satisfy invariants.

Member Function Documentation

◆ CanPopulate()

bool CmdLine::OptionParam::CanPopulate ( NameValuePair p) const

Checks if the NameValuePair can populate OptionParam.

Parameters
pThe NameValuePair to evaluate.
Returns
True if it can populate, otherwise false.

◆ Description()

std::string CmdLine::OptionParam::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()

std::string CmdLine::OptionParam::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()

bool CmdLine::OptionParam::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()

bool CmdLine::OptionParam::IsSpecified ( ) const
inlineoverridevirtual

Indicates whether the OptionParam has been specified.

An OptionParam will only indicate it was specified if the parameter has been successfully populated by an OptionValue. The Parser also uses this to determine if a mandatory OptionParam has been specified.

Returns
True if the parameter is specified, otherwise false.

Implements CmdLine::Param.

◆ Name()

std::string CmdLine::OptionParam::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::OptionParam::Populate ( NameValuePair p)

Populates the OptionParam from a NameValuePair.

An OptionParam is populated from a NameValuePair that represents an argument (value) that was passed to the ValueOption at the command line. If the NameValuePair itself does not specifiy a value, population will only mark the parameter as specified, otherwise it will both specify the parameter and populate its value.

Parameters
pThe NameValuePair to populate the OptionParam with.
Returns
True if population was successful, otherwise false.
Precondition
The pair's name must match the OptionParam's name.
Postcondition
The OptionParam is marked as specified.
If there was a value, the value will be set.

◆ Value()

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

Gets the value the OptionParam was populated with.

The value of an OptionParam is the value of a NameValuePair passed to the ValueOption at the command line. If just a name was passed, the value will be an empty string.

Returns
The value of the parameter.

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