ROSE 0.11.145.192
Public Member Functions | List of all members
Sawyer::CommandLine::ParsedValue Class Reference

Description

Information about a parsed switch value.

Each time a switch argument is parsed to create a value, whether it comes from the program command line or a default value string, a ParsedValue object is constructed to describe it. These objects hold the value, the string from whence the value was parsed, and information about where the value came from and with which switch it is associated. This class also provides a number of methods for conveniently and safely casting the value to other types.

Definition at line 505 of file Sawyer/CommandLine.h.

#include <Sawyer/CommandLine.h>

Public Member Functions

 ParsedValue ()
 Construct a new empty value.
 
 ParsedValue (const boost::any value, const Location &loc, const std::string &str, const ValueSaver::Ptr &saver)
 Construct a new value.
 
ParsedValueswitchInfo (const std::string &key, const Location &loc, const std::string &str)
 Update switch information.
 
const std::string & string () const
 String representation.
 
template<typename T >
as () const
 Convenient any_cast.
 
const std::string & switchString () const
 The string for the switch that caused this value to be parsed.
 
Location switchLocation () const
 The command-line location of the switch to which this value belongs.
 
size_t keySequence () const
 How this value relates to others with the same key.
 
size_t switchSequence () const
 How this value relates to others created by the same switch.
 
const ValueSaver::Ptr valueSaver () const
 How to save a value at a user-supplied location.
 
void save () const
 Save this value in switch-supplied storage.
 
bool isEmpty () const
 True if the value is void.
 
void print (std::ostream &) const
 Print some debugging information.
 
const boost::any & value () const
 Property: the parsed value.
 
void value (const boost::any &v)
 Property: the parsed value.
 
Location valueLocation () const
 Property: command-line location from whence this value came.
 
ParsedValuevalueLocation (const Location &loc)
 Property: command-line location from whence this value came.
 
int asInt () const
 Convenience cast.
 
unsigned asUnsigned () const
 Convenience cast.
 
long asLong () const
 Convenience cast.
 
unsigned long asUnsignedLong () const
 Convenience cast.
 
boost::int64_t asInt64 () const
 Convenience cast.
 
boost::uint64_t asUnsigned64 () const
 Convenience cast.
 
double asDouble () const
 Convenience cast.
 
float asFloat () const
 Convenience cast.
 
bool asBool () const
 Convenience cast.
 
std::string asString () const
 Convenience cast.
 
ParsedValueswitchKey (const std::string &s)
 Property: switch key.
 
const std::string & switchKey () const
 Property: switch key.
 

Constructor & Destructor Documentation

◆ ParsedValue() [1/2]

Sawyer::CommandLine::ParsedValue::ParsedValue ( )
inline

Construct a new empty value.

The isEmpty method will return true for values that are default constructed.

Definition at line 520 of file Sawyer/CommandLine.h.

◆ ParsedValue() [2/2]

Sawyer::CommandLine::ParsedValue::ParsedValue ( const boost::any  value,
const Location loc,
const std::string &  str,
const ValueSaver::Ptr saver 
)
inline

Construct a new value.

The type of the value is erased via boost::any so that templates do not need to be used at the API level. It is the responsibility of the user to remember the type of the value, although restoration of the type information via boost::any_cast will check consistency. The loc is the starting location of the value on the command line, or the constant NOWHERE. The str is the string that was parsed to create the value (or at least a string representation of the value). The saver is an optional pointer to the functor that's responsible for pushing the value to a user-specified variable when ParserResult::apply is called.

The arguments specified here are the values returned by value, valueLocation, string, and valueSaver.

Definition at line 531 of file Sawyer/CommandLine.h.

Member Function Documentation

◆ switchInfo()

ParsedValue & Sawyer::CommandLine::ParsedValue::switchInfo ( const std::string &  key,
const Location loc,
const std::string &  str 
)
inline

Update switch information.

This updates information about the switch that parsed the value. The arguments specified here will be the values returned by switchKey, switchLocation, and switchString.

Definition at line 536 of file Sawyer/CommandLine.h.

◆ value() [1/2]

const boost::any & Sawyer::CommandLine::ParsedValue::value ( ) const
inline

Property: the parsed value.

Parsed values are represented by boost::any, which is capable of storing any type of parsed value including void. This is the most basic access to the value; the class also provides a variety of casting accessors that are sometimes more convenient (their names start with the word "as").

