The command arguments parser base class. More...

#include "murl_util_environment.h"

Classes

class  ColorParameter
 The color parameter class. More...
 
class  DoubleArrayParameter
 The floating point array parameter class. More...
 
class  DoubleParameter
 The floating point parameter class. More...
 
class  EnumArrayParameter
 The enumeration array parameter template class. More...
 
class  EnumParameter
 The enumeration parameter template class. More...
 
class  Parameter
 The parameter base class. More...
 
class  SInt32ArrayParameter
 The integer array parameter class. More...
 
class  SInt32Parameter
 The integer parameter class. More...
 
class  StringArrayParameter
 The string array parameter class. More...
 
class  StringListArrayParameter
 The string list array parameter class. More...
 
class  StringPairArrayParameter
 The string pair array parameter class. More...
 
class  StringParameter
 The string parameter class. More...
 
class  SwitchParameter
 The boolean parameter class. More...
 

Public Member Functions

 Environment ()
 The default constructor.
 
virtual ~Environment ()
 The destructor.
 
virtual Bool Create (SInt32 argC, const char **argV)
 Initialize and parse the command arguments. More...
 
virtual Bool Create (SInt32 argC, char **argV)
 Forward to Create(SInt32 argC, const char** argV). More...
 
virtual Bool Create (const StringArray &args)
 Initialize and parse the command arguments. More...
 
const StringArrayGetFreeParameters () const
 Get the free parameters string array. More...
 
virtual Bool Validate ()
 Check if all mandatory parameters are set. More...
 
virtual String GetUsage () const
 Get the usage string of all registered parameters. More...
 
virtual const StringGetLastError () const
 Get the last error parameter. More...
 

Protected Types

enum  ParameterType { PARAMETER_TYPE_MANDATORY , PARAMETER_TYPE_OPTIONAL , NUM_PARAMETER_TYPES }
 Definition of the parameter types. More...
 

Protected Member Functions

virtual void Init ()=0
 Overload to initialize and register parameters.
 
virtual void DeInit ()
 Overload to de-initialize, default implementation is empty.
 
template<class ParamType >
const ParamType * Register (ParamType *param)
 Register a new allocated parameter object. More...
 
virtual Bool ParseParameters (SInt32 argC, const char **argV)
 Parse the command arguments and convert the argument values into the corresponding registered parameter objects. More...
 
void SetAcceptFreeParameters (Bool accept, ParameterType type)
 Set the free parameters acceptance. More...
 
template<class ParamType >
const ParamType * Register (ParameterType type, const String &longId, const String &shortId, const String &description)
 Create and register a parameter class. More...
 
template<class ParamType , class DataType >
const ParamType * Register (ParameterType type, const String &longId, const String &shortId, const String &description, const DataType &defaultValue)
 Create and register a parameter class with a default value. More...
 
template<class ParamType , class EnumType >
const ParamType * Register (ParameterType type, const String &longId, const String &shortId, const String &description, const Enum< EnumType > &enumClass)
 Create and register an enumeration parameter class. More...
 
template<class ParamType , class EnumType >
const ParamType * Register (ParameterType type, const String &longId, const String &shortId, const String &description, const Enum< EnumType > &enumClass, EnumType defaultValue)
 Create and register an enumeration parameter class with a default value. More...
 

Protected Attributes

Array< Parameter::AutoPtrmParameters
 The registered parameter objects.
 
Bool mAcceptFreeParameters
 The accept free parameters, default false.
 
ParameterType mFreeParametersType
 The free parameters type, default PARAMETER_TYPE_OPTIONAL.
 
StringArray mFreeParameters
 The free parameters.
 
String mLastError
 The last error parameter.
 

Detailed Description

The command arguments parser base class.

To create and register command arguments, you must derive from the base class and implement Init() e.g.:

class MyEnvironment : public Util::Environment
{
public:
const SwitchParameter* mMySwitchParam;
const StringArrayParameter* mMyFilesParam;
protected:
virtual void Init()
{
mMySwitchParam = Register<SwitchParameter>(PARAMETER_TYPE_OPTIONAL, "switch", "s", "Enable My Switch");
mMyFilesParam = Register<StringArrayParameter>(PARAMETER_TYPE_MANDATORY, "file", "f", "Specify files(s)");
}
};
@ PARAMETER_TYPE_MANDATORY
The mandatory parameter type.
Definition: murl_util_environment.h:163
@ PARAMETER_TYPE_OPTIONAL
The optional parameter type.
Definition: murl_util_environment.h:165
virtual void Init()=0
Overload to initialize and register parameters.

To get the command arguments simply call Create(), e.g. assuming an instance mMyEnvironment exists:

if (!mMyEnvironment.Create(argC, argV))
{
System::Console::Print(mEnvironment.GetUsage());
return false;
}
if (mMyEnvironment.mMySwitchParam->mValue)
{
// parameter --switch or -s was specified
}
for (UInt32 i = 0; i < mMyEnvironment.mMyFilesParam->mValue.GetCount(); i++)
{
// parameter --file or -f was specified, e.g. -f aFile1 -f aFile2 --file aFile3
mMyEnvironment.mMyFilesParam->mValue[i]; // get the file name string
}
static Bool Print(const Char *format,...)
Print formatted variadic arguments.
MurlUInt32 UInt32
Unsigned 32 bit integer data type.
Definition: murl_types.h:136

Member Enumeration Documentation

◆ ParameterType

Definition of the parameter types.

Enumerator
PARAMETER_TYPE_MANDATORY 

The mandatory parameter type.

PARAMETER_TYPE_OPTIONAL 

The optional parameter type.

Member Function Documentation

