Ocean
|
This class implements a manager for command arguments. More...
Data Structures | |
class | Manager |
This class implements a simple singleton holding the raw application's command arguments. More... | |
class | Parameter |
This class defines a named value with long and short name, with default parameter and description. More... | |
Public Types | |
template<typename T > | |
using | ArgumentT = std::basic_string< T > |
Definition of a string with either char or wchar_t . More... | |
template<typename T > | |
using | ArgumentsT = std::vector< ArgumentT< T > > |
Definition of a vector holding strings with either char or wchar_t . More... | |
Public Member Functions | |
CommandArguments () | |
Creates a new object. More... | |
CommandArguments (const std::string &applicationDescription) | |
Creates a new object with a description of the application. More... | |
bool | registerParameter (const std::string &longName, const std::string &shortName=std::string(), const std::string &description=std::string(), const Value &defaultValue=Value()) |
Registers a new named parameter which can be parsed as command argument. More... | |
bool | registerNamelessParameters (std::string &&description) |
Registers nameless parameters for the summary. More... | |
template<typename TChar > | |
bool | parse (const TChar *commandLine) |
Parses A given command line. More... | |
template<typename TChar > | |
bool | parse (const TChar *const *arguments, const size_t size, const bool skipFirstArgument=true) |
Parses the several command arguments. More... | |
bool | parse (const ArgumentsT< char > &separatedArguments) |
Parses the command arguments already separated into individual arguments. More... | |
bool | parse (const ArgumentsT< wchar_t > &separatedArguments) |
Parses the command arguments already separated into individual arguments. More... | |
Value | value (const std::string &longName, const bool allowDefaultValue=true, const size_t namelessValueIndex=size_t(-1)) const |
Returns the value of a specific parameter which has been parsed. More... | |
template<typename T > | |
T | value (const std::string &longName, const T &invalidValue, const bool allowDefaultValue, const size_t namelessValueIndex=size_t(-1)) const |
Returns the value of a specific parameter which has been parsed. More... | |
bool | hasValue (const std::string &longName, Value *value=nullptr, const bool allowDefaultValue=true, const size_t namelessValueIndex=size_t(-1)) const |
Checks whether a specific parameter value has been parsed, or whether a default value is defined. More... | |
template<typename T > | |
bool | hasValue (const std::string &longName, T &value, const bool allowDefaultValue=true, const size_t namelessValueIndex=size_t(-1)) const |
Checks whether a specific parameter value has been parsed with specific data type, or whether a default value is defined. More... | |
const std::vector< std::string > & | namelessValues () const |
Returns all nameless values which have been parsed. More... | |
std::string | makeSummary () |
Creates a summary of all possible command arguments. More... | |
template<> | |
bool | parse (const wchar_t *commandLine) |
template<> | |
bool | hasValue (const std::string &longName, bool &boolValue, const bool allowDefaultValue, const size_t namelessValueIndex) const |
template<> | |
bool | hasValue (const std::string &longName, int32_t &intValue, const bool allowDefaultValue, const size_t namelessValueIndex) const |
template<> | |
bool | hasValue (const std::string &longName, double &doubleValue, const bool allowDefaultValue, const size_t namelessValueIndex) const |
template<> | |
bool | hasValue (const std::string &longName, std::string &stringValue, const bool allowDefaultValue, const size_t namelessValueIndex) const |
Static Public Member Functions | |
template<typename TChar > | |
static ArgumentsT< TChar > | separateArguments (const TChar *commandLine) |
Parses the command line and returns the individual command elements. More... | |
Protected Types | |
typedef std::map< std::string, Parameter > | ParameterMap |
Definition of a map mapping long parameter names to parameter objects. More... | |
typedef std::unordered_map< std::string, std::string > | ShortToLongMap |
Definition of a map mapping short parameter names to long parameter names. More... | |
typedef std::unordered_map< std::string, Value > | ValueMap |
Definition of a map mapping long parameter names to values. More... | |
Protected Member Functions | |
template<typename TChar > | |
bool | isLongParameter (const TChar *parameter) |
Returns whether a given string is the start of a long parameter name (whether it starts with "--"). More... | |
template<typename TChar > | |
bool | isShortParameter (const TChar *parameter) |
Returns whether a given string is the start of a short parameter name (whether it starts with "--"). More... | |
Static Protected Member Functions | |
template<typename TChar > | |
static TChar | dashCharacter () |
Returns a dash character. More... | |
template<typename TChar > | |
static TChar | quoteCharacter () |
Returns a quote character. More... | |
template<typename TChar > | |
static TChar | spaceCharacter () |
Returns a space character. More... | |
template<typename TChar > | |
static TChar | backslashCharacter () |
Returns a backslash character. More... | |
template<> | |
char | dashCharacter () |
template<> | |
wchar_t | dashCharacter () |
template<> | |
char | quoteCharacter () |
template<> | |
wchar_t | quoteCharacter () |
template<> | |
char | spaceCharacter () |
template<> | |
wchar_t | spaceCharacter () |
template<> | |
char | backslashCharacter () |
template<> | |
wchar_t | backslashCharacter () |
Protected Attributes | |
std::string | applicationDescription_ |
The description text for the application using these command arguments. More... | |
ShortToLongMap | shortToLongMap_ |
The map mapping short parameter names to long parameter names. More... | |
ParameterMap | parameterMap_ |
The map mapping long parameter names to parameter objects. More... | |
ValueMap | valueMap_ |
The map mapping long parameter names to values. More... | |
std::vector< std::string > | namelessValues_ |
The vector of values without name. More... | |
std::string | descriptionNamelessParameters_ |
Optional description for nameless parameters. More... | |
This class implements a manager for command arguments.
Arguments can be configured, parsed, and acquired.
using Ocean::CommandArguments::ArgumentsT = std::vector<ArgumentT<T> > |
Definition of a vector holding strings with either char
or wchar_t
.
using Ocean::CommandArguments::ArgumentT = std::basic_string<T> |
Definition of a string with either char
or wchar_t
.
|
protected |
Definition of a map mapping long parameter names to parameter objects.
|
protected |
Definition of a map mapping short parameter names to long parameter names.
|
protected |
Definition of a map mapping long parameter names to values.
Ocean::CommandArguments::CommandArguments | ( | ) |
Creates a new object.
Ocean::CommandArguments::CommandArguments | ( | const std::string & | applicationDescription | ) |
Creates a new object with a description of the application.
applicationDescription | A description text for the application using these command arguments. |
|
inlinestaticprotected |
Returns a backslash character.
TChar | The character type, char or wchar_t |
|
inlinestaticprotected |
|
inlinestaticprotected |
|
inlinestaticprotected |
Returns a dash character.
TChar | The character type, char or wchar_t |
|
inlinestaticprotected |
|
inlinestaticprotected |
|
inline |
|
inline |
|
inline |
|
inline |
bool Ocean::CommandArguments::hasValue | ( | const std::string & | longName, |
T & | value, | ||
const bool | allowDefaultValue = true , |
||
const size_t | namelessValueIndex = size_t(-1) |
||
) | const |
Checks whether a specific parameter value has been parsed with specific data type, or whether a default value is defined.
longName | The long name of the parameter value to check, must be valid |
value | The resulting value of the parameter |
allowDefaultValue | True, to allow a default value to count as parsed value; False, to check for actually parsed values only |
namelessValueIndex | Optional explicit index of a nameless value which is used in case the named value does not exist, -1 to avoid using a nameless value |
T | The data type of the value, 'Value' by default, can also be one of these specific values 'bool', 'int32_t', 'double' (while integers are accepted as well), 'std::string' |
bool Ocean::CommandArguments::hasValue | ( | const std::string & | longName, |
Value * | value = nullptr , |
||
const bool | allowDefaultValue = true , |
||
const size_t | namelessValueIndex = size_t(-1) |
||
) | const |
Checks whether a specific parameter value has been parsed, or whether a default value is defined.
longName | The long name of the parameter value to check, must be valid |
value | Optional resulting value of the parameter |
allowDefaultValue | True, to allow a default value to count as parsed value; False, to check for actually parsed values only |
namelessValueIndex | Optional explicit index of a nameless value which is used in case the named value does not exist, -1 to avoid using a nameless value |
|
protected |
Returns whether a given string is the start of a long parameter name (whether it starts with "--").
The string must be zero terminated.
parameter | The string to be checked, must be valid |
TChar | The character type, char or wchar_t |
|
protected |
Returns whether a given string is the start of a short parameter name (whether it starts with "--").
The string must be zero terminated.
parameter | The string to be checked, must be valid |
TChar | The character type, char or wchar_t |
std::string Ocean::CommandArguments::makeSummary | ( | ) |
Creates a summary of all possible command arguments.
|
inline |
Returns all nameless values which have been parsed.
bool Ocean::CommandArguments::parse | ( | const ArgumentsT< char > & | separatedArguments | ) |
Parses the command arguments already separated into individual arguments.
separatedArguments | The individual arguments to be parsed |
bool Ocean::CommandArguments::parse | ( | const ArgumentsT< wchar_t > & | separatedArguments | ) |
Parses the command arguments already separated into individual arguments.
separatedArguments | The individual arguments to be parsed |
|
inline |
Parses A given command line.
The entire command line may contain several commands separated by space characters.
Command elements which contain space characters must be surrounded by quotation marks.
However, all surrounding quotation marks will be removed during the parsing process.
The first argument should not be the filename (and path) of the executable.
commandLine | The command line to be parsed, must be valid |
TChar | The character type, char or wchar_t |
bool Ocean::CommandArguments::parse | ( | const TChar *const * | arguments, |
const size_t | size, | ||
const bool | skipFirstArgument = true |
||
) |
Parses the several command arguments.
arguments | The arguments to be parsed, an be nullptr if size == 0 |
size | The number of arguments, with size [0, infinity) |
skipFirstArgument | True, to skip the first argument (as this is commonly the path of the executable); False, to parse the first parameter as well |
TChar | The character type, char or wchar_t |
|
inline |
|
inlinestaticprotected |
Returns a quote character.
TChar | The character type, char or wchar_t |
|
inlinestaticprotected |
|
inlinestaticprotected |
bool Ocean::CommandArguments::registerNamelessParameters | ( | std::string && | description | ) |
Registers nameless parameters for the summary.
description | The description for nameless parameters, must not be empty |
bool Ocean::CommandArguments::registerParameter | ( | const std::string & | longName, |
const std::string & | shortName = std::string() , |
||
const std::string & | description = std::string() , |
||
const Value & | defaultValue = Value() |
||
) |
Registers a new named parameter which can be parsed as command argument.
longName | The long name of the parameter, must be valid, must start with a alphabetic character |
shortName | Optional short name of the parameter, can be empty, must start with a alphabetic character if defined |
description | Optional description of the parameter, can be empty |
defaultValue | Optional default value of the parameter, can be invalid |
|
static |
Parses the command line and returns the individual command elements.
The entire command line may contain several commands separated by space characters.
Command elements which contain space characters must be surrounded by quotation marks.
However, all surrounding quotation marks will be removed during the parsing process.
The first argument should not be the filename (and path) of the executable.
commandLine | Command line to be parsed |
TChar | The character type, char or wchar_t |
|
inlinestaticprotected |
Returns a space character.
TChar | The character type, char or wchar_t |
|
inlinestaticprotected |
|
inlinestaticprotected |
Value Ocean::CommandArguments::value | ( | const std::string & | longName, |
const bool | allowDefaultValue = true , |
||
const size_t | namelessValueIndex = size_t(-1) |
||
) | const |
Returns the value of a specific parameter which has been parsed.
Below an example of how to use this function:
longName | The long name of the parameter for which the value will be returned, must be valid |
allowDefaultValue | True, to return the default value in case no actual value has been parsed; False, to return an invalid value if no actual value has been parsed |
namelessValueIndex | Optional explicit index of a nameless value which is used in case the named value does not exist, -1 to avoid using a nameless value |
T Ocean::CommandArguments::value | ( | const std::string & | longName, |
const T & | invalidValue, | ||
const bool | allowDefaultValue, | ||
const size_t | namelessValueIndex = size_t(-1) |
||
) | const |
Returns the value of a specific parameter which has been parsed.
Below an example of how to use this function:
longName | The long name of the parameter for which the value will be returned, must be valid |
invalidValue | The resulting value in case the parameter was not parsed |
allowDefaultValue | True, to return the default value in case no actual value has been parsed; False, to return an invalid value if no actual value has been parsed |
namelessValueIndex | Optional explicit index of a nameless value which is used in case the named value does not exist, -1 to avoid using a nameless value |
|
protected |
The description text for the application using these command arguments.
|
protected |
Optional description for nameless parameters.
|
protected |
The vector of values without name.
|
protected |
The map mapping long parameter names to parameter objects.
|
protected |
The map mapping short parameter names to long parameter names.
|
protected |
The map mapping long parameter names to values.