ROSE  0.9.10.91
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 483 of file util/Sawyer/CommandLine.h.

#include <CommandLine.h>

Public Member Functions

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

Constructor & Destructor Documentation

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 498 of file util/Sawyer/CommandLine.h.

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 509 of file util/Sawyer/CommandLine.h.

Member Function Documentation

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 514 of file util/Sawyer/CommandLine.h.

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 534 of file util/Sawyer/CommandLine.h.

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

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 535 of file util/Sawyer/CommandLine.h.

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 541 of file util/Sawyer/CommandLine.h.

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

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 542 of file util/Sawyer/CommandLine.h.

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 546 of file util/Sawyer/CommandLine.h.

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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 567 of file util/Sawyer/CommandLine.h.

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 571 of file util/Sawyer/CommandLine.h.

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 572 of file util/Sawyer/CommandLine.h.

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 582 of file util/Sawyer/CommandLine.h.

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 587 of file util/Sawyer/CommandLine.h.

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 591 of file util/Sawyer/CommandLine.h.

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 595 of file util/Sawyer/CommandLine.h.

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 599 of file util/Sawyer/CommandLine.h.

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.

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

True if the value is void.

Returns true if this object has no value.

Definition at line 606 of file util/Sawyer/CommandLine.h.

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

void Sawyer::CommandLine::ParsedValue::print ( std::ostream &  ) const

Print some debugging information.


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