Ocean
Loading...
Searching...
No Matches
CommandArguments.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_BASE_COMMAND_ARGUMENTS_H
9#define META_OCEAN_BASE_COMMAND_ARGUMENTS_H
10
11#include "ocean/base/Base.h"
12#include "ocean/base/Lock.h"
15#include "ocean/base/String.h"
16#include "ocean/base/Value.h"
17
18#include <numeric>
19#include <unordered_map>
20
21namespace Ocean
22{
23
24/**
25 * This class implements a manager for command arguments.
26 * Arguments can be configured, parsed, and acquired.
27 * @ingroup base
28 */
29class OCEAN_BASE_EXPORT CommandArguments
30{
31 public:
32
33 /**
34 * Definition of a string with either `char` or `wchar_t`.
35 */
36 template <typename T>
37 using ArgumentT = std::basic_string<T>;
38
39 /**
40 * Definition of a vector holding strings with either `char` or `wchar_t`.
41 */
42 template <typename T>
43 using ArgumentsT = std::vector<ArgumentT<T>>;
44
45 /**
46 * This class implements a simple singleton holding the raw application's command arguments.
47 */
48 class OCEAN_BASE_EXPORT Manager : public Singleton<Manager>
49 {
50 friend class Singleton<Manager>;
51
52 public:
53
54 /**
55 * Sets/registers the command arguments of the application.
56 * @param arguments The arguments to set, ensure that the pointer is valid as long as the application exists, must be valid
57 * @param size The number of provided command arguments, with range [1, infinity)
58 */
59 bool setRawArguments(const char* const* arguments, const size_t size);
60
61 /**
62 * Sets/registers the command arguments of the application.
63 * @param arguments The arguments to set, ensure that the pointer is valid as long as the application exists, must be valid
64 * @param size The number of provided command arguments, with range [1, infinity)
65 */
66 bool setRawArguments(const wchar_t* const* arguments, const size_t size);
67
68 /**
69 * Returns the command arguments of the application.
70 * @return The application's command arguments, nullptr if the arguments have not been set via setArguments().
71 * @tparam TChar The character type, `char` or `wchar_t`
72 * @see setArguments().
73 */
74 template <typename TChar = char>
75 const TChar* const* rawArguments() const;
76
77 /**
78 * Returns the number of command arguments of the application.
79 * @return The application's number of command arguments, 0 if arguments have not been set via setArguments().
80 * @see setArguments().
81 */
82 size_t size() const;
83
84 protected:
85
86 /**
87 * Protected default constructor.
88 */
90
91 protected:
92
93 /// The command arguments, nullptr if no arguments are defined.
94 const char* const* argumentsChar_ = nullptr;
95
96 /// The command arguments, nullptr if no arguments are defined.
97 const wchar_t* const* argumentsWchar_ = nullptr;
98
99 /// The number of command arguments, with range [0, infinity).
100 size_t size_ = 0;
101
102 /// The manager's lock.
103 mutable Lock lock_;
104 };
105
106 protected:
107
108 /**
109 * This class defines a named value with long and short name, with default parameter and description.
110 */
112 {
113 public:
114
115 /**
116 * Creates a new parameter object.
117 * @param longName The long name of the parameter, must be valid
118 * @param shortName The short name of the parameter, can be empty
119 * @param description The optional description of the parameter, can be empty
120 * @param defaultValue The optional default value, invalid otherwise
121 */
122 Parameter(const std::string& longName, const std::string& shortName, const std::string& description, const Value& defaultValue);
123
124 /**
125 * Returns the long name of this parameter
126 * @return The parameter's long name
127 */
128 inline const std::string& longName() const;
129
130 /**
131 * Returns the short name of this parameter
132 * @return The parameter's short name, can be empty
133 */
134 inline const std::string& shortName() const;
135
136 /**
137 * Returns the description of this parameter.
138 * @return The parameter's description
139 */
140 inline const std::string& description() const;
141
142 /**
143 * Returns the default value of this parameter.
144 * @return The parameter's default name, can be invalid
145 */
146 inline const Value& defaultValue() const;
147
148 protected:
149
150 /// The long name of the parameter.
151 std::string longName_;
152
153 /// The short name of the parameter, can be empty.
154 std::string shortName_;
155
156 /// The description of this parameter, can be empty.
157 std::string description_;
158
159 /// The default value of this parameter, can be invalid.
161 };
162
163 /**
164 * Definition of a map mapping long parameter names to parameter objects.
165 */
166 using ParameterMap = std::map<std::string, Parameter>;
167
168 /**
169 * Definition of a map mapping short parameter names to long parameter names.
170 */
171 using ShortToLongMap = std::unordered_map<std::string, std::string>;
172
173 /**
174 * Definition of a map mapping long parameter names to values.
175 */
176 using ValueMap = std::unordered_map<std::string, Value>;
177
178 public:
179
180 /**
181 * Creates a new object.
182 */
184
185 /**
186 * Creates a new object with a description of the application
187 * @param applicationDescription A description text for the application using these command arguments.
188 */
189 CommandArguments(const std::string& applicationDescription);
190
191 /**
192 * Registers a new named parameter which can be parsed as command argument.
193 * @param longName The long name of the parameter, must be valid, must start with a alphabetic character
194 * @param shortName Optional short name of the parameter, can be empty, must start with a alphabetic character if defined
195 * @param description Optional description of the parameter, can be empty
196 * @param defaultValue Optional default value of the parameter, can be invalid
197 * @return True, if the named parameter did not exist before
198 */
199 bool registerParameter(const std::string& longName, const std::string& shortName = std::string(), const std::string& description = std::string(), const Value& defaultValue = Value());
200
201 /**
202 * Registers nameless parameters for the summary.
203 * @param description The description for nameless parameters, must not be empty
204 * @return True, if succeeded
205 */
206 bool registerNamelessParameters(std::string&& description);
207
208 /**
209 * Parses A given command line.
210 * The entire command line may contain several commands separated by space characters.<br>
211 * Command elements which contain space characters must be surrounded by quotation marks.<br>
212 * However, all surrounding quotation marks will be removed during the parsing process.<br>
213 * The first argument should not be the filename (and path) of the executable.
214 * @param commandLine The command line to be parsed, must be valid
215 * @return True, if succeeded
216 * @tparam TChar The character type, `char` or `wchar_t`
217 */
218 template <typename TChar>
219 inline bool parse(const TChar* commandLine);
220
221 /**
222 * Parses the several command arguments.
223 * @param arguments The arguments to be parsed, an be nullptr if `size == 0`
224 * @param size The number of arguments, with size [0, infinity)
225 * @param skipFirstArgument True, to skip the first argument (as this is commonly the path of the executable); False, to parse the first parameter as well
226 * @return True, if succeeded
227 * @tparam TChar The character type, `char` or `wchar_t`
228 */
229 template <typename TChar>
230 bool parse(const TChar* const* arguments, const size_t size, const bool skipFirstArgument = true);
231
232 /**
233 * Parses the command arguments already separated into individual arguments.
234 * @param separatedArguments The individual arguments to be parsed
235 * @return True, if succeeded
236 */
237 bool parse(const ArgumentsT<char>& separatedArguments);
238
239 /**
240 * Parses the command arguments already separated into individual arguments.
241 * @param separatedArguments The individual arguments to be parsed
242 * @return True, if succeeded
243 */
244 bool parse(const ArgumentsT<wchar_t>& separatedArguments);
245
246 /**
247 * Returns the value of a specific parameter which has been parsed.
248 * Below an example of how to use this function:
249 * @code
250 * CommandArguments commandArguments;
251 * commandArguments.registerParameter("input", "i", "The input file");
252 *
253 * const Value inputFile = commandArguments.value<std::string>("input");
254 * if (inputFile.isString())
255 * {
256 * const std::string inputFileString = inputFile.stringValue();
257 * // ...
258 * }
259 * @endcode
260 * @param longName The long name of the parameter for which the value will be returned, must be valid
261 * @param 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
262 * @param 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
263 * @return The value of the parameter, an invalid value if the value has not been parsed
264 */
265 Value value(const std::string& longName, const bool allowDefaultValue = true, const size_t namelessValueIndex = size_t(-1)) const;
266
267 /**
268 * Returns the value of a specific parameter which has been parsed.
269 * Below an example of how to use this function:
270 * @code
271 * CommandArguments commandArguments;
272 * commandArguments.registerParameter("input", "i", "The input file");
273 * commandArguments.registerParameter("factor", "f", "The factor parameter");
274 *
275 * const std::string inputFile = commandArguments.value<std::string>("input", std::string(), false);
276 * const double factor = commandArguments.value<double>("factor", -1.0, false);
277 * @endcode
278 * @param longName The long name of the parameter for which the value will be returned, must be valid
279 * @param invalidValue The resulting value in case the parameter was not parsed
280 * @param 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
281 * @param 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
282 * @return The value of the parameter, the invalid value if the value has not been parsed
283 */
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;
286
287 /**
288 * Checks whether a specific parameter value has been parsed, or whether a default value is defined.
289 * @param longName The long name of the parameter value to check, must be valid
290 * @param value Optional resulting value of the parameter
291 * @param allowDefaultValue True, to allow a default value to count as parsed value; False, to check for actually parsed values only
292 * @param 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
293 * @return True, if succeeded
294 */
295 bool hasValue(const std::string& longName, Value* value = nullptr, const bool allowDefaultValue = true, const size_t namelessValueIndex = size_t(-1)) const;
296
297 /**
298 * Checks whether a specific parameter value has been parsed with specific data type, or whether a default value is defined.
299 * @param longName The long name of the parameter value to check, must be valid
300 * @param value The resulting value of the parameter
301 * @param allowDefaultValue True, to allow a default value to count as parsed value; False, to check for actually parsed values only
302 * @param 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
303 * @return True, if succeeded
304 * @tparam 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'
305 */
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;
308
309 /**
310 * Returns all nameless values which have been parsed.
311 * @return The nameless values, in order as they have been parsed, if any
312 */
313 inline const Strings& namelessValues() const;
314
315 /**
316 * Creates a summary of all possible command arguments.
317 * @return The string containing the summary
318 */
319 std::string makeSummary();
320
321 /**
322 * Parses the command line and returns the individual command elements.
323 * The entire command line may contain several commands separated by space characters.<br>
324 * Command elements which contain space characters must be surrounded by quotation marks.<br>
325 * However, all surrounding quotation marks will be removed during the parsing process.<br>
326 * The first argument should not be the filename (and path) of the executable.
327 * @param commandLine Command line to be parsed
328 * @return The separated command arguments
329 * @tparam TChar The character type, `char` or `wchar_t`
330 */
331 template <typename TChar>
332 static ArgumentsT<TChar> separateArguments(const TChar* commandLine);
333
334 protected:
335
336 /**
337 * Returns whether a given string is the start of a long parameter name (whether it starts with "--").
338 * The string must be zero terminated.
339 * @param parameter The string to be checked, must be valid
340 * @return True, if so
341 * @tparam TChar The character type, `char` or `wchar_t`
342 */
343 template <typename TChar>
344 bool isLongParameter(const TChar* parameter);
345
346 /**
347 * Returns whether a given string is the start of a short parameter name (whether it starts with "--").
348 * The string must be zero terminated.
349 * @param parameter The string to be checked, must be valid
350 * @return True, if so
351 * @tparam TChar The character type, `char` or `wchar_t`
352 */
353 template <typename TChar>
354 bool isShortParameter(const TChar* parameter);
355
356 /**
357 * Returns a dash character.
358 * @return Dash character
359 * @tparam TChar The character type, `char` or `wchar_t`
360 */
361 template <typename TChar>
362 static inline TChar dashCharacter();
363
364 /**
365 * Returns a quote character.
366 * @return Quote character
367 * @tparam TChar The character type, `char` or `wchar_t`
368 */
369 template <typename TChar>
370 static inline TChar quoteCharacter();
371
372 /**
373 * Returns a space character.
374 * @return Space character
375 * @tparam TChar The character type, `char` or `wchar_t`
376 */
377 template <typename TChar>
378 static inline TChar spaceCharacter();
379
380 /**
381 * Returns a backslash character.
382 * @return Backslash character
383 * @tparam TChar The character type, `char` or `wchar_t`
384 */
385 template <typename TChar>
386 static inline TChar backslashCharacter();
387
388 protected:
389
390 /// The description text for the application using these command arguments.
392
393 /// The map mapping short parameter names to long parameter names.
395
396 /// The map mapping long parameter names to parameter objects.
398
399 /// The map mapping long parameter names to values.
401
402 /// The vector of values without name.
404
405 /// Optional description for nameless parameters.
407};
408
409inline const std::string& CommandArguments::Parameter::longName() const
410{
411 return longName_;
412}
413
414inline const std::string& CommandArguments::Parameter::shortName() const
415{
416 return shortName_;
417}
418
419inline const std::string& CommandArguments::Parameter::description() const
420{
421 return description_;
422}
423
425{
426 return defaultValue_;
427}
428
429template <typename TChar>
430inline bool CommandArguments::parse(const TChar* commandLine)
431{
432 if (commandLine == nullptr)
433 {
434 return false;
435 }
436
437 return parse(separateArguments(commandLine));
438}
439
440template <>
441inline bool CommandArguments::parse<wchar_t>(const wchar_t* commandLine)
442{
443 if (commandLine == nullptr)
444 {
445 return false;
446 }
447
448 const ArgumentsT<wchar_t> wSeparatedArguments = separateArguments(commandLine);
449
450 ArgumentsT<char> separatedArguments;
451 separatedArguments.reserve(wSeparatedArguments.size());
452
453 for (const ArgumentT<wchar_t>& argument : wSeparatedArguments)
454 {
455 separatedArguments.emplace_back(String::toAString(argument));
456 }
457
458 return parse(separatedArguments);
459}
460
461template <typename TChar>
462bool CommandArguments::parse(const TChar* const* arguments, const size_t size, const bool skipFirstArgument)
463{
464 if (size == 0)
465 {
466 return true;
467 }
468
469 if (arguments == nullptr)
470 {
471 ocean_assert(false && "Invalid arguments!");
472 return false;
473 }
474
475 ArgumentsT<char> separatedArguments;
476 separatedArguments.reserve(size);
477
478 const size_t nStart = skipFirstArgument ? 1 : 0;
479
480 for (size_t n = nStart; n < size; ++n)
481 {
482 separatedArguments.emplace_back(String::toAString(arguments[n]));
483 }
484
485 return parse(separatedArguments);
486}
487
488template <typename T>
489T CommandArguments::value(const std::string& longName, const T& invalidValue, const bool allowDefaultValue, const size_t namelessValueIndex) const
490{
491 T validValue;
492 if (hasValue<T>(longName, validValue, allowDefaultValue, namelessValueIndex))
493 {
494 return validValue;
495 }
496
497 return invalidValue;
498}
499
500template <>
501inline bool CommandArguments::hasValue(const std::string& longName, bool& boolValue, const bool allowDefaultValue, const size_t namelessValueIndex) const
502{
503 Value value;
504 if (!hasValue(longName, &value, allowDefaultValue, namelessValueIndex))
505 {
506 return false;
507 }
508
509 if (!value.isBool())
510 {
511 return false;
512 }
513
514 boolValue = value.boolValue();
515
516 return true;
517}
518
519template <>
520inline bool CommandArguments::hasValue(const std::string& longName, int32_t& intValue, const bool allowDefaultValue, const size_t namelessValueIndex) const
521{
522 Value value;
523 if (!hasValue(longName, &value, allowDefaultValue, namelessValueIndex))
524 {
525 return false;
526 }
527
528 int64_t signedValue64 = 0;
529
530 if (value.isInt())
531 {
532 signedValue64 = value.intValue();
533 }
534 else if (value.isInt64())
535 {
536 signedValue64 = value.int64Value();
537 }
538 else
539 {
540 return false;
541 }
542
543 if (signedValue64 < int64_t(std::numeric_limits<int32_t>::lowest()) || signedValue64 > int64_t(std::numeric_limits<int32_t>::max()))
544 {
545 return false;
546 }
547
548 intValue = int32_t(signedValue64);
549
550 return true;
551}
552
553template <>
554inline bool CommandArguments::hasValue(const std::string& longName, uint32_t& intValue, const bool allowDefaultValue, const size_t namelessValueIndex) const
555{
556 Value value;
557 if (!hasValue(longName, &value, allowDefaultValue, namelessValueIndex))
558 {
559 return false;
560 }
561
562 int64_t signedValue64 = 0;
563
564 if (value.isInt())
565 {
566 signedValue64 = value.intValue();
567 }
568 else if (value.isInt64())
569 {
570 signedValue64 = value.int64Value();
571 }
572 else
573 {
574 return false;
575 }
576
577 if (signedValue64 < 0 || signedValue64 > int64_t(std::numeric_limits<uint32_t>::max()))
578 {
579 return false;
580 }
581
582 intValue = uint32_t(signedValue64);
583
584 return true;
585}
586
587template <>
588inline bool CommandArguments::hasValue(const std::string& longName, int64_t& intValue, const bool allowDefaultValue, const size_t namelessValueIndex) const
589{
590 Value value;
591 if (!hasValue(longName, &value, allowDefaultValue, namelessValueIndex))
592 {
593 return false;
594 }
595
596 if (value.isInt())
597 {
598 intValue = value.intValue();
599 }
600 else if (value.isInt64())
601 {
602 intValue = value.int64Value();
603 }
604 else
605 {
606 return false;
607 }
608
609 return true;
610}
611
612template <>
613inline bool CommandArguments::hasValue(const std::string& longName, uint64_t& intValue, const bool allowDefaultValue, const size_t namelessValueIndex) const
614{
615 Value value;
616 if (!hasValue(longName, &value, allowDefaultValue, namelessValueIndex))
617 {
618 return false;
619 }
620
621 int64_t signedValue64 = 0;
622
623 if (value.isInt())
624 {
625 signedValue64 = value.intValue();
626 }
627 else if (value.isInt64())
628 {
629 signedValue64 = value.int64Value();
630 }
631 else
632 {
633 return false;
634 }
635
636 if (signedValue64 < 0)
637 {
638 return false;
639 }
640
641 intValue = uint64_t(signedValue64);
642
643 return true;
644}
645
646template <>
647inline bool CommandArguments::hasValue(const std::string& longName, double& doubleValue, const bool allowDefaultValue, const size_t namelessValueIndex) const
648{
649 Value value;
650 if (!hasValue(longName, &value, allowDefaultValue, namelessValueIndex))
651 {
652 return false;
653 }
654
655 if (!value.isFloat64(true /*allowIntAndFloat*/))
656 {
657 return false;
658 }
659
660 doubleValue = value.float64Value(true /*allowIntAndFloat*/);
661
662 return true;
663}
664
665template <>
666inline bool CommandArguments::hasValue(const std::string& longName, std::string& stringValue, const bool allowDefaultValue, const size_t namelessValueIndex) const
667{
668 Value value;
669 if (!hasValue(longName, &value, allowDefaultValue, namelessValueIndex))
670 {
671 return false;
672 }
673
674 if (!value.isString())
675 {
676 return false;
677 }
678
679 stringValue = value.stringValue();
680
681 return true;
682}
683
684template <typename T>
685inline bool CommandArguments::hasValue(const std::string& /*longName*/, T& /*stringValue*/, const bool /*allowDefaultValue*/, const size_t /*namelessValueIndex*/) const
686{
687 ocean_assert(false && "Invalid data type!");
688
689 return false;
690}
691
693{
694 return namelessValues_;
695}
696
697template <typename TChar>
699{
700 ArgumentsT<TChar> applicationArguments;
701
702 if (!commandLine)
703 {
704 return applicationArguments;
705 }
706
707 ArgumentT<TChar> line(commandLine);
708
709 while (!line.empty())
710 {
711 const typename ArgumentT<TChar>::size_type posQuote = line.find(quoteCharacter<TChar>());
712 const typename ArgumentT<TChar>::size_type posSpace = line.find(spaceCharacter<TChar>());
713
714 if (posQuote == 0)
715 {
716 // parse string command
717
718 typename ArgumentT<TChar>::size_type posEndQuote = line.find(quoteCharacter<TChar>(), 1);
719
720 while (posEndQuote != ArgumentT<TChar>::npos)
721 {
722 if (line[posEndQuote - 1] == backslashCharacter<TChar>())
723 {
724 posEndQuote = line.find(quoteCharacter<TChar>(), posEndQuote + 1);
725 }
726 else
727 {
728 applicationArguments.push_back(line.substr(1, posEndQuote - 1));
729 line = line.substr(posEndQuote + 1);
730 break;
731 }
732 }
733
734 if (posEndQuote == ArgumentT<TChar>::npos)
735 {
736 Log::error() << "Invalid string command: " << String::toAString(line) << "The end quote is missing!";
737 return applicationArguments;
738 }
739
740 continue;
741 }
742
743 // parse string-less command
744 if (posSpace == ArgumentT<TChar>::npos)
745 {
746 ocean_assert(line.length() != 0);
747
748 applicationArguments.push_back(line);
749 break;
750 }
751
752 if (posSpace == 0)
753 {
754 line = line.substr(1);
755 }
756 else
757 {
758 applicationArguments.push_back(line.substr(0, posSpace));
759 line = line.substr(posSpace + 1);
760 }
761 }
762
763 return applicationArguments;
764}
765
766template <typename TChar>
767bool CommandArguments::isLongParameter(const TChar* parameter)
768{
769 ocean_assert(parameter != nullptr);
770
771 // we expect two '-' followed by at least one alphabetic character
772
773 return parameter[0] == dashCharacter<TChar>() && parameter[1] == dashCharacter<TChar>() && parameter[2] != dashCharacter<TChar>() && std::isalpha(int(parameter[2])) != 0;
774}
775
776template <typename TChar>
777bool CommandArguments::isShortParameter(const TChar* parameter)
778{
779 ocean_assert(parameter != nullptr);
780
781 // we expect one '-' followed by at least one alphabetic character
782
783 return parameter[0] == dashCharacter<TChar>() && std::isalpha(int(parameter[1])) != 0;
784}
785
786template <>
787inline char CommandArguments::dashCharacter<char>()
788{
789 return '-';
790}
791
792template <>
793inline wchar_t CommandArguments::dashCharacter<wchar_t>()
794{
795 return L'-';
796}
797
798template <>
799inline char CommandArguments::quoteCharacter<char>()
800{
801 return '"';
802}
803
804template <>
805inline wchar_t CommandArguments::quoteCharacter<wchar_t>()
806{
807 return L'"';
808}
809
810template <>
811inline char CommandArguments::spaceCharacter<char>()
812{
813 return ' ';
814}
815
816template <>
817inline wchar_t CommandArguments::spaceCharacter<wchar_t>()
818{
819 return L' ';
820}
821
822template <>
823inline char CommandArguments::backslashCharacter<char>()
824{
825 return '\\';
826}
827
828template <>
829inline wchar_t CommandArguments::backslashCharacter<wchar_t>()
830{
831 return L'\\';
832}
833
834}
835
836#endif // META_OCEAN_BASE_COMMAND_ARGUMENTS_H
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
const TChar *const * rawArguments() const
Returns the command arguments of the application.
Manager()
Protected default constructor.
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: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.
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
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.
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
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:389
bool isFloat64(const bool allowIntAndFloat=false) const
Returns whether this object holds a 64 bit float value as internal data.
Definition Value.h:377
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.
int64_t int64Value() const
Returns the internal value as 64 bit integer.
bool isInt() const
Returns whether this object holds an integer value as internal data.
Definition Value.h:362
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:357
bool isInt64() const
Returns whether this object holds a 64 bit integer value as internal data.
Definition Value.h:367
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