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

A command line Option that is populated with values. More...

#include <ValueOption.h>

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

Classes

struct  Definition
 This definition is used to construct a ValueOption. More...
 
class  DuplicateOptionParam
 An exception thrown for adding duplicate OptionParams. More...
 
class  NullOptionParam
 An exception thrown for adding null OptionParam. More...
 

Public Member Functions

 ValueOption (Definition d)
 Constructs a new ValueOption.
 
bool Populate (std::deque< std::string > &args) override
 Populates this ValueOption from an argument queue.
 
bool CanPopulate (const std::deque< std::string > &args) const override
 Determines if arguments can populate this ValueOption.
 
std::size_t Consumes (const std::deque< std::string > &args) const override
 Gets the number of arguments the ValueOption consumes.
 
std::string HelpInfo () const override
 Gets help info for the ValueOption.
 
std::vector< std::string > Values () const
 Gets the values the ValueOption is populated with.
 
void Add (OptionParam *p)
 Adds the specified OptionParam to the ValueOption.
 
- Public Member Functions inherited from CmdLine::Option
 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.
 
bool IsSpecified () const override
 Indicates whether the Option has been specified.
 
bool IsMandatory () const override
 Indicates whether the Option is mandatory.
 
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.
 

Additional Inherited Members

- Public Types inherited from CmdLine::Option
enum class  Style { Unix , Windows }
 The style of the Option to use. More...
 

Detailed Description

A command line Option that is populated with values.

A command line ValueOption is an ArgParam that not only specifies an Option for the program but also captures one or more values associated with that option. 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, a media program might have an Option that prints song metadata. Wit the Unix Option style, either "-p" or "--print" could be used to specify such an Option. With the Windows Option::Style, either "/p" or "/print" could be used. Using that same example, to print the album of a song using a hypothetical "--print" Option, one might specify "--print album" at the command line. In this case, not only would the "--print" Option be specified, but it would be populated with the value "album". Assuming there was a specific list of possible values for "--print", ValueOption supports adding OptionParams which are specified by a specific Option value. For instance, one might add OptionParams for "song", "album", "artist", etc. For an Option that sets such a value, you could have an "--edit" option. For example, "--edit 'artist=The Beatles'". In this case the value is a NameValuePair and an "artist" OptionParam could extract the value from the pair. Finally, a ValueOption may be specified multiple times to populate it with multiple values.

Constructor & Destructor Documentation

◆ ValueOption()

CmdLine::ValueOption::ValueOption ( Definition d)

Constructs a new ValueOption.

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.
By extension, so are OptionParams and values.
Must not have duplicate OptionParams.
Exceptions
Param::InvalidDefinitionDoesn't satisfy invariants.

Member Function Documentation

◆ Add()

void CmdLine::ValueOption::Add ( OptionParam * p)

Adds the specified OptionParam to the ValueOption.

Each OptionParam pointer added to the ValueOption can be populated by a ValueOption value (or name-value pair) that matches the OptionParam's name. Adding OptionParams to the ValueOption can be a way of constraining the ValueOption to a list of possible values that will indicate whether or not they were specified.

Parameters
pThe OptionParam pointer to add
Returns
True if the add operation was successful, otherwise false.
Precondition
The OptionParam pointer must not be null.
A duplicate OptionParam must not have already been added.
Postcondition
The OptionParam is added to the ValueOption.
Exceptions
DuplicateOptionParamTried to add duplicate OptionParam.

◆ CanPopulate()

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

Determines if arguments can populate this ValueOption.

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

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

Reimplemented from CmdLine::Option.

◆ Consumes()

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

Gets the number of arguments the ValueOption consumes.

A ValueOption should consume exatly 2 arguments.

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

Reimplemented from CmdLine::Option.

◆ HelpInfo()

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

Gets help info for the ValueOption.

The help info will include names (short and / or long) and description of the Option when generating help. It will also include the help info of any OptionParams that were added to the ValueOption.

Returns
Help info for the ValueOption.

Reimplemented from CmdLine::Option.

◆ Populate()

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

Populates this ValueOption from an argument queue.

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

Parameters
argsThe argument queue to populate the Option with.
Returns
True if population is successful, otherwise false.
Precondition
Size of arguments >= 2
Postcondition
The ValueOption is marked specified.
The next two arguments are removed from the queue.
Consumed arguments are added to the ValueOption values.

Reimplemented from CmdLine::Option.

◆ Values()

std::vector< std::string > CmdLine::ValueOption::Values ( ) const
inline

Gets the values the ValueOption is populated with.

ValueOptions are populated with the command line argument that is passed to the ValueOtion by the user. This argument can either be a single value or a NameValuePair.

Returns
The values the ValueOption is populated with.

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