C++ Utilities  4.14.1
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;
56 
57 typedef std::initializer_list<Argument *> ArgumentInitializerList;
58 typedef std::vector<Argument *> ArgumentVector;
59 typedef std::function<bool(Argument *)> ArgumentPredicate;
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 
135 namespace ValueConversion {
136 template <typename TargetType> TargetType convert(const char *value);
137 
138 template <typename TargetType, Traits::EnableIf<std::is_same<TargetType, std::string>>> TargetType convert(const char *value)
139 {
140  return std::string(value);
141 }
142 
143 template <typename TargetType, Traits::EnableIf<std::is_arithmetic<TargetType>>> TargetType convert(const char *value)
144 {
145  return ConversionUtilities::stringToNumber<TargetType>(value);
146 }
147 } // namespace ValueConversion
148 
153  ArgumentOccurrence(std::size_t index);
154  ArgumentOccurrence(std::size_t index, const std::vector<Argument *> parentPath, Argument *parent);
155 
159  std::size_t index;
160 
164  std::vector<const char *> values;
165 
170  std::vector<Argument *> path;
171 
172  template <typename TargetType> std::tuple<TargetType> convertValues() const;
173  template <typename FirstTargetType, typename... RemainingTargetTypes> std::tuple<FirstTargetType, RemainingTargetTypes...> convertValues() const;
174 
175 private:
176  template <typename FirstTargetType, typename... RemainingTargetTypes>
177  std::tuple<FirstTargetType, RemainingTargetTypes...> convertValues(std::vector<const char *>::const_iterator firstValue) const;
178 };
179 
184 template <typename TargetType> std::tuple<TargetType> ArgumentOccurrence::convertValues() const
185 {
186  return std::make_tuple<TargetType>(ValueConversion::convert<TargetType>(values.front()));
187 }
188 
193 template <typename FirstTargetType, typename... RemainingTargetTypes>
194 std::tuple<FirstTargetType, RemainingTargetTypes...> ArgumentOccurrence::convertValues() const
195 {
196  return std::tuple_cat(std::make_tuple<FirstTargetType>(ValueConversion::convert<FirstTargetType>(values.front())),
197  convertValues<RemainingTargetTypes...>(values.cbegin() + 1));
198 }
199 
201 template <typename FirstTargetType, typename... RemainingTargetTypes>
202 std::tuple<FirstTargetType, RemainingTargetTypes...> ArgumentOccurrence::convertValues(std::vector<const char *>::const_iterator firstValue) const
203 {
204  return std::tuple_cat(std::make_tuple<FirstTargetType>(ValueConversion::convert<FirstTargetType>(*firstValue)),
205  convertValues<RemainingTargetTypes...>(firstValue + 1));
206 }
208 
212 inline ArgumentOccurrence::ArgumentOccurrence(std::size_t index)
213  : index(index)
214 {
215 }
216 
225 inline ArgumentOccurrence::ArgumentOccurrence(std::size_t index, const std::vector<Argument *> parentPath, Argument *parent)
226  : index(index)
227  , path(parentPath)
228 {
229  if (parent) {
230  path.push_back(parent);
231  }
232 }
233 
235  friend ArgumentParser;
236  friend ArgumentReader;
237  friend ArgumentParserTests;
238 
239 public:
240  typedef std::function<void(const ArgumentOccurrence &)> CallbackFunction;
241 
242  Argument(const char *name, char abbreviation = '\0', const char *description = nullptr, const char *example = nullptr);
243  ~Argument();
244 
245  // declare getter/setter/properties/operations for argument definition:
246  // - those properties must be set *before* parsing
247  // - they control the behaviour of the parser, eg.
248  // - the name/abbreviation to look for
249  // - constraints to be checked
250  // - callbacks to be invoked
251  // TODO v5: It would make sense to move these to a separate class (eg. ArgumentDefinition) to prevent this one from
252  // becoming to big.
253  const char *name() const;
254  void setName(const char *name);
255  char abbreviation() const;
256  void setAbbreviation(char abbreviation);
257  const char *environmentVariable() const;
258  void setEnvironmentVariable(const char *environmentVariable);
259  const char *description() const;
260  void setDescription(const char *description);
261  const char *example() const;
262  void setExample(const char *example);
263  std::size_t requiredValueCount() const;
264  void setRequiredValueCount(std::size_t requiredValueCount);
265  const std::vector<const char *> &valueNames() const;
266  void setValueNames(std::initializer_list<const char *> valueNames);
267  void appendValueName(const char *valueName);
268  void setConstraints(std::size_t minOccurrences, std::size_t maxOccurrences);
269  const std::vector<Argument *> &path(std::size_t occurrence = 0) const;
270  bool isRequired() const;
271  void setRequired(bool required);
272  bool isCombinable() const;
273  void setCombinable(bool value);
274  bool isImplicit() const;
275  void setImplicit(bool value);
276  bool denotesOperation() const;
277  void setDenotesOperation(bool denotesOperation);
278  const CallbackFunction &callback() const;
279  void setCallback(CallbackFunction callback);
280  const ArgumentVector &subArguments() const;
281  void setSubArguments(const ArgumentInitializerList &subArguments);
282  void addSubArgument(Argument *arg);
283  bool hasSubArguments() const;
284  const ArgumentVector parents() const;
285  void printInfo(std::ostream &os, unsigned char indentation = 0) const;
286 
287  // declare getter/setter/properties for bash completion: those properties must be set *before parsing
288  ValueCompletionBehavior valueCompletionBehaviour() const;
289  void setValueCompletionBehavior(ValueCompletionBehavior valueCompletionBehaviour);
290  const char *preDefinedCompletionValues() const;
291  void setPreDefinedCompletionValues(const char *preDefinedCompletionValues);
292 
293  // declare getter/read-only properties for parsing results: those properties will be populated when parsing
294  const std::vector<const char *> &values(std::size_t occurrence = 0) const;
295  template <typename... TargetType> std::tuple<TargetType...> convertValues(std::size_t occurrence = 0) const;
296  template <typename... TargetType> std::vector<std::tuple<TargetType...>> convertAllValues() const;
297 
298  const char *firstValue() const;
299  bool allRequiredValuesPresent(std::size_t occurrence = 0) const;
300  bool isPresent() const;
301  std::size_t occurrences() const;
302  std::size_t index(std::size_t occurrence) const;
303  std::size_t minOccurrences() const;
304  std::size_t maxOccurrences() const;
305  bool isMainArgument() const;
306  bool isParentPresent() const;
307  Argument *conflictsWithArgument() const;
308  Argument *wouldConflictWithArgument() const;
309  Argument *specifiedOperation() const;
310  const std::vector<ArgumentOccurrence> &occurrenceInfo() const;
311  std::vector<ArgumentOccurrence> &occurrenceInfo();
312  void reset();
313  void resetRecursively();
314 
319  static constexpr std::size_t varValueCount = std::numeric_limits<std::size_t>::max();
320 
321 private:
322  // declare internal getter/setter/properties/operations for argument definition
323  bool matchesDenotation(const char *denotation, size_t denotationLength) const;
324 
325  const char *m_name;
326  char m_abbreviation;
327  const char *m_environmentVar;
328  const char *m_description;
329  const char *m_example;
330  std::size_t m_minOccurrences;
331  std::size_t m_maxOccurrences;
332  bool m_combinable;
333  bool m_denotesOperation;
334  std::size_t m_requiredValueCount;
335  std::vector<const char *> m_valueNames;
336  bool m_implicit;
337  std::vector<ArgumentOccurrence> m_occurrences;
338  ArgumentVector m_subArgs;
339  CallbackFunction m_callbackFunction;
340  ArgumentVector m_parents;
341  bool m_isMainArg;
342  ValueCompletionBehavior m_valueCompletionBehavior;
343  const char *m_preDefinedCompletionValues;
344 };
345 
350 template <typename... TargetType> std::tuple<TargetType...> Argument::convertValues(std::size_t occurrence) const
351 {
352  return m_occurrences[occurrence].convertValues<TargetType...>();
353 }
354 
359 template <typename... TargetType> std::vector<std::tuple<TargetType...>> Argument::convertAllValues() const
360 {
361  std::vector<std::tuple<TargetType...>> res;
362  res.reserve(m_occurrences.size());
363  for (const auto &occurrence : m_occurrences) {
364  res.emplace_back(occurrence.convertValues<TargetType...>());
365  }
366  return res;
367 }
368 
370 
372  friend ArgumentParserTests;
373  friend ArgumentReader;
374 
375 public:
376  ArgumentParser();
377 
378  // declare getter/setter for argument definitions
379  const ArgumentVector &mainArguments() const;
380  void setMainArguments(const ArgumentInitializerList &mainArguments);
381  void addMainArgument(Argument *argument);
382 
383  // declare operations which will consider previously assigned argument definitions and maybe modify parsing results
384  void printHelp(std::ostream &os) const;
385  void parseArgs(int argc, const char *const *argv);
386  void parseArgsOrExit(int argc, const char *const *argv);
387  void parseArgsExt(int argc, const char *const *argv,
388  ParseArgumentBehavior behavior
390  void readArgs(int argc, const char *const *argv);
391  void resetArgs();
392  void checkConstraints();
393  void invokeCallbacks();
394 
395  // declare getter for parsing results
396  unsigned int actualArgumentCount() const;
397  const char *executable() const;
398  UnknownArgumentBehavior unknownArgumentBehavior() const;
399  void setUnknownArgumentBehavior(UnknownArgumentBehavior behavior);
400  Argument *defaultArgument() const;
401  void setDefaultArgument(Argument *argument);
402  Argument *specifiedOperation() const;
403  bool isUncombinableMainArgPresent() const;
404 
405 private:
406  // declare internal operations
407  IF_DEBUG_BUILD(void verifyArgs(const ArgumentVector &args);)
408  ArgumentCompletionInfo determineCompletionInfo(
409  int argc, const char *const *argv, unsigned int currentWordIndex, const ArgumentReader &reader) const;
410  std::string findSuggestions(int argc, const char *const *argv, unsigned int cursorPos, const ArgumentReader &reader) const;
411  void printBashCompletion(int argc, const char *const *argv, unsigned int cursorPos, const ArgumentReader &reader) const;
412  void checkConstraints(const ArgumentVector &args);
413  static void invokeCallbacks(const ArgumentVector &args);
414 
415  ArgumentVector m_mainArgs;
416  unsigned int m_actualArgc;
417  const char *m_executable;
418  UnknownArgumentBehavior m_unknownArgBehavior;
419  Argument *m_defaultArg;
420 };
421 
427 inline const char *Argument::name() const
428 {
429  return m_name;
430 }
431 
439 inline void Argument::setName(const char *name)
440 {
441 #ifdef DEBUG_BUILD
442  if (name && *name) {
443  assert(*name != '-');
444  for (const char *c = name; *c; ++c) {
445  assert(*c != ' ' && *c != '=' && *c != '\'' && *c != '\"' && *c != '\n' && *c != '\r');
446  }
447  }
448 #endif
449  m_name = name;
450 }
451 
457 inline char Argument::abbreviation() const
458 {
459  return m_abbreviation;
460 }
461 
469 inline void Argument::setAbbreviation(char abbreviation)
470 {
471  IF_DEBUG_BUILD(assert(abbreviation != ' ' && abbreviation != '=' && abbreviation != '-' && abbreviation != '\'' && abbreviation != '"'
472  && abbreviation != '\n' && abbreviation != '\r'));
473  m_abbreviation = abbreviation;
474 }
475 
480 inline const char *Argument::environmentVariable() const
481 {
482  return m_environmentVar;
483 }
484 
489 inline void Argument::setEnvironmentVariable(const char *environmentVariable)
490 {
491  m_environmentVar = environmentVariable;
492 }
493 
499 inline const char *Argument::description() const
500 {
501  return m_description;
502 }
503 
509 inline void Argument::setDescription(const char *description)
510 {
511  m_description = description;
512 }
513 
519 inline const char *Argument::example() const
520 {
521  return m_example;
522 }
523 
529 inline void Argument::setExample(const char *example)
530 {
531  m_example = example;
532 }
533 
540 inline const std::vector<const char *> &Argument::values(std::size_t occurrence) const
541 {
542  return m_occurrences[occurrence].values;
543 }
544 
558 inline std::size_t Argument::requiredValueCount() const
559 {
560  return m_requiredValueCount;
561 }
562 
575 inline void Argument::setRequiredValueCount(std::size_t requiredValueCount)
576 {
577  m_requiredValueCount = requiredValueCount;
578 }
579 
588 inline const std::vector<const char *> &Argument::valueNames() const
589 {
590  return m_valueNames;
591 }
592 
606 inline void Argument::setValueNames(std::initializer_list<const char *> valueNames)
607 {
608  m_valueNames.assign(valueNames);
609 }
610 
617 inline void Argument::appendValueName(const char *valueName)
618 {
619  m_valueNames.emplace_back(valueName);
620 }
621 
625 inline bool Argument::allRequiredValuesPresent(std::size_t occurrence) const
626 {
627  return m_requiredValueCount == Argument::varValueCount
628  || (m_occurrences[occurrence].values.size() >= static_cast<std::size_t>(m_requiredValueCount));
629 }
630 
635 inline bool Argument::isImplicit() const
636 {
637  return m_implicit;
638 }
639 
644 inline void Argument::setImplicit(bool implicit)
645 {
646  m_implicit = implicit;
647 }
648 
652 inline bool Argument::isPresent() const
653 {
654  return !m_occurrences.empty();
655 }
656 
660 inline std::size_t Argument::occurrences() const
661 {
662  return m_occurrences.size();
663 }
664 
668 inline std::size_t Argument::index(std::size_t occurrence) const
669 {
670  return m_occurrences[occurrence].index;
671 }
672 
678 inline std::size_t Argument::minOccurrences() const
679 {
680  return m_minOccurrences;
681 }
682 
688 inline std::size_t Argument::maxOccurrences() const
689 {
690  return m_maxOccurrences;
691 }
692 
698 inline void Argument::setConstraints(std::size_t minOccurrences, std::size_t maxOccurrences)
699 {
700  m_minOccurrences = minOccurrences;
701  m_maxOccurrences = maxOccurrences;
702 }
703 
707 inline const std::vector<Argument *> &Argument::path(std::size_t occurrence) const
708 {
709  return m_occurrences[occurrence].path;
710 }
711 
721 inline bool Argument::isRequired() const
722 {
723  return m_minOccurrences;
724 }
725 
733 inline void Argument::setRequired(bool required)
734 {
735  if (required) {
736  if (!m_minOccurrences) {
737  m_minOccurrences = 1;
738  }
739  } else {
740  m_minOccurrences = 0;
741  }
742 }
743 
752 inline bool Argument::isCombinable() const
753 {
754  return m_combinable;
755 }
756 
765 inline void Argument::setCombinable(bool value)
766 {
767  m_combinable = value;
768 }
769 
780 inline bool Argument::denotesOperation() const
781 {
782  return m_denotesOperation;
783 }
784 
789 inline void Argument::setDenotesOperation(bool denotesOperation)
790 {
791  m_denotesOperation = denotesOperation;
792 }
793 
799 {
800  return m_callbackFunction;
801 }
802 
810 {
811  m_callbackFunction = callback;
812 }
813 
821 {
822  return m_subArgs;
823 }
824 
831 inline bool Argument::hasSubArguments() const
832 {
833  return !m_subArgs.empty();
834 }
835 
848 inline const ArgumentVector Argument::parents() const
849 {
850  return m_parents;
851 }
852 
859 inline bool Argument::isMainArgument() const
860 {
861  return m_isMainArg;
862 }
863 
871 {
872  return m_valueCompletionBehavior;
873 }
874 
882 {
883  m_valueCompletionBehavior = completionValues;
884 }
885 
889 inline const char *Argument::preDefinedCompletionValues() const
890 {
891  return m_preDefinedCompletionValues;
892 }
893 
897 inline void Argument::setPreDefinedCompletionValues(const char *preDefinedCompletionValues)
898 {
899  m_preDefinedCompletionValues = preDefinedCompletionValues;
900 }
901 
907 inline void Argument::reset()
908 {
909  m_occurrences.clear();
910 }
911 
916 inline const std::vector<ArgumentOccurrence> &Argument::occurrenceInfo() const
917 {
918  return m_occurrences;
919 }
920 
927 inline std::vector<ArgumentOccurrence> &Argument::occurrenceInfo()
928 {
929  return m_occurrences;
930 }
931 
937 {
938  return m_mainArgs;
939 }
940 
944 inline unsigned int ArgumentParser::actualArgumentCount() const
945 {
946  return m_actualArgc;
947 }
948 
952 inline const char *ArgumentParser::executable() const
953 {
954  return m_executable;
955 }
956 
963 {
964  return m_unknownArgBehavior;
965 }
966 
973 {
974  m_unknownArgBehavior = behavior;
975 }
976 
982 {
983  return m_defaultArg;
984 }
985 
991 {
992  m_defaultArg = argument;
993 }
994 
1001 {
1002  checkConstraints(m_mainArgs);
1003 }
1004 
1010 {
1011  invokeCallbacks(m_mainArgs);
1012 }
1013 
1015 public:
1016  HelpArgument(ArgumentParser &parser);
1017 };
1018 
1020 public:
1021  OperationArgument(const char *name, char abbreviation = '\0', const char *description = nullptr, const char *example = nullptr);
1022 };
1023 
1024 inline OperationArgument::OperationArgument(const char *name, char abbreviation, const char *description, const char *example)
1025  : Argument(name, abbreviation, description, example)
1026 {
1027  setDenotesOperation(true);
1028 }
1029 
1031 public:
1032  ConfigValueArgument(const char *name, char abbreviation = '\0', const char *description = nullptr,
1033  std::initializer_list<const char *> valueNames = std::initializer_list<const char *>());
1034 };
1035 
1040  const char *name, char abbreviation, const char *description, std::initializer_list<const char *> valueNames)
1041  : Argument(name, abbreviation, description)
1042 {
1043  setCombinable(true);
1046 }
1047 
1049  friend ArgumentParserTests;
1050 
1051 public:
1052  NoColorArgument();
1053  ~NoColorArgument();
1054  static void apply();
1055 
1056 private:
1057  static NoColorArgument *s_instance;
1058 };
1059 
1060 } // namespace ApplicationUtilities
1061 
1062 #endif // APPLICATION_UTILITIES_ARGUMENTPARSER_H
bool isMainArgument() const
Returns an indication whether the argument is used as main argument.
std::function< bool(Argument *)> ArgumentPredicate
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.
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.
std::initializer_list< Argument * > ArgumentInitializerList
TargetType convert(const char *value)
#define IF_DEBUG_BUILD(x)
Wraps debug-only lines conveniently.
Definition: global.h:130
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.
void setCombinable(bool value)
Sets whether this argument can be combined.
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.
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
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.
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&#39;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()).
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.
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.
std::tuple< TargetType... > convertValues(std::size_t occurrence=0) const
Converts the present values for the specified occurrence to the specified target types.
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::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.
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.
std::tuple< TargetType > convertValues() const
Converts the present value to the specified target type.
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.
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).
std::vector< std::tuple< TargetType... > > convertAllValues() const
Converts the present values for all occurrence to the specified target types.
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...
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
const char * environmentVariable() const
Returns the environment variable queried when firstValue() is called.
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.
bool isImplicit() const
Returns an indication whether the argument is an implicit argument.
The ArgumentParser class provides a means for handling command line arguments.
void setValueNames(std::initializer_list< const char *> valueNames)
Sets the names of the requried values.
std::vector< Argument * > ArgumentVector
constexpr DirectoryEntryType operator &(DirectoryEntryType lhs, DirectoryEntryType rhs)
Definition: path.h:38
std::vector< const char * > values
The parameter values which have been specified after the occurrence of the argument.