AGX Dynamics 2.42.3.1
Loading...
Searching...
No Matches
agxIO::ArgumentParser Class Reference

A small argument parser class. More...

#include <ArgumentParser.h>

+ Inheritance diagram for agxIO::ArgumentParser:

Classes

class  Argument
 Help class for handling arguments. More...
 

Public Types

typedef agx::Vector< Argument > ArgumentVector
 

Public Member Functions

 ArgumentParser ()
 Constructs an argument parser from the program command line input.
 
 ArgumentParser (const agx::StringVector &arguments)
 Constructs an argument parser from the program command line input given as an array of strings.
 
 ArgumentParser (int argc, char **argv)
 Constructs an argument parser from the program command line input.
 
void addArgument (const char *argumentName)
 Adds an argument manually.
 
template<typename T >
T defaultRead (const std::string &key, const T &defaultValue, size_t startPosition=0, bool findAlsoHandled=true)
 Reads an argument into a variable.
 
int find (const std::string &key, size_t startPosition=0, bool findAlsoHandled=true)
 Finds the position of an argument.
 
Argument * getArgument (size_t index)
 Gets an argument at a position in the argument container.
 
char ** getArgv ()
 Gets the original array of c-strings.
 
size_t getNumArguments () const
 Returns the number of arguments.
 
bool read (const std::string &key, size_t startPosition=0, bool findAlsoHandled=true)
 Looks for an argument, and returns true if it finds it.
 
template<typename T >
bool read1 (const std::string &key, T &value, size_t startPosition=0, bool findAlsoHandled=true)
 Looks for an argument, and reads it into a variable if found.
 
template<typename T1 , typename T2 >
bool read2 (const std::string &key, T1 &value1, T2 &value2, size_t startPosition=0, bool findAlsoHandled=true)
 Reads two argument into variables.
 
template<typename T1 , typename T2 , typename T3 >
bool read3 (const std::string &key, T1 &value1, T2 &value2, T3 &value3, size_t startPosition=0, bool findAlsoHandled=true)
 Reads three argument into variables.
 
bool reportUnreadArguments (std::ostream &out=std::cout)
 Writes all arguments which have not been handled to a stream.
 
- Public Member Functions inherited from agx::Referenced
 Referenced ()
 Default constructor.
 
 Referenced (const Referenced &)
 
template<typename T >
T * as ()
 Subclass casting.
 
template<typename T >
const T * as () const
 
template<typename T >
T * asSafe ()
 Safe subclass casting, return nullptr if template type does not match.
 
template<typename T >
const T * asSafe () const
 
int getReferenceCount () const
 
template<typename T >
bool is () const
 Subclass test.
 
Referenced & operator= (const Referenced &)
 Assignment operator. Will increment the number of references to the referenced object.
 
void reference (void *ptr=nullptr) const
 Explicitly increment the reference count by one, indicating that this object has another pointer which is referencing it.
 
void unreference (void *ptr=nullptr) const
 Decrement the reference count by one, indicating that a pointer to this object is referencing it.
 
void unreference_nodelete () const
 Decrement the reference count by one, indicating that a pointer to this object is referencing it.
 

Protected Member Functions

virtual ~ArgumentParser ()
 
- Protected Member Functions inherited from agx::Referenced
virtual ~Referenced ()
 Destructor.
 
void allocateObserverVector () const
 
void deleteUsingDeleteHandler () const
 

Additional Inherited Members

- Static Public Member Functions inherited from agx::Referenced
template<typename T >
static bool ValidateCast (const Referenced *object)
 
- Static Protected Member Functions inherited from agx::Referenced
static DeleteHandler * getDeleteHandler ()
 
static void setDeleteHandler (DeleteHandler *handler)
 Internal: Set a DeleteHandler to which deletion of all referenced counted objects will be delegated to.
 
- Protected Attributes inherited from agx::Referenced
Mutex m_mutex
 
ObserverContainer m_observers
 
AtomicValue m_refCount
 

Detailed Description

A small argument parser class.

Copies over the content of all c strings to std::strings.

There is no differentiation between keys and values, which means that if the arguments are "a b", then "a" could be key and "b" value, and/or both could be keys (without values).

Definition at line 37 of file ArgumentParser.h.

Member Typedef Documentation

◆ ArgumentVector

Constructor & Destructor Documentation

◆ ArgumentParser() [1/3]

agxIO::ArgumentParser::ArgumentParser ( )

Constructs an argument parser from the program command line input.

