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>
128 inline const std::string&
longName()
const;
134 inline const std::string&
shortName()
const;
176 using ValueMap = std::unordered_map<std::string, Value>;
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;
331 template <
typename TChar>
343 template <
typename TChar>
353 template <
typename TChar>
361 template <
typename TChar>
369 template <
typename TChar>
377 template <
typename TChar>
385 template <
typename TChar>
429template <
typename TChar>
432 if (commandLine ==
nullptr)
443 if (commandLine ==
nullptr)
451 separatedArguments.reserve(wSeparatedArguments.size());
458 return parse(separatedArguments);
461template <
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);
489T
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))
501inline bool CommandArguments::hasValue(
const std::string& longName,
bool& boolValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
504 if (!
hasValue(longName, &
value, allowDefaultValue, namelessValueIndex))
514 boolValue =
value.boolValue();
520inline 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))
528 int64_t signedValue64 = 0;
532 signedValue64 =
value.intValue();
534 else if (
value.isInt64())
536 signedValue64 =
value.int64Value();
543 if (signedValue64 < int64_t(std::numeric_limits<int32_t>::lowest()) || signedValue64 > int64_t(std::numeric_limits<int32_t>::max()))
548 intValue = int32_t(signedValue64);
554inline bool CommandArguments::hasValue(
const std::string& longName, uint32_t& intValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
557 if (!
hasValue(longName, &
value, allowDefaultValue, namelessValueIndex))
562 int64_t signedValue64 = 0;
566 signedValue64 =
value.intValue();
568 else if (
value.isInt64())
570 signedValue64 =
value.int64Value();
577 if (signedValue64 < 0 || signedValue64 > int64_t(std::numeric_limits<uint32_t>::max()))
582 intValue = uint32_t(signedValue64);
588inline bool CommandArguments::hasValue(
const std::string& longName, int64_t& intValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
591 if (!
hasValue(longName, &
value, allowDefaultValue, namelessValueIndex))
598 intValue =
value.intValue();
600 else if (
value.isInt64())
602 intValue =
value.int64Value();
613inline bool CommandArguments::hasValue(
const std::string& longName, uint64_t& intValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
616 if (!
hasValue(longName, &
value, allowDefaultValue, namelessValueIndex))
621 int64_t signedValue64 = 0;
625 signedValue64 =
value.intValue();
627 else if (
value.isInt64())
629 signedValue64 =
value.int64Value();
636 if (signedValue64 < 0)
641 intValue = uint64_t(signedValue64);
647inline bool CommandArguments::hasValue(
const std::string& longName,
double& doubleValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
650 if (!
hasValue(longName, &
value, allowDefaultValue, namelessValueIndex))
655 if (!
value.isFloat64(
true ))
660 doubleValue =
value.float64Value(
true );
666inline bool CommandArguments::hasValue(
const std::string& longName, std::string& stringValue,
const bool allowDefaultValue,
const size_t namelessValueIndex)
const
669 if (!
hasValue(longName, &
value, allowDefaultValue, namelessValueIndex))
674 if (!
value.isString())
679 stringValue =
value.stringValue();
687 ocean_assert(
false &&
"Invalid data type!");
697template <
typename TChar>
704 return applicationArguments;
709 while (!line.empty())
728 applicationArguments.push_back(line.substr(1, posEndQuote - 1));
729 line = line.substr(posEndQuote + 1);
737 return applicationArguments;
746 ocean_assert(line.length() != 0);
748 applicationArguments.push_back(line);
754 line = line.substr(1);
758 applicationArguments.push_back(line.substr(0, posSpace));
759 line = line.substr(posSpace + 1);
763 return applicationArguments;
766template <
typename TChar>
769 ocean_assert(parameter !=
nullptr);
776template <
typename TChar>
779 ocean_assert(parameter !=
nullptr);
size_t size_
The number of command arguments, with range [0, infinity).
Definition CommandArguments.h:100
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.
const char *const * argumentsChar_
The command arguments, nullptr if no arguments are defined.
Definition CommandArguments.h:94
Lock lock_
The manager's lock.
Definition CommandArguments.h:103
const wchar_t *const * argumentsWchar_
The command arguments, nullptr if no arguments are defined.
Definition CommandArguments.h:97
const TChar *const * rawArguments() const
Returns the command arguments of the application.
Manager()
Protected default constructor.
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
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:767
std::unordered_map< std::string, std::string > ShortToLongMap
Definition of a map mapping short parameter names to long parameter names.
Definition CommandArguments.h:171
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:777
static ArgumentsT< TChar > separateArguments(const TChar *commandLine)
Parses the command line and returns the individual command elements.
Definition CommandArguments.h:698
bool parse(const TChar *commandLine)
Parses A given command line.
Definition CommandArguments.h:430
static TChar dashCharacter()
Returns a dash character.
std::vector< ArgumentT< T > > ArgumentsT
Definition of a vector holding strings with either char or wchar_t.
Definition CommandArguments.h:43
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.
Strings namelessValues_
The vector of values without name.
Definition CommandArguments.h:403
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
CommandArguments()
Creates a new object.
const Strings & namelessValues() const
Returns all nameless values which have been parsed.
Definition CommandArguments.h:692
bool parse(const ArgumentsT< wchar_t > &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
static TChar quoteCharacter()
Returns a quote character.
std::unordered_map< std::string, Value > ValueMap
Definition of a map mapping long parameter names to values.
Definition CommandArguments.h:176
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
static TChar backslashCharacter()
Returns a backslash character.
bool parse(const ArgumentsT< char > &separatedArguments)
Parses the command arguments already separated into individual arguments.
ValueMap valueMap_
The map mapping long parameter names to values.
Definition CommandArguments.h:400
static TChar spaceCharacter()
Returns a space character.
This class implements a recursive lock object.
Definition Lock.h:31
static MessageObject error()
Returns the message for error messages.
Definition Messenger.h:1095
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
std::vector< std::string > Strings
Definition of a vector holding strings.
Definition Base.h:162
The namespace covering the entire Ocean framework.
Definition Accessor.h:15