8 #ifndef META_OCEAN_BASE_COMMAND_ARGUMENTS_H
9 #define META_OCEAN_BASE_COMMAND_ARGUMENTS_H
19 #include <unordered_map>
74 template <
typename TChar =
char>
94 const char*
const* argumentsChar_ =
nullptr;
97 const wchar_t*
const* argumentsWchar_ =
nullptr;
122 Parameter(
const std::string& longName,
const std::string& shortName,
const std::string& description,
const Value& defaultValue);
128 inline const std::string& longName()
const;
134 inline const std::string& shortName()
const;
140 inline const std::string& description()
const;
146 inline const Value& defaultValue()
const;
176 typedef std::unordered_map<std::string, Value>
ValueMap;
199 bool registerParameter(
const std::string& longName,
const std::string& shortName = std::string(),
const std::string& description = std::string(),
const Value& defaultValue =
Value());
218 template <
typename TChar>
219 inline bool parse(
const TChar* commandLine);
229 template <
typename TChar>
230 bool parse(
const TChar*
const* arguments,
const size_t size,
const bool skipFirstArgument =
true);
265 Value value(
const std::string& longName,
const bool allowDefaultValue =
true,
const size_t namelessValueIndex =
size_t(-1))
const;
284 template <
typename T>
285 T value(
const std::string& longName,
const T& invalidValue,
const bool allowDefaultValue,
const size_t namelessValueIndex =
size_t(-1))
const;
295 bool hasValue(
const std::string& longName,
Value* value =
nullptr,
const bool allowDefaultValue =
true,
const size_t namelessValueIndex =
size_t(-1))
const;
306 template <
typename T>
307 bool hasValue(
const std::string& longName, T& value,
const bool allowDefaultValue =
true,
const size_t namelessValueIndex =
size_t(-1))
const;
313 inline const std::vector<std::string>& namelessValues()
const;
331 template <
typename TChar>
343 template <
typename TChar>
344 bool isLongParameter(
const TChar* parameter);
353 template <
typename TChar>
354 bool isShortParameter(
const TChar* parameter);
361 template <
typename TChar>
369 template <
typename TChar>
377 template <
typename TChar>
385 template <
typename TChar>
426 return defaultValue_;
429 template <
typename TChar>
432 if (commandLine ==
nullptr)
441 inline bool CommandArguments::parse<wchar_t>(
const wchar_t* commandLine)
443 if (commandLine ==
nullptr)
451 separatedArugments.reserve(wSeparatedArugments.size());
458 return parse(separatedArugments);
461 template <
typename TChar>
469 if (arguments ==
nullptr)
471 ocean_assert(
false &&
"Invalid arguments!");
476 separatedArguments.reserve(size);
478 const size_t nStart = skipFirstArgument ? 1 : 0;
480 for (
size_t n = nStart; n < size; ++n)
485 return parse(separatedArguments);
488 template <
typename T>
489 T
CommandArguments::value(
const std::string& longName,
const T& invalidValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
492 if (hasValue<T>(longName, validValue, allowDefaultValue, namelessValueIndex))
501 inline bool CommandArguments::hasValue(
const std::string& longName,
bool& boolValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
504 if (!
hasValue(longName, &
value, allowDefaultValue, namelessValueIndex))
520 inline bool CommandArguments::hasValue(
const std::string& longName, int32_t& intValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
523 if (!
hasValue(longName, &
value, allowDefaultValue, namelessValueIndex))
539 inline bool CommandArguments::hasValue(
const std::string& longName,
double& doubleValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
542 if (!
hasValue(longName, &
value, allowDefaultValue, namelessValueIndex))
558 inline bool CommandArguments::hasValue(
const std::string& longName, std::string& stringValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
561 if (!
hasValue(longName, &
value, allowDefaultValue, namelessValueIndex))
581 template <
typename TChar>
588 return applicationArguments;
593 while (!line.empty())
606 if (line[posEndQuote - 1] == backslashCharacter<TChar>())
608 posEndQuote = line.find(quoteCharacter<TChar>(), posEndQuote + 1);
612 applicationArguments.push_back(line.substr(1, posEndQuote - 1));
613 line = line.substr(posEndQuote + 1);
621 return applicationArguments;
630 ocean_assert(line.length() != 0);
632 applicationArguments.push_back(line);
638 line = line.substr(1);
642 applicationArguments.push_back(line.substr(0, posSpace));
643 line = line.substr(posSpace + 1);
647 return applicationArguments;
650 template <
typename TChar>
653 ocean_assert(parameter !=
nullptr);
657 return parameter[0] == dashCharacter<TChar>() && parameter[1] == dashCharacter<TChar>() && parameter[2] != dashCharacter<TChar>() && std::isalpha(
int(parameter[2])) != 0;
660 template <
typename TChar>
663 ocean_assert(parameter !=
nullptr);
667 return parameter[0] == dashCharacter<TChar>() && std::isalpha(
int(parameter[1])) != 0;
671 inline char CommandArguments::dashCharacter<char>()
677 inline wchar_t CommandArguments::dashCharacter<wchar_t>()
683 inline char CommandArguments::quoteCharacter<char>()
689 inline wchar_t CommandArguments::quoteCharacter<wchar_t>()
695 inline char CommandArguments::spaceCharacter<char>()
701 inline wchar_t CommandArguments::spaceCharacter<wchar_t>()
707 inline char CommandArguments::backslashCharacter<char>()
713 inline wchar_t CommandArguments::backslashCharacter<wchar_t>()
This class implements a simple singleton holding the raw application's command arguments.
Definition: CommandArguments.h:49
size_t size() const
Returns the number of command arguments of the application.
bool setRawArguments(const wchar_t *const *arguments, const size_t size)
Sets/registers the command arguments of the application.
bool setRawArguments(const char *const *arguments, const size_t size)
Sets/registers the command arguments of the application.
Lock lock_
The manager's lock.
Definition: CommandArguments.h:103
Manager()
Protected default constructor.
const TChar *const * rawArguments() const
Returns the command arguments of the application.
This class defines a named value with long and short name, with default parameter and description.
Definition: CommandArguments.h:112
Parameter(const std::string &longName, const std::string &shortName, const std::string &description, const Value &defaultValue)
Creates a new parameter object.
const std::string & description() const
Returns the description of this parameter.
Definition: CommandArguments.h:419
std::string longName_
The long name of the parameter.
Definition: CommandArguments.h:151
Value defaultValue_
The default value of this parameter, can be invalid.
Definition: CommandArguments.h:160
const Value & defaultValue() const
Returns the default value of this parameter.
Definition: CommandArguments.h:424
const std::string & shortName() const
Returns the short name of this parameter.
Definition: CommandArguments.h:414
const std::string & longName() const
Returns the long name of this parameter.
Definition: CommandArguments.h:409
std::string shortName_
The short name of the parameter, can be empty.
Definition: CommandArguments.h:154
std::string description_
The description of this parameter, can be empty.
Definition: CommandArguments.h:157
This class implements a manager for command arguments.
Definition: CommandArguments.h:30
std::string makeSummary()
Creates a summary of all possible command arguments.
ShortToLongMap shortToLongMap_
The map mapping short parameter names to long parameter names.
Definition: CommandArguments.h:394
bool registerNamelessParameters(std::string &&description)
Registers nameless parameters for the summary.
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.
bool isLongParameter(const TChar *parameter)
Returns whether a given string is the start of a long parameter name (whether it starts with "--").
Definition: CommandArguments.h:651
const std::vector< std::string > & namelessValues() const
Returns all nameless values which have been parsed.
Definition: CommandArguments.h:576
bool isShortParameter(const TChar *parameter)
Returns whether a given string is the start of a short parameter name (whether it starts with "--").
Definition: CommandArguments.h:661
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,...
static ArgumentsT< TChar > separateArguments(const TChar *commandLine)
Parses the command line and returns the individual command elements.
Definition: CommandArguments.h:582
bool parse(const TChar *commandLine)
Parses A given command line.
Definition: CommandArguments.h:430
static TChar dashCharacter()
Returns a dash character.
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.
std::string descriptionNamelessParameters_
Optional description for nameless parameters.
Definition: CommandArguments.h:406
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.
ParameterMap parameterMap_
The map mapping long parameter names to parameter objects.
Definition: CommandArguments.h:397
std::string applicationDescription_
The description text for the application using these command arguments.
Definition: CommandArguments.h:391
std::vector< std::string > namelessValues_
The vector of values without name.
Definition: CommandArguments.h:403
CommandArguments()
Creates a new object.
bool parse(const ArgumentsT< wchar_t > &separatedArguments)
Parses the command arguments already separated into individual arguments.
static TChar quoteCharacter()
Returns a quote character.
CommandArguments(const std::string &applicationDescription)
Creates a new object with a description of the application.
std::basic_string< T > ArgumentT
Definition of a string with either char or wchar_t.
Definition: CommandArguments.h:37
std::vector< ArgumentT< T > > ArgumentsT
Definition of a vector holding strings with either char or wchar_t.
Definition: CommandArguments.h:43
static TChar backslashCharacter()
Returns a backslash character.
std::unordered_map< std::string, Value > ValueMap
Definition of a map mapping long parameter names to values.
Definition: CommandArguments.h:176
bool parse(const ArgumentsT< char > &separatedArguments)
Parses the command arguments already separated into individual arguments.
std::map< std::string, Parameter > ParameterMap
Definition of a map mapping long parameter names to parameter objects.
Definition: CommandArguments.h:166
ValueMap valueMap_
The map mapping long parameter names to values.
Definition: CommandArguments.h:400
static TChar spaceCharacter()
Returns a space character.
std::unordered_map< std::string, std::string > ShortToLongMap
Definition of a map mapping short parameter names to long parameter names.
Definition: CommandArguments.h:171
This class implements a recursive lock object.
Definition: Lock.h:31
static MessageObject error()
Returns the message for error messages.
Definition: Messenger.h:1074
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
static std::string toAString(const char value)
Converts a value to a string with 8bit character.
This class implements a type independent value.
Definition: Value.h:23
bool isString() const
Returns whether this object holds a string value as internal data.
Definition: Value.h:395
bool isFloat64(const bool allowIntAndFloat=false) const
Returns whether this object holds a 64 bit float value as internal data.
Definition: Value.h:383
bool boolValue() const
Returns the internal value as a boolean.
int32_t intValue() const
Returns the internal value as integer.
double float64Value(const bool allowIntAndFloat=false) const
Returns the internal value as 64 bit float.
bool isInt() const
Returns whether this object holds an integer value as internal data.
Definition: Value.h:368
std::string stringValue() const
Returns the internal value as string.
bool isBool() const
Returns whether this object holds an boolean value as internal data.
Definition: Value.h:363
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15