◆ Create() [1/3]

virtual Bool Murl::Util::Environment::Create ( SInt32  argC,
const char **  argV 
)
virtual

Initialize and parse the command arguments.

Calls DeInit(), deletes all registered parameters, calls Init() and ParseParameters().

Parameters
argCThe number of arguments.
argVThe argument string values.
Returns
false if ParseParameters() failed, otherwise the result from IsValid() is returned.

◆ Create() [2/3]

virtual Bool Murl::Util::Environment::Create ( SInt32  argC,
char **  argV 
)
virtual

Forward to Create(SInt32 argC, const char** argV).

Parameters
argCThe number of arguments.
argVThe argument string values.
Returns
false if ParseParameters() failed, otherwise the result from IsValid() is returned.

◆ Create() [3/3]

virtual Bool Murl::Util::Environment::Create ( const StringArray args)
virtual

Initialize and parse the command arguments.

See Create(SInt32 argC, const char** argV).

Parameters
argsA string array containing the arguments.
Returns
false if ParseParameters() failed, otherwise the result from IsValid() is returned.

◆ GetFreeParameters()

const StringArray& Murl::Util::Environment::GetFreeParameters ( ) const

Get the free parameters string array.

Free parameters are parameters which have no leading '-' option, e.g. a list of file names. To accept this kind of parameters SetAcceptFreeParameters() must be called before Create() or during Init().

Returns
The free parameters string array.

◆ Validate()

virtual Bool Murl::Util::Environment::Validate ( )
virtual

Check if all mandatory parameters are set.

Returns
true if all mandatory parameters are set.

◆ GetUsage()

virtual String Murl::Util::Environment::GetUsage ( ) const
virtual

Get the usage string of all registered parameters.

Returns
The usage string of all registered parameters.

◆ GetLastError()

virtual const String& Murl::Util::Environment::GetLastError ( ) const
virtual

Get the last error parameter.

Returns
The last error parameter string.

◆ Register() [1/5]

template<class ParamType >
const ParamType* Murl::Util::Environment::Register ( ParamType *  param)
inlineprotected

Register a new allocated parameter object.

All registered parameter objects are deleted automatically at environment class destruction or when calling Create().

Parameters
paramThe new allocated parameter object.
Returns
The registered parameter object.

References mParameters.

Referenced by Register().

◆ ParseParameters()

virtual Bool Murl::Util::Environment::ParseParameters ( SInt32  argC,
const char **  argV 
)
protectedvirtual

Parse the command arguments and convert the argument values into the corresponding registered parameter objects.

Parameters
argCThe number of arguments.
argVThe argument string values.
Returns
true if successful.

◆ SetAcceptFreeParameters()

void Murl::Util::Environment::SetAcceptFreeParameters ( Bool  accept,
ParameterType  type 
)
protected

Set the free parameters acceptance.

Free parameters are parameters which have no leading '-' option, e.g. a list of file names. To accept this kind of parameters this method must be called before Create() or during Init().
Use GetFreeParameters() to read the parameters after parsing.

Parameters
accepttrue to accept free parameters.
typeThe type of the parameter.

◆ Register() [2/5]

template<class ParamType >
const ParamType* Murl::Util::Environment::Register ( ParameterType  type,
const String longId,
const String shortId,
const String description 
)
inlineprotected

Create and register a parameter class.

Parameters
typeThe type of the parameter.
longIdThe long parameter identifier string.
shortIdThe short parameter identifier string.
descriptionThe parameter description string.
Template Parameters
ParamTypeThe parameter class type to create.
Returns
The registered parameter object.

References Register().

◆ Register() [3/5]

template<class ParamType , class DataType >
const ParamType* Murl::Util::Environment::Register ( ParameterType  type,
const String longId,
const String shortId,
const String description,
const DataType &  defaultValue 
)
inlineprotected

Create and register a parameter class with a default value.

Parameters
typeThe type of the parameter.
longIdThe long parameter identifier string.
shortIdThe short parameter identifier string.
descriptionThe parameter description string.
defaultValueThe parameter default value.
Template Parameters
ParamTypeThe parameter class type to create.
DataTypeThe default value class type.
Returns
The registered parameter object.

References Register().

◆ Register() [4/5]

template<class ParamType , class EnumType >
const ParamType* Murl::Util::Environment::Register ( ParameterType  type,
const String longId,
const String shortId,
const String description,
const Enum< EnumType > &  enumClass 
)
inlineprotected

Create and register an enumeration parameter class.

Parameters
typeThe type of the parameter.
longIdThe long parameter identifier string.
shortIdThe short parameter identifier string.
descriptionThe parameter description string.
enumClassThe enumeration string mapping class.
Template Parameters
ParamTypeThe parameter class type to create.
EnumTypeThe enumeration class type.
Returns
The registered parameter object.

References Register().

◆ Register() [5/5]

template<class ParamType , class EnumType >
const ParamType* Murl::Util::Environment::Register ( ParameterType  type,
const String longId,
const String shortId,
const String description,
const Enum< EnumType > &  enumClass,
EnumType  defaultValue 
)
inlineprotected

Create and register an enumeration parameter class with a default value.

Parameters
typeThe type of the parameter.
longIdThe long parameter identifier string.
shortIdThe short parameter identifier string.
descriptionThe parameter description string.
enumClassThe enumeration string mapping class.
defaultValueThe parameter default value.
Template Parameters
ParamTypeThe parameter class type to create.
EnumTypeThe enumeration class type.
Returns
The registered parameter object.

References Register().


The documentation for this class was generated from the following file:
  • murl_util_environment.h


Copyright © 2011-2024 Spraylight GmbH.