Definition at line 556 of file Sawyer/CommandLine.h.

Referenced by Sawyer::CommandLine::Sum< T >::operator()().

◆ value() [2/2]

void Sawyer::CommandLine::ParsedValue::value ( const boost::any &  v)
inline

Property: the parsed value.

Parsed values are represented by boost::any, which is capable of storing any type of parsed value including void. This is the most basic access to the value; the class also provides a variety of casting accessors that are sometimes more convenient (their names start with the word "as").

Definition at line 557 of file Sawyer/CommandLine.h.

◆ valueLocation() [1/2]

Location Sawyer::CommandLine::ParsedValue::valueLocation ( ) const
inline

Property: command-line location from whence this value came.

For values that are defaults which didn't come from the command-line, the constant NOWHERE is returned.

Definition at line 563 of file Sawyer/CommandLine.h.

Referenced by Sawyer::CommandLine::SwitchArgument::SwitchArgument(), Sawyer::CommandLine::Switch::intrinsicValue(), and Sawyer::CommandLine::Switch::intrinsicValue().

◆ valueLocation() [2/2]

ParsedValue & Sawyer::CommandLine::ParsedValue::valueLocation ( const Location loc)
inline

Property: command-line location from whence this value came.

For values that are defaults which didn't come from the command-line, the constant NOWHERE is returned.

Definition at line 564 of file Sawyer/CommandLine.h.

◆ string()

const std::string & Sawyer::CommandLine::ParsedValue::string ( ) const
inline

String representation.

This is the string that was parsed to create the value.

Definition at line 568 of file Sawyer/CommandLine.h.

Referenced by Sawyer::CommandLine::SwitchArgument::defaultValueString().

◆ asInt()

int Sawyer::CommandLine::ParsedValue::asInt ( ) const

Convenience cast.

This returns the value cast to the specified type. Whereas boost::any_cast requires an exact type match for the cast to be successful, this method makes more of an effort to be successful. It recognizes a variety of common types; less common types will need to be explicitly converted by hand. In any case, the original string representation and parser are available if needed.

◆ asUnsigned()

unsigned Sawyer::CommandLine::ParsedValue::asUnsigned ( ) const

Convenience cast.

This returns the value cast to the specified type. Whereas boost::any_cast requires an exact type match for the cast to be successful, this method makes more of an effort to be successful. It recognizes a variety of common types; less common types will need to be explicitly converted by hand. In any case, the original string representation and parser are available if needed.

◆ asLong()

long Sawyer::CommandLine::ParsedValue::asLong ( ) const

Convenience cast.

This returns the value cast to the specified type. Whereas boost::any_cast requires an exact type match for the cast to be successful, this method makes more of an effort to be successful. It recognizes a variety of common types; less common types will need to be explicitly converted by hand. In any case, the original string representation and parser are available if needed.

◆ asUnsignedLong()

unsigned long Sawyer::CommandLine::ParsedValue::asUnsignedLong ( ) const

Convenience cast.

This returns the value cast to the specified type. Whereas boost::any_cast requires an exact type match for the cast to be successful, this method makes more of an effort to be successful. It recognizes a variety of common types; less common types will need to be explicitly converted by hand. In any case, the original string representation and parser are available if needed.

◆ asInt64()

boost::int64_t Sawyer::CommandLine::ParsedValue::asInt64 ( ) const

Convenience cast.

This returns the value cast to the specified type. Whereas boost::any_cast requires an exact type match for the cast to be successful, this method makes more of an effort to be successful. It recognizes a variety of common types; less common types will need to be explicitly converted by hand. In any case, the original string representation and parser are available if needed.

◆ asUnsigned64()

boost::uint64_t Sawyer::CommandLine::ParsedValue::asUnsigned64 ( ) const

Convenience cast.

This returns the value cast to the specified type. Whereas boost::any_cast requires an exact type match for the cast to be successful, this method makes more of an effort to be successful. It recognizes a variety of common types; less common types will need to be explicitly converted by hand. In any case, the original string representation and parser are available if needed.

◆ asDouble()

double Sawyer::CommandLine::ParsedValue::asDouble ( ) const

Convenience cast.

This returns the value cast to the specified type. Whereas boost::any_cast requires an exact type match for the cast to be successful, this method makes more of an effort to be successful. It recognizes a variety of common types; less common types will need to be explicitly converted by hand. In any case, the original string representation and parser are available if needed.

