Command Class Reference

A command is a functionality which can be called via commandline on program startup. More...

#include <command.h>

Inheritance diagram for Command:
MultipleCommand< std::string > SingleCommand< int > SingleCommand< std::string > Command_LogLevel MultipleCommand< T, U, V, W > MultipleCommandZeroArguments SingleCommand< T, U, V, W > SingleCommandZeroArguments

List of all members.

Public Member Functions

 Command (const std::string &name, const std::string &shortName="", const std::string &infoText="", const std::string &parameterList="", const int argumentNum=1, const bool allowMultipleCalls=false)
 The constructor which just saves the arguments to out own member variables.
const std::string getName ()
 Returns the name of this command.
const std::string getShortName ()
 Returns the short name of this command.
const std::string getParameterList ()
 Returns the parameter list necessary for the usage method.
const std::string getInfoText ()
 Returns a short description used for the --help command.
int getArgumentNumber ()
 Returns the number of accepted arguments for this command.
bool getAllowMultipleCalls ()
 Returns if the command can be called more than once in a single command line.
virtual bool execute (const std::vector< std::string > &parameters)=0
 Executes this command with the given parameters.
virtual bool checkParameters (const std::vector< std::string > &parameters)
 Checks the parameters for consistency and number.
virtual const std::string usage ()
 Returns the usage-part for a command. Used in the usage()-method from the commandlineparser.
virtual const std::string help ()
 Returns the help-part for a command. Used in the help()-method from the commandlineparser.

Protected Member Functions

template<class T >
cast (const std::string &s) throw (std::exception)
 Trys to cast the string value s to the templated parameter T if this fails, an std::exception will be thrown The conversion is done via an std::istringstream so it can only cast those types supported by the stream.
template<class T >
bool is (const std::string &s)
 Checks if there is a corresponding value from the string value s to the template type T.
bool isValueInSet (const std::string &value, const std::set< std::string > &set)
 Tests, if the 'value' is present in the given array.

Protected Attributes

std::string name_
 Name of the command used on commandline level.
std::string shortName_
 The short name of this command (usually an abbreviation.
std::string infoText_
 A description of the command; used in the help method.
std::string parameterList_
 The parameter list necessary for the usage method.
std::string loggerCat_
 Name used as a prefix for logging.
int argumentNum_
 Stores the number of arguments this command accepts.
bool allowMultipleCalls_
 Stores, if the command can be called multiple times in a single commandline.

Detailed Description

A command is a functionality which can be called via commandline on program startup.

As this class is virtual, you have to derive from it and add it (in the program) to a commandline parser. In the parser, the command's name and shortName must be unique.

There are other convenience classes to use, for example

See also:
Command_Boolean ,
Command_Float ,
Command_TwoFloat ,
Command_Integer ,
Command_TwoInteger ,
Command_String,
Command_TwoString which accept one or two of the listed parameter-types.

If -for example- you want to add a class which accepts one float value and one bool value, you would create a new class Command_FloatBool which derives from Command and implement some methods: Command_FloatBool(float* fp, bool* bp, const std::string& name, const std::string& shortName = "" , const std::string& infoText = "", const std::string& parameterList = "") and call the Command constructor with the arguments (name, shortName, infoText, parameterList, 2, false) If you want to be able to put the command multiple times on the commandline it would look like this: Command_FloatBool(std::vector<float>* fp, std::vector<bool>* bp, const std::string& name, const std::string& shortName = "" , const std::string& infoText = "", const std::string& parameterList = "") and the last argument for the super-ctor would be true. If you don't provide a vector as return value, you'll (obviously) get only one value back. This value will be the result of the latest call of the command.

If the argumentNum is equal to -1, the remaining parameters of the commandline will be used. Of course, only one of those commands can be used reasonably.

At least you have to write the methode

See also:
execute in which you do something with the passed values. For example just parse them and write them back to the member variables. If you want to do something fancy with
checkParameters , you have to write this too, because the version of this class only checks the parameters for the right count.

Definition at line 70 of file command.h.


Constructor & Destructor Documentation

Command ( const std::string &  name,
const std::string &  shortName = "",
const std::string &  infoText = "",
const std::string &  parameterList = "",
const int  argumentNum = 1,
const bool  allowMultipleCalls = false 
)

The constructor which just saves the arguments to out own member variables.

Parameters:
name The (long) name of the parameter. e.g. --command1
shortName The abbriviated name of the parameter. e.g. -c
infoText A short text (preferably one line) explaining what the command does. Used in the
See also:
displayHelp() method from the commandlineparser
Parameters:
parameterList A description which parameters are used. In the example with one float and one bool value, this would be <float value>=""> <bool value>="">. This is used in the
See also:
displayUsage() and
displayHelp() methods.
Parameters:
argumentNum The number of arguments this command accepts
allowMultipleCalls If is possible to have more than one instance of this command in one commandline? E.g. program1 -a dosomething -b somethingelse -a doitagain.

Definition at line 34 of file command.cpp.


Member Function Documentation

bool checkParameters ( const std::vector< std::string > &  parameters  )  [virtual]

Checks the parameters for consistency and number.

This version only tests for the number of parameters. If you want to test for other conditions, overwrite this method in a derived class

Parameters:
parameters The parameters which should be tested
Returns:
true, if the parameters are correct

Reimplemented in Command_LogLevel, MultipleCommand< T, U, V, W >, SingleCommand< T, U, V, W >, MultipleCommand< std::string >, SingleCommand< std::string >, and SingleCommand< int >.

Definition at line 95 of file command.cpp.

virtual bool execute ( const std::vector< std::string > &  parameters  )  [pure virtual]

Executes this command with the given parameters.

Parameters:
parameters The parameters needed for the execution of this command.
Returns:
true, if the execution was successful, false otherwise

Implemented in Command_LogLevel, MultipleCommand< T, U, V, W >, MultipleCommandZeroArguments, SingleCommand< T, U, V, W >, SingleCommandZeroArguments, MultipleCommand< std::string >, SingleCommand< std::string >, and SingleCommand< int >.

bool is ( const std::string &  s  )  [inline, protected]

Checks if there is a corresponding value from the string value s to the template type T.

As the method trys to convert the value, only those types are possible, which are supported by the std::istringstream

Definition at line 152 of file command.h.