C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
argumentparser.h
Go to the documentation of this file.
1 #ifndef APPLICATION_UTILITIES_ARGUMENTPARSER_H
2 #define APPLICATION_UTILITIES_ARGUMENTPARSER_H
3 
4 #include "../conversion/stringconversion.h"
5 #include "../global.h"
6 #include "../misc/traits.h"
7 
8 #include <functional>
9 #include <initializer_list>
10 #include <limits>
11 #include <vector>
12 #ifdef DEBUG_BUILD
13 #include <cassert>
14 #endif
15 
17 
19 
20 CPP_UTILITIES_EXPORT extern const char *applicationName;
21 CPP_UTILITIES_EXPORT extern const char *applicationAuthor;
22 CPP_UTILITIES_EXPORT extern const char *applicationVersion;
23 CPP_UTILITIES_EXPORT extern const char *applicationUrl;
24 CPP_UTILITIES_EXPORT extern std::initializer_list<const char *> dependencyVersions;
25 CPP_UTILITIES_EXPORT extern std::vector<const char *> dependencyVersions2;
26 
33 #ifndef APP_STATICALLY_LINKED
34 #define SET_DEPENDENCY_INFO ::ApplicationUtilities::dependencyVersions2 = DEPENCENCY_VERSIONS
35 #else
36 #define SET_DEPENDENCY_INFO ::ApplicationUtilities::dependencyVersions2 = STATIC_DEPENCENCY_VERSIONS
37 #endif
38 
44 #define SET_APPLICATION_INFO \
45  ::ApplicationUtilities::applicationName = APP_NAME; \
46  ::ApplicationUtilities::applicationAuthor = APP_AUTHOR; \
47  ::ApplicationUtilities::applicationVersion = APP_VERSION; \
48  ::ApplicationUtilities::applicationUrl = APP_URL; \
49  SET_DEPENDENCY_INFO
50 
51 CPP_UTILITIES_EXPORT extern void (*exitFunction)(int);
52 
53 class Argument;
54 class ArgumentParser;
55 class ArgumentReader;
56 
57 using ArgumentInitializerList = std::initializer_list<Argument *>;
58 using ArgumentVector = std::vector<Argument *>;
59 using ArgumentPredicate = std::function<bool(Argument *)>;
60 
66  Ignore,
67  Warn,
68  Fail
69 };
70 
78  ReadArguments = 0x0,
79  CheckConstraints = 0x1,
80  InvokeCallbacks = 0x2,
82  = 0x4,
83 };
84 
87 {
88  return static_cast<ParseArgumentBehavior>(static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs));
89 }
90 
92 {
93  return static_cast<bool>(static_cast<unsigned char>(lhs) & static_cast<unsigned char>(rhs));
94 }
96 
106 enum class ValueCompletionBehavior : unsigned char {
107  None = 0,
108  PreDefinedValues = 2,
109  Files = 4,
110  Directories = 8,
112  AppendEquationSign = 32,
113  InvokeCallback = 64,
114 };
115 
118 {
119  return static_cast<ValueCompletionBehavior>(static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs));
120 }
121 
123 {
124  return static_cast<bool>(static_cast<unsigned char>(lhs) & static_cast<unsigned char>(rhs));
125 }
127 
128 // TODO v5: Make function private
129 Argument CPP_UTILITIES_EXPORT *firstPresentUncombinableArg(const ArgumentVector &args, const Argument *except);
130 
138 namespace ValueConversion {
139 template <typename TargetType, Traits::EnableIf<std::is_same<TargetType, std::string>> * = nullptr> TargetType convert(const char *value)
140 {
141  return std::string(value);
142 }
143 
144 template <typename TargetType, Traits::EnableIf<std::is_arithmetic<TargetType>> * = nullptr> TargetType convert(const char *value)
145 {
146  return ConversionUtilities::stringToNumber<TargetType>(value);
147 }
148 
150 namespace Helper {
151 struct CPP_UTILITIES_EXPORT ArgumentValueConversionError {
152  const std::string errorMessage;
153  const char *const valueToConvert;
154  const char *const targetTypeName;
155 
156  [[noreturn]] void throwFailure(const std::vector<Argument *> &argumentPath) const;
157 };
158 
159 template <std::size_t N, typename FirstTargetType, typename... RemainingTargetTypes> struct ArgumentValueConverter {
160  static std::tuple<FirstTargetType, RemainingTargetTypes...> convertValues(std::vector<const char *>::const_iterator firstValue)
161  {
162  return std::tuple_cat(ArgumentValueConverter<1, FirstTargetType>::convertValues(firstValue),
163  ArgumentValueConverter<N - 1, RemainingTargetTypes...>::convertValues(firstValue + 1));
164  }
165 };
166 
167 template <typename FirstTargetType, typename... RemainingTargetTypes> struct ArgumentValueConverter<1, FirstTargetType, RemainingTargetTypes...> {
168  static std::tuple<FirstTargetType> convertValues(std::vector<const char *>::const_iterator firstValue)
169  {
170  // FIXME: maybe use std::expected here when available
171  try {
172  return std::make_tuple<FirstTargetType>(ValueConversion::convert<FirstTargetType>(*firstValue));
173  } catch (const ConversionUtilities::ConversionException &exception) {
174  throw ArgumentValueConversionError{ exception.what(), *firstValue, typeid(FirstTargetType).name() };
175  }
176  }
177 };
178 } // namespace Helper
180 
181 } // namespace ValueConversion
182 
187  ArgumentOccurrence(std::size_t index);
188  ArgumentOccurrence(std::size_t index, const std::vector<Argument *> parentPath, Argument *parent);
189 
193  std::size_t index;
194 
198  std::vector<const char *> values;
199 
204  std::vector<Argument *> path;
205 
206  template <typename... RemainingTargetTypes> std::tuple<RemainingTargetTypes...> convertValues() const;
207 
208 private:
209  [[noreturn]] void throwNumberOfValuesNotSufficient(unsigned long valuesToConvert) const;
210 };
211 
217 template <typename... RemainingTargetTypes> std::tuple<RemainingTargetTypes...> ArgumentOccurrence::convertValues() const
218 {
219  constexpr auto valuesToConvert = sizeof...(RemainingTargetTypes);
220  if (values.size() < valuesToConvert) {
221  throwNumberOfValuesNotSufficient(valuesToConvert);
222  }
223  try {
224  return ValueConversion::Helper::ArgumentValueConverter<valuesToConvert, RemainingTargetTypes...>::convertValues(values.cbegin());
225  } catch (const ValueConversion::Helper::ArgumentValueConversionError &error) {
226  error.throwFailure(path);
227  }
228 }
229 
233 inline ArgumentOccurrence::ArgumentOccurrence(std::size_t index)
234  : index(index)
235 {
236 }
237 
246 inline ArgumentOccurrence::ArgumentOccurrence(std::size_t index, const std::vector<Argument *> parentPath, Argument *parent)
247  : index(index)
248  , path(parentPath)
249 {
250  if (parent) {
251  path.push_back(parent);
252  }
253 }
254 
256  friend ArgumentParser;
257  friend ArgumentReader;
258  friend ArgumentParserTests;
259 
260 public:
261  typedef std::function<void(const ArgumentOccurrence &)> CallbackFunction;
262 
263  Argument(const char *name, char abbreviation = '\0', const char *description = nullptr, const char *example = nullptr);
264  ~Argument();
265 
266  // declare getter/setter/properties/operations for argument definition:
267  // - those properties must be set *before* parsing
268  // - they control the behaviour of the parser, eg.
269  // - the name/abbreviation to look for
270  // - constraints to be checked
271  // - callbacks to be invoked
272  // TODO v5: It would make sense to move these to a separate class (eg. ArgumentDefinition) to prevent this one from
273  // becoming to big.
274  const char *name() const;
275  void setName(const char *name);
276  char abbreviation() const;
277  void setAbbreviation(char abbreviation);
278  const char *environmentVariable() const;
279  void setEnvironmentVariable(const char *environmentVariable);
280  const char *description() const;
281  void setDescription(const char *description);
282  const char *example() const;
283  void setExample(const char *example);
284  std::size_t requiredValueCount() const;
285  void setRequiredValueCount(std::size_t requiredValueCount);
286  const std::vector<const char *> &valueNames() const;
287  void setValueNames(std::initializer_list<const char *> valueNames);
288  void appendValueName(const char *valueName);
289  void setConstraints(std::size_t minOccurrences, std::size_t maxOccurrences);
290  const std::vector<Argument *> &path(std::size_t occurrence = 0) const;
291  bool isRequired() const;
292  void setRequired(bool required);
293  bool isCombinable() const;
294  void setCombinable(bool value);
295  bool isImplicit() const;
296  void setImplicit(bool value);
297  bool denotesOperation() const;
298  void setDenotesOperation(bool denotesOperation);
299  const CallbackFunction &callback() const;
300  void setCallback(CallbackFunction callback);
301  const ArgumentVector &subArguments() const;
302  void setSubArguments(const ArgumentInitializerList &subArguments);
303  void addSubArgument(Argument *arg);
304  bool hasSubArguments() const;
305  const ArgumentVector parents() const;
306  void printInfo(std::ostream &os, unsigned char indentation = 0) const;
307 
308  // declare getter/setter/properties for bash completion: those properties must be set *before parsing
309  ValueCompletionBehavior valueCompletionBehaviour() const;
310  void setValueCompletionBehavior(ValueCompletionBehavior valueCompletionBehaviour);
311  const char *preDefinedCompletionValues() const;
312  void setPreDefinedCompletionValues(const char *preDefinedCompletionValues);
313 
314  // declare getter/read-only properties for parsing results: those properties will be populated when parsing
315  const std::vector<const char *> &values(std::size_t occurrence = 0) const;
316  template <typename... TargetType> std::tuple<TargetType...> valuesAs(std::size_t occurrence = 0) const;
317  template <typename... TargetType> std::vector<std::tuple<TargetType...>> allValuesAs() const;
318 
319  const char *firstValue() const;
320  bool allRequiredValuesPresent(std::size_t occurrence = 0) const;
321  bool isPresent() const;
322  std::size_t occurrences() const;
323  std::size_t index(std::size_t occurrence) const;
324  std::size_t minOccurrences() const;
325  std::size_t maxOccurrences() const;
326  bool isMainArgument() const;
327  bool isParentPresent() const;
328  Argument *conflictsWithArgument() const;
329  Argument *wouldConflictWithArgument() const;
330  Argument *specifiedOperation() const;
331  const std::vector<ArgumentOccurrence> &occurrenceInfo() const;
332  std::vector<ArgumentOccurrence> &occurrenceInfo();
333  void reset();
334  void resetRecursively();
335 
340  static constexpr std::size_t varValueCount = std::numeric_limits<std::size_t>::max();
341 
342 private:
343  // declare internal getter/setter/properties/operations for argument definition
344  bool matchesDenotation(const char *denotation, size_t denotationLength) const;
345 
346  const char *m_name;
347  char m_abbreviation;
348  const char *m_environmentVar;
349  const char *m_description;
350  const char *m_example;
351  std::size_t m_minOccurrences;
352  std::size_t m_maxOccurrences;
353  bool m_combinable;
354  bool m_denotesOperation;
355  std::size_t m_requiredValueCount;
356  std::vector<const char *> m_valueNames;
357  bool m_implicit;
358  std::vector<ArgumentOccurrence> m_occurrences;
359  ArgumentVector m_subArgs;
360  CallbackFunction m_callbackFunction;
361  ArgumentVector m_parents;
362  bool m_isMainArg;
363  ValueCompletionBehavior m_valueCompletionBehavior;
364  const char *m_preDefinedCompletionValues;
365 };
366 
372 template <typename... TargetType> std::tuple<TargetType...> Argument::valuesAs(std::size_t occurrence) const
373 {
374  return m_occurrences[occurrence].convertValues<TargetType...>();
375 }
376 
382 template <typename... TargetType> std::vector<std::tuple<TargetType...>> Argument::allValuesAs() const
383 {
384  std::vector<std::tuple<TargetType...>> res;
385  res.reserve(m_occurrences.size());
386  for (const auto &occurrence : m_occurrences) {
387  res.emplace_back(occurrence.convertValues<TargetType...>());
388  }
389  return res;
390 }
391 
393 
395  friend ArgumentParserTests;
396  friend ArgumentReader;
397 
398 public:
399  ArgumentParser();
400 
401  // declare getter/setter for argument definitions
402  const ArgumentVector &mainArguments() const;
403  void setMainArguments(const ArgumentInitializerList &mainArguments);
404  void addMainArgument(Argument *argument);
405 
406  // declare operations which will consider previously assigned argument definitions and maybe modify parsing results
407  void printHelp(std::ostream &os) const;
408  void parseArgs(int argc, const char *const *argv);
409  void parseArgsOrExit(int argc, const char *const *argv);
410  void parseArgsExt(int argc, const char *const *argv,
411  ParseArgumentBehavior behavior
413  void readArgs(int argc, const char *const *argv);
414  void resetArgs();
415  void checkConstraints();
416  void invokeCallbacks();
417 
418  // declare getter for parsing results
419  unsigned int actualArgumentCount() const;
420  const char *executable() const;
421  UnknownArgumentBehavior unknownArgumentBehavior() const;
422  void setUnknownArgumentBehavior(UnknownArgumentBehavior behavior);
423  Argument *defaultArgument() const;
424  void setDefaultArgument(Argument *argument);
425  Argument *specifiedOperation() const;
426  bool isUncombinableMainArgPresent() const;
427 
428 private:
429  // declare internal operations
430  IF_DEBUG_BUILD(void verifyArgs(const ArgumentVector &args);)
431  ArgumentCompletionInfo determineCompletionInfo(
432  int argc, const char *const *argv, unsigned int currentWordIndex, const ArgumentReader &reader) const;
433  std::string findSuggestions(int argc, const char *const *argv, unsigned int cursorPos, const ArgumentReader &reader) const;
434  void printBashCompletion(int argc, const char *const *argv, unsigned int cursorPos, const ArgumentReader &reader) const;
435  void checkConstraints(const ArgumentVector &args);
436  static void invokeCallbacks(const ArgumentVector &args);
437 
438  ArgumentVector m_mainArgs;
439  unsigned int m_actualArgc;
440  const char *m_executable;
441  UnknownArgumentBehavior m_unknownArgBehavior;
442  Argument *m_defaultArg;
443 };
444 
450 inline const char *Argument::name() const
451 {
452  return m_name;
453 }
454 
462 inline void Argument::setName(const char *name)
463 {
464 #ifdef DEBUG_BUILD
465  if (name && *name) {
466  assert(*name != '-');
467  for (const char *c = name; *c; ++c) {
468  assert(*c != ' ' && *c != '=' && *c != '\'' && *c != '\"' && *c != '\n' && *c != '\r');
469  }
470  }
471 #endif
472  m_name = name;
473 }
474 
480 inline char Argument::abbreviation() const
481 {
482  return m_abbreviation;
483 }
484 
492 inline void Argument::setAbbreviation(char abbreviation)
493 {
494  IF_DEBUG_BUILD(assert(abbreviation != ' ' && abbreviation != '=' && abbreviation != '-' && abbreviation != '\'' && abbreviation != '"'
495  && abbreviation != '\n' && abbreviation != '\r'));
496  m_abbreviation = abbreviation;
497 }
498 
503 inline const char *Argument::environmentVariable() const
504 {
505  return m_environmentVar;
506 }
507 
512 inline void Argument::setEnvironmentVariable(const char *environmentVariable)
513 {
514  m_environmentVar = environmentVariable;
515 }
516 
522 inline const char *Argument::description() const
523 {
524  return m_description;
525 }
526 
532 inline void Argument::setDescription(const char *description)
533 {
534  m_description = description;
535 }
536 
542 inline const char *Argument::example() const
543 {
544  return m_example;
545 }
546 
552 inline void Argument::setExample(const char *example)
553 {
554  m_example = example;
555 }
556 
563 inline const std::vector<const char *> &Argument::values(std::size_t occurrence) const
564 {
565  return m_occurrences[occurrence].values;
566 }
567 
581 inline std::size_t Argument::requiredValueCount() const
582 {
583  return m_requiredValueCount;
584 }
585 
598 inline void Argument::setRequiredValueCount(std::size_t requiredValueCount)
599 {
600  m_requiredValueCount = requiredValueCount;
601 }
602 
611 inline const std::vector<const char *> &Argument::valueNames() const
612 {
613  return m_valueNames;
614 }
615 
629 inline void Argument::setValueNames(std::initializer_list<const char *> valueNames)
630 {
631  m_valueNames.assign(valueNames);
632 }
633 
640 inline void Argument::appendValueName(const char *valueName)
641 {
642  m_valueNames.emplace_back(valueName);
643 }
644 
648 inline bool Argument::allRequiredValuesPresent(std::size_t occurrence) const
649 {
650  return m_requiredValueCount == Argument::varValueCount
651  || (m_occurrences[occurrence].values.size() >= static_cast<std::size_t>(m_requiredValueCount));
652 }
653 
658 inline bool Argument::isImplicit() const
659 {
660  return m_implicit;
661 }
662 
667 inline void Argument::setImplicit(bool implicit)
668 {
669  m_implicit = implicit;
670 }
671 
675 inline bool Argument::isPresent() const
676 {
677  return !m_occurrences.empty();
678 }
679 
683 inline std::size_t Argument::occurrences() const
684 {
685  return m_occurrences.size();
686 }
687 
691 inline std::size_t Argument::index(std::size_t occurrence) const
692 {
693  return m_occurrences[occurrence].index;
694 }
695 
701 inline std::size_t Argument::minOccurrences() const
702 {
703  return m_minOccurrences;
704 }
705 
711 inline std::size_t Argument::maxOccurrences() const
712 {
713  return m_maxOccurrences;
714 }
715 
721 inline void Argument::setConstraints(std::size_t minOccurrences, std::size_t maxOccurrences)
722 {
723  m_minOccurrences = minOccurrences;
724  m_maxOccurrences = maxOccurrences;
725 }
726 
730 inline const std::vector<Argument *> &Argument::path(std::size_t occurrence) const
731 {
732  return m_occurrences[occurrence].path;
733 }
734 
744 inline bool Argument::isRequired() const
745 {
746  return m_minOccurrences;
747 }
748 
756 inline void Argument::setRequired(bool required)
757 {
758  if (required) {
759  if (!m_minOccurrences) {
760  m_minOccurrences = 1;
761  }
762  } else {
763  m_minOccurrences = 0;
764  }
765 }
766 
775 inline bool Argument::isCombinable() const
776 {
777  return m_combinable;
778 }
779 
788 inline void Argument::setCombinable(bool value)
789 {
790  m_combinable = value;
791 }
792 
803 inline bool Argument::denotesOperation() const
804 {
805  return m_denotesOperation;
806 }
807 
812 inline void Argument::setDenotesOperation(bool denotesOperation)
813 {
814  m_denotesOperation = denotesOperation;
815 }
816 
822 {
823  return m_callbackFunction;
824 }
825 
833 {
834  m_callbackFunction = callback;
835 }
836 
844 {
845  return m_subArgs;
846 }
847 
854 inline bool Argument::hasSubArguments() const
855 {
856  return !m_subArgs.empty();
857 }
858 
871 inline const ArgumentVector Argument::parents() const
872 {
873  return m_parents;
874 }
875 
882 inline bool Argument::isMainArgument() const
883 {
884  return m_isMainArg;
885 }
886 
894 {
895  return m_valueCompletionBehavior;
896 }
897 
905 {
906  m_valueCompletionBehavior = completionValues;
907 }
908 
912 inline const char *Argument::preDefinedCompletionValues() const
913 {
914  return m_preDefinedCompletionValues;
915 }
916 
920 inline void Argument::setPreDefinedCompletionValues(const char *preDefinedCompletionValues)
921 {
922  m_preDefinedCompletionValues = preDefinedCompletionValues;
923 }
924 
930 inline void Argument::reset()
931 {
932  m_occurrences.clear();
933 }
934 
939 inline const std::vector<ArgumentOccurrence> &Argument::occurrenceInfo() const
940 {
941  return m_occurrences;
942 }
943 
950 inline std::vector<ArgumentOccurrence> &Argument::occurrenceInfo()
951 {
952  return m_occurrences;
953 }
954 
960 {
961  return m_mainArgs;
962 }
963 
967 inline unsigned int ArgumentParser::actualArgumentCount() const
968 {
969  return m_actualArgc;
970 }
971 
975 inline const char *ArgumentParser::executable() const
976 {
977  return m_executable;
978 }
979 
986 {
987  return m_unknownArgBehavior;
988 }
989 
996 {
997  m_unknownArgBehavior = behavior;
998 }
999 
1005 {
1006  return m_defaultArg;
1007 }
1008 
1014 {
1015  m_defaultArg = argument;
1016 }
1017 
1024 {
1025  checkConstraints(m_mainArgs);
1026 }
1027 
1033 {
1034  invokeCallbacks(m_mainArgs);
1035 }
1036 
1038 public:
1039  HelpArgument(ArgumentParser &parser);
1040 };
1041 
1043 public:
1044  OperationArgument(const char *name, char abbreviation = '\0', const char *description = nullptr, const char *example = nullptr);
1045 };
1046 
1047 inline OperationArgument::OperationArgument(const char *name, char abbreviation, const char *description, const char *example)
1048  : Argument(name, abbreviation, description, example)
1049 {
1050  setDenotesOperation(true);
1051 }
1052 
1054 public:
1055  ConfigValueArgument(const char *name, char abbreviation = '\0', const char *description = nullptr,
1056  std::initializer_list<const char *> valueNames = std::initializer_list<const char *>());
1057 };
1058 
1063  const char *name, char abbreviation, const char *description, std::initializer_list<const char *> valueNames)
1064  : Argument(name, abbreviation, description)
1065 {
1066  setCombinable(true);
1069 }
1070 
1072  friend ArgumentParserTests;
1073 
1074 public:
1075  NoColorArgument();
1076  ~NoColorArgument();
1077  static void apply();
1078 
1079 private:
1080  static NoColorArgument *s_instance;
1081 };
1082 
1083 } // namespace ApplicationUtilities
1084 
1085 #endif // APPLICATION_UTILITIES_ARGUMENTPARSER_H
bool isMainArgument() const
Returns an indication whether the argument is used as main argument.
CPP_UTILITIES_EXPORT const char * applicationUrl
Specifies the URL to the application website (used by ArgumentParser::printHelp()).
ValueCompletionBehavior
The ValueCompletionBehavior enum specifies the items to be considered when generating completion for ...
const char * preDefinedCompletionValues() const
Returns the assigned values used when generating completion for the values.
std::tuple< RemainingTargetTypes... > convertValues() const
Converts the present values to the specified target types.
bool denotesOperation() const
Returns whether the argument denotes an operation.
UnknownArgumentBehavior unknownArgumentBehavior() const
Returns how unknown arguments are treated.
The ConfigValueArgument class is an Argument where setCombinable() is true by default.
#define IF_DEBUG_BUILD(x)
Wraps debug-only lines conveniently.
Argument * defaultArgument() const
Returns the default argument.
std::size_t requiredValueCount() const
Returns the number of values which are required to be given for this argument.
void setImplicit(bool value)
Sets whether the argument is an implicit argument.
const char * description() const
Returns the description of the argument.
std::size_t index
The index of the occurrence.
#define CPP_UTILITIES_EXPORT
void setCombinable(bool value)
Sets whether this argument can be combined.
std::vector< Argument * > ArgumentVector
ValueCompletionBehavior valueCompletionBehaviour() const
Returns the items to be considered when generating completion for the values.
void setUnknownArgumentBehavior(UnknownArgumentBehavior behavior)
Sets how unknown arguments are treated.
const ArgumentVector parents() const
Returns the parents of this argument.
The ConversionException class is thrown by the various conversion functions of this library when a co...
Contains currently only ArgumentParser and related classes.
std::size_t maxOccurrences() const
Returns the maximum number of occurrences.
constexpr DirectoryEntryType operator|(DirectoryEntryType lhs, DirectoryEntryType rhs)
Definition: path.h:28
constexpr DirectoryEntryType operator &(DirectoryEntryType lhs, DirectoryEntryType rhs)
Definition: path.h:38
bool isRequired() const
Returns an indication whether the argument is mandatory.
Argument CPP_UTILITIES_EXPORT * firstPresentUncombinableArg(const ArgumentVector &args, const Argument *except)
This function return the first present and uncombinable argument of the given list of arguments.
void setRequired(bool required)
Sets whether this argument is mandatory or not.
TargetType convert(const char *value)
const std::vector< ArgumentOccurrence > & occurrenceInfo() const
Returns information about all occurrences of the argument which have been detected when parsing.
std::size_t index(std::size_t occurrence) const
Returns the indices of the argument's occurences which could be detected when parsing.
std::function< void(const ArgumentOccurrence &)> CallbackFunction
void setDefaultArgument(Argument *argument)
Sets the default argument.
CPP_UTILITIES_EXPORT const char * applicationVersion
Specifies the version of the application (used by ArgumentParser::printHelp()).
void invokeCallbacks()
Invokes all assigned callbacks.
The NoColorArgument class allows to specify whether use of escape codes or similar technique to provi...
The ArgumentParserTests class tests the ArgumentParser and Argument classes.
void checkConstraints()
Checks whether contraints are violated.
const char * name() const
Returns the name of the argument.
const std::vector< const char * > & valueNames() const
Returns the names of the requried values.
The OperationArgument class is an Argument where denotesOperation() is true by default.
void setConstraints(std::size_t minOccurrences, std::size_t maxOccurrences)
Sets the allowed number of occurrences.
const ArgumentVector & subArguments() const
Returns the secondary arguments for this argument.
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition: math.h:29
std::initializer_list< Argument * > ArgumentInitializerList
std::size_t minOccurrences() const
Returns the minimum number of occurrences.
void setAbbreviation(char abbreviation)
Sets the abbreviation of the argument.
void setDescription(const char *description)
Sets the description of the argument.
std::function< bool(Argument *)> ArgumentPredicate
void setValueCompletionBehavior(ValueCompletionBehavior valueCompletionBehaviour)
Sets the items to be considered when generating completion for the values.
void appendValueName(const char *valueName)
Appends a value name.
CPP_UTILITIES_EXPORT std::initializer_list< const char * > dependencyVersions
Specifies the dependency versions the application was linked against (used by ArgumentParser::printHe...
const std::vector< const char * > & values(std::size_t occurrence=0) const
Returns the parameter values for the specified occurrence of argument.
CPP_UTILITIES_EXPORT void(* exitFunction)(int)
Specifies a function quit the application.
ParseArgumentBehavior
The ParseArgumentBehavior enum specifies the behavior when parsing arguments.
const char * executable() const
Returns the name of the current executable.
std::size_t occurrences() const
Returns how often the argument could be detected when parsing.
The Argument class is a wrapper for command line argument information.
void setCallback(CallbackFunction callback)
Sets a callback function which will be called by the parser if the argument could be found and no par...
void setExample(const char *example)
Sets the a usage example for the argument.
const std::vector< Argument * > & path(std::size_t occurrence=0) const
Returns the path of the specified occurrence.
static constexpr std::size_t varValueCount
Denotes a variable number of values.
bool isCombinable() const
Returns an indication whether the argument is combinable.
void setPreDefinedCompletionValues(const char *preDefinedCompletionValues)
Assignes the values to be used when generating completion for the values.
The ArgumentCompletionInfo struct holds information internally used for shell completion and suggesti...
std::vector< Argument * > path
The "path" of the occurrence (the parent elements which have been specified before).
UnknownArgumentBehavior
The UnknownArgumentBehavior enum specifies the behavior of the argument parser when an unknown argume...
OperationArgument(const char *name, char abbreviation='\0', const char *description=nullptr, const char *example=nullptr)
void setEnvironmentVariable(const char *environmentVariable)
Sets the environment variable queried when firstValue() is called.
void setValueNames(std::initializer_list< const char * > valueNames)
Sets the names of the requried values.
The ArgumentOccurrence struct holds argument values for an occurrence of an argument.
CPP_UTILITIES_EXPORT const char * applicationName
Specifies the name of the application (used by ArgumentParser::printHelp()).
bool isPresent() const
Returns an indication whether the argument could be detected when parsing.
bool hasSubArguments() const
Returns an indication whether the argument has secondary arguments.
const CallbackFunction & callback() const
Returns the assigned callback function.
The HelpArgument class prints help information for an argument parser when present (–help,...
bool allRequiredValuesPresent(std::size_t occurrence=0) const
Returns an indication whether all required values are present.
CPP_UTILITIES_EXPORT std::vector< const char * > dependencyVersions2
Specifies the dependency versions the application was linked against (used by ArgumentParser::printHe...
const ArgumentVector & mainArguments() const
Returns the main arguments.
void setDenotesOperation(bool denotesOperation)
Sets whether the argument denotes the operation.
char abbreviation() const
Returns the abbreviation of the argument.
const char * example() const
Returns the usage example of the argument.
void reset()
Resets occurrences (indices, values and paths).
ArgumentOccurrence(std::size_t index)
Constructs an argument occurrence for the specified index.
void setRequiredValueCount(std::size_t requiredValueCount)
Sets the number of values which are required to be given for this argument.
CPP_UTILITIES_EXPORT const char * applicationAuthor
Specifies the author of the application (used by ArgumentParser::printHelp()).
The ArgumentReader class internally encapsulates the process of reading command line arguments.
const char * environmentVariable() const
Returns the environment variable queried when firstValue() is called.
std::tuple< TargetType... > valuesAs(std::size_t occurrence=0) const
Converts the present values for the specified occurrence to the specified target types.
ConfigValueArgument(const char *name, char abbreviation='\0', const char *description=nullptr, std::initializer_list< const char * > valueNames=std::initializer_list< const char * >())
Constructs a new ConfigValueArgument with the specified parameter.
unsigned int actualArgumentCount() const
Returns the actual number of arguments that could be found when parsing.
void setName(const char *name)
Sets the name of the argument.
std::vector< std::tuple< TargetType... > > allValuesAs() const
Converts the present values for all occurrence to the specified target types.
bool isImplicit() const
Returns an indication whether the argument is an implicit argument.
The ArgumentParser class provides a means for handling command line arguments.
std::vector< const char * > values
The parameter values which have been specified after the occurrence of the argument.