◆ asFloat()

float Sawyer::CommandLine::ParsedValue::asFloat ( ) const

Convenience cast.

This returns the value cast to the specified type. Whereas boost::any_cast requires an exact type match for the cast to be successful, this method makes more of an effort to be successful. It recognizes a variety of common types; less common types will need to be explicitly converted by hand. In any case, the original string representation and parser are available if needed.

◆ asBool()

bool Sawyer::CommandLine::ParsedValue::asBool ( ) const

Convenience cast.

This returns the value cast to the specified type. Whereas boost::any_cast requires an exact type match for the cast to be successful, this method makes more of an effort to be successful. It recognizes a variety of common types; less common types will need to be explicitly converted by hand. In any case, the original string representation and parser are available if needed.

◆ asString()

std::string Sawyer::CommandLine::ParsedValue::asString ( ) const

Convenience cast.

This returns the value cast to the specified type. Whereas boost::any_cast requires an exact type match for the cast to be successful, this method makes more of an effort to be successful. It recognizes a variety of common types; less common types will need to be explicitly converted by hand. In any case, the original string representation and parser are available if needed.

◆ as()

template<typename T >
T Sawyer::CommandLine::ParsedValue::as ( ) const
inline

Convenient any_cast.

This is a slightly less verbose way to get the value and perform a boost::any_cast.

Definition at line 589 of file Sawyer/CommandLine.h.

◆ switchKey() [1/2]

ParsedValue & Sawyer::CommandLine::ParsedValue::switchKey ( const std::string &  s)
inline

Property: switch key.

The key used by the switch that created this value.

Definition at line 593 of file Sawyer/CommandLine.h.

◆ switchKey() [2/2]

const std::string & Sawyer::CommandLine::ParsedValue::switchKey ( ) const
inline

Property: switch key.

The key used by the switch that created this value.

Definition at line 594 of file Sawyer/CommandLine.h.

◆ switchString()

const std::string & Sawyer::CommandLine::ParsedValue::switchString ( ) const
inline

The string for the switch that caused this value to be parsed.

This string includes the switch prefix and the switch name in order to allow programs to distinguish between the same switch occuring with two different prefixes (like the "-invert" vs "+invert" style which is sometimes used for Boolean-valued switches).

For nestled short switches, the string returned by this method doesn't necessarily appear anywhere on the program command line. For instance, this method might return "-b" when the command line was "-ab", because "-a" is a different switch with presumably a different set of parsed values.

Definition at line 604 of file Sawyer/CommandLine.h.

◆ switchLocation()

Location Sawyer::CommandLine::ParsedValue::switchLocation ( ) const
inline

The command-line location of the switch to which this value belongs.

The return value indicates the start of the switch name after any leading prefix. For nestled single-character switches, the location's command line argument index will be truthful, but the character offset will refer to the string returned by switchString.

Definition at line 609 of file Sawyer/CommandLine.h.

◆ keySequence()

size_t Sawyer::CommandLine::ParsedValue::keySequence ( ) const
inline

How this value relates to others with the same key.

This method returns the sequence number for this value among all values created for the same switch key.

Definition at line 613 of file Sawyer/CommandLine.h.

◆ switchSequence()

size_t Sawyer::CommandLine::ParsedValue::switchSequence ( ) const
inline

How this value relates to others created by the same switch.

This method returns the sequence number for this value among all values created for switches with the same Switch::preferredName.

Definition at line 617 of file Sawyer/CommandLine.h.

◆ valueSaver()

const ValueSaver::Ptr Sawyer::CommandLine::ParsedValue::valueSaver ( ) const
inline

How to save a value at a user-supplied location.

These functors are used internally by the library and users don't usually see them.

Definition at line 621 of file Sawyer/CommandLine.h.

◆ save()

void Sawyer::CommandLine::ParsedValue::save ( ) const

Save this value in switch-supplied storage.

This calls the functor returned by valueSaver in order to save this parsed value to a user-specified variable in a type-specific manner.

◆ isEmpty()

bool Sawyer::CommandLine::ParsedValue::isEmpty ( ) const
inline

True if the value is void.

Returns true if this object has no value.

Definition at line 628 of file Sawyer/CommandLine.h.

Referenced by Sawyer::CommandLine::SwitchArgument::isRequired().


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