LibCppCmd v1.0 Beta 15
C++ Command Line Parsing Library
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | List of all members
CmdLine::Option Class Reference

An ArgParam that represents an Option. More...

#include <Option.h>

Inheritance diagram for CmdLine::Option:
CmdLine::ArgParam CmdLine::Param CmdLine::ValueOption

Classes

struct  Definition
 This Definition is used to construct an Option. More...
 

Public Types

enum class  Style { Unix , Windows }
 The style of the Option to use. More...
 

Public Member Functions

 Option (Definition d)
 Constructs a new Option.
 
std::string Name () const override
 Gets the name of the Option.
 
std::string Description () const override
 Gets the description of the Option.
 
std::string HelpInfo () const override
 Gets help info for the Option.
 
bool IsSpecified () const override
 Indicates whether the Option has been specified.
 
bool IsMandatory () const override
 Indicates whether the Option is mandatory.
 
bool Populate (std::deque< std::string > &args) override
 Populates this Option from an argument queue.
 
bool CanPopulate (const std::deque< std::string > &args) const override
 Determines if arguments can populate this Option.
 
std::size_t Consumes (const std::deque< std::string > &args) const override
 Gets the number of arguments the Option consumes.
 
void Set (Style s)
 Sets the Option::Style of the Option.
 
std::string LongName () const
 Gets the long name of the Option.
 
- 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

An ArgParam that represents an Option.

A command line Option is an ArgParam that specifies an Option for the program. This is the most simple type of Option since it simply indicates whether it is specified when it is populated. Command line arguments that populate and specify options begin with an Option prefix followed by a short name (single character) or long name (multiple characters). What prefix is used depends on the Option::Style used (Unix or Windows). For instance, some programs have an Option that tells the program to produce vebose output. With the Unix Option style, either -v or –verbose could be used to specify such an Option. With the Windows Option style, either /v or /verbose could be used.

Member Enumeration Documentation

◆ Style

enum class CmdLine::Option::Style
strong

The style of the Option to use.

The style of Option determines the style of prefix used to specify and populate the Option at the command line. Unix style indicates that a - prefix should be used for short Option names and – for long Option names. Windows style indicates that a / prefix should be used for both short and long name Options.

Constructor & Destructor Documentation

◆ Option()

CmdLine::Option::Option ( Definition d)

Constructs a new Option.

Parameters
dThe definition used to create the Option.
Invariant
Valid short names characters are alpha numerics and ?.
Valid long name characters are alpha numerics, - and _.
Long names must not start with the option prefix.
Long names must not be longer than 20 characters.
Options must not have both an empty short and long name.
An option can only be specified by successful population.
Exceptions
Param::InvalidDefinitionDoesn't satisfy invariants.

Member Function Documentation

◆ CanPopulate()

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

Determines if arguments can populate this Option.

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

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

Implements CmdLine::ArgParam.

Reimplemented in CmdLine::ValueOption.

◆ Consumes()

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

Gets the number of arguments the Option consumes.

An Option should consume exactly 1 argument.

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

Implements CmdLine::ArgParam.

Reimplemented in CmdLine::ValueOption.

◆ Description()

std::string CmdLine::Option::Description ( ) const
inlineoverridevirtual

Gets the description of the Option.

The description of the Option is used for generating help.

Returns
The description of the Option.

Implements CmdLine::Param.

◆ HelpInfo()

std::string CmdLine::Option::HelpInfo ( ) const
overridevirtual

Gets help info for the Option.

The help info will include names (short and / or long) and description of the Option when generating help.

Returns
Help info for the Option.

Implements CmdLine::Param.

Reimplemented in CmdLine::ValueOption.

◆ IsMandatory()

bool CmdLine::Option::IsMandatory ( ) const
inlineoverridevirtual

Indicates whether the Option is mandatory.

The command line Parser uses this to determine if an Option must be specified at the command line for parsing to be successful. If a mandatory Option is not specified i.e. the correct command line argument necessary to populate the Option was not supplied, parsing will fail indicating the program was supplied with bad command line arguments.

Returns
True if the Option is mandatory, otherwise false.

Implements CmdLine::Param.

◆ IsSpecified()

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

Indicates whether the Option has been specified.

An Option will only indicate it was specified if the Option has been successfully populated by a command line argument. The parser also uses this to determine if a mandatory Option has been specified.

Returns
True if the Option is specified, otherwise false.

Implements CmdLine::Param.

◆ LongName()

std::string CmdLine::Option::LongName ( ) const
inline

Gets the long name of the Option.

The long name of the Option is used to determine if the Option is specified at the command line. It is also used by the Parser to prevent duplicate Options from being added to to it. Finally, the long name is used to generate help info for the Option. This name will always represent the prefixed long name of the option unless the option has no long name, in which case this function will return an empty string.

Returns
The prefixed long name of the Option.

◆ Name()

std::string CmdLine::Option::Name ( ) const
overridevirtual

Gets the name of the Option.

The name of the Option is used to determine if the Option is specified at the command line. It is also used by the Parser to prevent duplicate Options from being added to to it. Finally, the name is used to generate help info for the option. This name will always represent the prefixed short name of the Option unless the Option only has a long name, in which case this function will return the same value as the LongName() method.

Returns
The prefixed name of the Option.
See also
LongName().

Implements CmdLine::Param.

◆ Populate()

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

Populates this Option from an argument queue.

Accepts a reference to an argument queue (deque) and attempts to populate the Option from the next argument in the queue. Populating the Option marks it as specified. Successful population also consumes the argument from the queue that populated the Option.

Parameters
argsThe argument queue to populate the Option with.
Returns
True if population is successful, otherwise false.
Precondition
Size of arguments > 0.
Next argument must be Option's prefixed short or long name.
Postcondition
Option is marked as specified.
Next argument is removed from the queue.

Implements CmdLine::ArgParam.

Reimplemented in CmdLine::ValueOption.

◆ Set()

void CmdLine::Option::Set ( Style s)
inline

Sets the Option::Style of the Option.

Allows the program to change the Option::Style of the Option after it has been created. Alternatively, the Option::Style is set upon construction in the Option::Definition.

Parameters
sThe Option::Style to set

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