◆ ArgumentParser() [2/3]

agxIO::ArgumentParser::ArgumentParser ( int  argc,
char **  argv 
)

Constructs an argument parser from the program command line input.

Each white-space separated string is treated as a separate argument, so that the number of arguments will correspond to argc. The content of argv is copied and stored internally.

◆ ArgumentParser() [3/3]

agxIO::ArgumentParser::ArgumentParser ( const agx::StringVector &  arguments)

Constructs an argument parser from the program command line input given as an array of strings.

◆ ~ArgumentParser()

virtual agxIO::ArgumentParser::~ArgumentParser ( )
protectedvirtual

Member Function Documentation

◆ addArgument()

void agxIO::ArgumentParser::addArgument ( const char *  argumentName)

Adds an argument manually.

Not recommended unless you know what you're doing.

Parameters
argumentNameName of the argument to be added.

◆ defaultRead()

template<typename T >
T agxIO::ArgumentParser::defaultRead ( const std::string &  key,
const T &  defaultValue,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Reads an argument into a variable.

Note
Will set the argument to 'handled'.
Parameters
keyKey argument
defaultValueValue to be returned if read is not successful
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
Thevalue of the value argument, if the read was successful, otherwise the defaultValue.

Definition at line 275 of file ArgumentParser.h.

References read1().

◆ find()

int agxIO::ArgumentParser::find ( const std::string &  key,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Finds the position of an argument.

Returns -1 if not found.

Note
Will not set the argument to 'handled'.
Parameters
keyArgument name
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
positionof argument if found, -1 if not found

Referenced by agxUnit::haveUnittestingArguments(), read1(), read2(), and read3().

◆ getArgument()

Argument * agxIO::ArgumentParser::getArgument ( size_t  index)

Gets an argument at a position in the argument container.

◆ getArgv()

char ** agxIO::ArgumentParser::getArgv ( )

Gets the original array of c-strings.

The content has been copied to an internal data structure.

◆ getNumArguments()

size_t agxIO::ArgumentParser::getNumArguments ( ) const

Returns the number of arguments.

◆ read()

bool agxIO::ArgumentParser::read ( const std::string &  key,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Looks for an argument, and returns true if it finds it.

Note
Will set the argument to 'handled'.
Parameters
keyArgument name
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
Returnsif the argument was found.

◆ read1()

template<typename T >
bool agxIO::ArgumentParser::read1 ( const std::string &  key,
T &  value,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Looks for an argument, and reads it into a variable if found.

Note
Will set the argument to 'handled'.
Parameters
keyKey argument
valueValue argument, which has to come directly after the key argument in the argument list.
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
Werethe key and value argument found, and was the conversion successful?

Definition at line 221 of file ArgumentParser.h.

References find(), and agx::Container::size().

Referenced by defaultRead().

◆ read2()

template<typename T1 , typename T2 >
bool agxIO::ArgumentParser::read2 ( const std::string &  key,
T1 &  value1,
T2 &  value2,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Reads two argument into variables.

Note
Will set the argument to 'handled'.
Parameters
keyKey argument
value1Value argument, which has to come directly after the key argument in the argument list.
value2Value argument, which has to come directly after the key argument in the argument list.
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
Werethe key and value argument found, and was the conversion successful?

Definition at line 237 of file ArgumentParser.h.

References find(), and agx::Container::size().

◆ read3()

template<typename T1 , typename T2 , typename T3 >
bool agxIO::ArgumentParser::read3 ( const std::string &  key,
T1 &  value1,
T2 &  value2,
T3 &  value3,
size_t  startPosition = 0,
bool  findAlsoHandled = true 
)

Reads three argument into variables.

Note
Will set the argument to 'handled'.
Parameters
keyKey argument
value1Value argument, which has to come directly after the key argument in the argument list.
value2Value argument, which has to come directly after the key argument in the argument list.
value3Value argument, which has to come directly after the key argument in the argument list.
startPositionPosition in argument array where to start searching from.
findAlsoHandledShould also arguments been found which have been treated as 'handled'?
Return values
Werethe key and value argument found, and was the conversion successful?

Definition at line 255 of file ArgumentParser.h.

References find(), and agx::Container::size().

◆ reportUnreadArguments()

bool agxIO::ArgumentParser::reportUnreadArguments ( std::ostream &  out = std::cout)

Writes all arguments which have not been handled to a stream.

Parameters
outAn std::ostream that should be written to.
Returns
true if all arguments were read. All is ok.

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