C++ Utilities 5.27.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
|
The Argument class is a wrapper for command line argument information. More...
#include <argumentparser.h>
Public Types | |
enum class | Flags : std::uint64_t { None = 0x0 , Combinable = 0x1 , Implicit = 0x2 , Operation = 0x4 , Deprecated = 0x8 , Greedy = 0x10 } |
The Flags enum specifies options for treating the argument in a special way. More... | |
typedef std::function< void(const ArgumentOccurrence &)> | CallbackFunction |
Public Member Functions | |
Argument (const char *name, char abbreviation='\0', const char *description=nullptr, const char *example=nullptr) | |
Constructs an Argument with the given name, abbreviation and description. | |
~Argument () | |
Destroys the Argument. | |
const char * | name () const |
Returns the name of the argument. | |
void | setName (const char *name) |
Sets the name of the argument. | |
char | abbreviation () const |
Returns the abbreviation of the argument. | |
void | setAbbreviation (char abbreviation) |
Sets the abbreviation of the argument. | |
const char * | environmentVariable () const |
Returns the environment variable queried when firstValue() is called. | |
void | setEnvironmentVariable (const char *environmentVariable) |
Sets the environment variable queried when firstValue() is called. | |
const char * | description () const |
Returns the description of the argument. | |
void | setDescription (const char *description) |
Sets the description of the argument. | |
const char * | example () const |
Returns the usage example of the argument. | |
void | setExample (const char *example) |
Sets the a usage example for the argument. | |
std::size_t | requiredValueCount () const |
Returns the number of values which are required to be given for this argument. | |
void | setRequiredValueCount (std::size_t requiredValueCount) |
Sets the number of values which are required to be given for this argument. | |
const std::vector< const char * > & | valueNames () const |
Returns the names of the required values. | |
void | setValueNames (std::initializer_list< const char * > valueNames) |
Sets the names of the required values. | |
void | appendValueName (const char *valueName) |
Appends a value name. | |
void | setConstraints (std::size_t minOccurrences, std::size_t maxOccurrences) |
Sets the allowed number of occurrences. | |
const std::vector< Argument * > & | path (std::size_t occurrence=0) const |
Returns the path of the specified occurrence. | |
bool | isRequired () const |
Returns an indication whether the argument is mandatory. | |
void | setRequired (bool required) |
Sets whether this argument is mandatory or not. | |
Argument::Flags | flags () const |
Returns Argument::Flags for the argument. | |
void | setFlags (Argument::Flags flags) |
Replaces all Argument::Flags for the argument with the flags. | |
void | setFlags (Argument::Flags flags, bool add) |
Adds or removes the specified flags. | |
bool | isCombinable () const |
Returns an indication whether the argument is combinable. | |
void | setCombinable (bool combinable) |
Sets whether this argument can be combined. | |
bool | isImplicit () const |
Returns an indication whether the argument is an implicit argument. | |
void | setImplicit (bool implicit) |
Sets whether the argument is an implicit argument. | |
bool | denotesOperation () const |
Returns whether the argument denotes an operation. | |
void | setDenotesOperation (bool denotesOperation) |
Sets whether the argument denotes the operation. | |
const CallbackFunction & | callback () const |
Returns the assigned callback function. | |
void | setCallback (CallbackFunction callback) |
Sets a callback function which will be called by the parser if the argument could be found and no parsing errors occurred. | |
const ArgumentVector & | subArguments () const |
Returns the secondary arguments for this argument. | |
void | setSubArguments (const ArgumentInitializerList &subArguments) |
Sets the secondary arguments for this argument. | |
void | addSubArguments (const ArgumentInitializerList &subArguments) |
Sets the secondary arguments for this argument. | |
void | addSubArgument (Argument *arg) |
Adds arg as a secondary argument for this argument. | |
bool | hasSubArguments () const |
Returns an indication whether the argument has secondary arguments. | |
const ArgumentVector & | parents () const |
Returns the parents of this argument. | |
void | printInfo (std::ostream &os, unsigned char indentation=0) const |
Writes the name, the abbreviation and other information about the Argument to the give ostream. | |
ValueCompletionBehavior | valueCompletionBehaviour () const |
Returns the items to be considered when generating completion for the values. | |
void | setValueCompletionBehavior (ValueCompletionBehavior valueCompletionBehaviour) |
Sets the items to be considered when generating completion for the values. | |
const char * | preDefinedCompletionValues () const |
Returns the assigned values used when generating completion for the values. | |
void | setPreDefinedCompletionValues (const char *preDefinedCompletionValues) |
Assigns the values to be used when generating completion for the values. | |
const std::vector< const char * > & | values (std::size_t occurrence=0) const |
Returns the parameter values for the specified occurrence of argument. | |
template<typename... TargetType> | |
std::tuple< TargetType... > | valuesAs (std::size_t occurrence=0) const |
Converts the present values for the specified occurrence to the specified target types. | |
template<typename... TargetType> | |
std::vector< std::tuple< TargetType... > > | allValuesAs () const |
Converts the present values for all occurrence to the specified target types. | |
const char * | firstValue () const |
Returns the first parameter value of the first occurrence of the argument. | |
const char * | firstValueOr (const char *fallback) const |
Returns the first value like Argument::firstValue() but returns fallback instead of nullptr if there's no value. | |
bool | allRequiredValuesPresent (std::size_t occurrence=0) const |
Returns an indication whether all required values are present. | |
bool | isPresent () const |
Returns an indication whether the argument could be detected when parsing. | |
std::size_t | occurrences () const |
Returns how often the argument could be detected when parsing. | |
std::size_t | index (std::size_t occurrence) const |
Returns the indices of the argument's occurrences which could be detected when parsing. | |
std::size_t | minOccurrences () const |
Returns the minimum number of occurrences. | |
std::size_t | maxOccurrences () const |
Returns the maximum number of occurrences. | |
bool | isDeprecated () const |
const Argument * | deprecatedBy () const |
Returns the argument which obsoletes this argument. | |
void | markAsDeprecated (const Argument *deprecatedBy=nullptr) |
Marks the argument as deprecated. | |
bool | isMainArgument () const |
Returns an indication whether the argument is used as main argument. | |
bool | isParentPresent () const |
Returns whether at least one parent argument is present. | |
Argument * | conflictsWithArgument () const |
Checks if this arguments conflicts with other arguments. | |
Argument * | wouldConflictWithArgument () const |
Checks if this argument would conflict with other arguments if it was present. | |
Argument * | specifiedOperation () const |
Returns the first operation argument specified by the user or nullptr if no operation has been specified. | |
const std::vector< ArgumentOccurrence > & | occurrenceInfo () const |
Returns information about all occurrences of the argument which have been detected when parsing. | |
std::vector< ArgumentOccurrence > & | occurrenceInfo () |
Returns information about all occurrences of the argument which have been detected when parsing. | |
void | reset () |
Resets occurrences (indices, values and paths). | |
void | resetRecursively () |
Resets this argument and all sub arguments recursively. | |
Static Public Attributes | |
static constexpr std::size_t | varValueCount = std::numeric_limits<std::size_t>::max() |
Denotes a variable number of values. | |
The Argument class is a wrapper for command line argument information.
Instances of the Argument class are used as definition when parsing command line arguments. Arguments can be assigned to an ArgumentParser using ArgumentParser::setMainArguments() and to another Argument instance using Argument::setSubArguments().
Definition at line 263 of file argumentparser.h.
std::function<void(const ArgumentOccurrence &)> CppUtilities::Argument::CallbackFunction |
Definition at line 269 of file argumentparser.h.
|
strong |
The Flags enum specifies options for treating the argument in a special way.
Enumerator | |
---|---|
None | No flags are present. The default for Argument(). |
Combinable | It is no error if this argument occurs besides other arguments on the same level. The default for ConfigValueArgument. |
Implicit | The argument is assumed to be present if its values are present. Only one argument can be implicit at the same level. |
Operation | The argument is an operation, so no |
Deprecated | The argument is considered deprecated and therefore excluded from the help. |
Greedy | The argument is "greedy" so when Argument::varValueCount is used all subsequent arguments will be considered values of that argument. This is useful to pass remaining arguments down to another argument parser as-is. |
Definition at line 272 of file argumentparser.h.
CppUtilities::Argument::Argument | ( | const char * | name, |
char | abbreviation = '\0', | ||
const char * | description = nullptr, | ||
const char * | example = nullptr ) |
Constructs an Argument with the given name, abbreviation and description.
The name and the abbreviation mustn't contain any whitespaces. The name mustn't be empty. The abbreviation and the description might be empty.
Definition at line 463 of file argumentparser.cpp.
CppUtilities::Argument::~Argument | ( | ) |
Destroys the Argument.
Definition at line 484 of file argumentparser.cpp.
|
inline |
Returns the abbreviation of the argument.
The parser compares the abbreviation with the characters following a "-" prefix to identify arguments.
Definition at line 550 of file argumentparser.h.
void CppUtilities::Argument::addSubArgument | ( | Argument * | arg | ) |
Adds arg as a secondary argument for this argument.
Definition at line 666 of file argumentparser.cpp.
void CppUtilities::Argument::addSubArguments | ( | const ArgumentInitializerList & | subArguments | ) |
Sets the secondary arguments for this argument.
The given arguments will be considered as secondary arguments of this argument by the argument parser. This means that the parser will complain if these arguments are given, but not this argument. If secondary arguments are labeled as mandatory their parent is also mandatory.
The Argument does not take ownership. Do not destroy the given arguments as long as they are used as secondary arguments.
Definition at line 643 of file argumentparser.cpp.
|
inline |
Returns an indication whether all required values are present.
Definition at line 718 of file argumentparser.h.
std::vector< std::tuple< TargetType... > > CppUtilities::Argument::allValuesAs | ( | ) | const |
Converts the present values for all occurrence to the specified target types.
For each occurrence, there must be as many values present as types are specified.
Throws | ArgumentUtilities::Failure when the number of present values is not sufficient or a conversion error occurs. |
Definition at line 421 of file argumentparser.h.
|
inline |
Appends a value name.
The value names names will be shown when printing information about the argument.
Definition at line 710 of file argumentparser.h.
|
inline |
Returns the assigned callback function.
Definition at line 941 of file argumentparser.h.
Argument * CppUtilities::Argument::conflictsWithArgument | ( | ) | const |
Checks if this arguments conflicts with other arguments.
If the argument is in conflict with an other argument this argument will be returned. Otherwise nullptr will be returned.
Definition at line 702 of file argumentparser.cpp.
|
inline |
Returns whether the argument denotes an operation.
An argument which denotes an operation might be specified without "--" or "-" prefix.
The default value is false, except for OperationArgument instances.
Definition at line 923 of file argumentparser.h.
|
inline |
Returns the argument which obsoletes this argument.
Definition at line 794 of file argumentparser.h.
|
inline |
Returns the description of the argument.
The parser uses the description when printing help information.
Definition at line 592 of file argumentparser.h.
|
inline |
Returns the environment variable queried when firstValue() is called.
Definition at line 573 of file argumentparser.h.
|
inline |
Returns the usage example of the argument.
The parser uses the description when printing help information.
Definition at line 612 of file argumentparser.h.
const char * CppUtilities::Argument::firstValue | ( | ) | const |
Returns the first parameter value of the first occurrence of the argument.
Definition at line 495 of file argumentparser.cpp.
const char * CppUtilities::Argument::firstValueOr | ( | const char * | fallback | ) | const |
Returns the first value like Argument::firstValue() but returns fallback instead of nullptr if there's no value.
Definition at line 509 of file argumentparser.cpp.
|
inline |
Returns Argument::Flags for the argument.
Definition at line 864 of file argumentparser.h.
|
inline |
Returns an indication whether the argument has secondary arguments.
Definition at line 974 of file argumentparser.h.
|
inline |
Returns the indices of the argument's occurrences which could be detected when parsing.
Definition at line 761 of file argumentparser.h.
|
inline |
Returns an indication whether the argument is combinable.
The parser will complain if two arguments labeled as uncombinable are present at the same time.
Definition at line 895 of file argumentparser.h.
|
inline |
Definition at line 786 of file argumentparser.h.
|
inline |
Returns an indication whether the argument is an implicit argument.
Definition at line 728 of file argumentparser.h.
|
inline |
Returns an indication whether the argument is used as main argument.
An argument used as main argument shouldn't be used as secondary arguments at the same time.
Definition at line 1000 of file argumentparser.h.
bool CppUtilities::Argument::isParentPresent | ( | ) | const |
Returns whether at least one parent argument is present.
Definition at line 681 of file argumentparser.cpp.
|
inline |
Returns an indication whether the argument could be detected when parsing.
Definition at line 745 of file argumentparser.h.
|
inline |
Returns an indication whether the argument is mandatory.
The parser will complain if a mandatory argument is not present.
The default value is false.
Definition at line 838 of file argumentparser.h.
|
inline |
Marks the argument as deprecated.
If another argument should be used instead, specify it via deprecatedBy.
Definition at line 804 of file argumentparser.h.
|
inline |
Returns the maximum number of occurrences.
If the argument occurs more often, the parser will complain.
Definition at line 781 of file argumentparser.h.
|
inline |
Returns the minimum number of occurrences.
If the argument occurs not that many times, the parser will complain.
Definition at line 771 of file argumentparser.h.
|
inline |
Returns the name of the argument.
The parser compares the name with the characters following a "--" prefix to identify arguments.
Definition at line 520 of file argumentparser.h.
|
inline |
Returns information about all occurrences of the argument which have been detected when parsing.
Definition at line 1086 of file argumentparser.h.
|
inline |
Returns information about all occurrences of the argument which have been detected when parsing.
Definition at line 1075 of file argumentparser.h.
|
inline |
Returns how often the argument could be detected when parsing.
Definition at line 753 of file argumentparser.h.
|
inline |
Returns the parents of this argument.
If this argument is used as secondary argument, the arguments which contain this argument as secondary arguments are returned as "parents" of this argument.
If this argument is used as a main argument shouldn't be used as secondary argument at the same time and thus have no parents.
Definition at line 989 of file argumentparser.h.
|
inline |
Returns the path of the specified occurrence.
Definition at line 824 of file argumentparser.h.
|
inline |
Returns the assigned values used when generating completion for the values.
Definition at line 1030 of file argumentparser.h.
void CppUtilities::Argument::printInfo | ( | std::ostream & | os, |
unsigned char | indentation = 0 ) const |
Writes the name, the abbreviation and other information about the Argument to the give ostream.
Definition at line 521 of file argumentparser.cpp.
|
inline |
Returns the number of values which are required to be given for this argument.
The parser will expect that many values when parsing command line arguments. A negative value indicates a variable number of arguments to be expected.
The default value is 0, except for ConfigValueArgument instances.
Definition at line 651 of file argumentparser.h.
|
inline |
Resets occurrences (indices, values and paths).
So parsing results are wiped while the argument definition is preserved.
Definition at line 1048 of file argumentparser.h.
void CppUtilities::Argument::resetRecursively | ( | ) |
Resets this argument and all sub arguments recursively.
Definition at line 748 of file argumentparser.cpp.
|
inline |
Sets the abbreviation of the argument.
The abbreviation might be empty but mustn't be white spaces, equation char, single quote, double quote or newline.
The parser compares the abbreviation with the characters following a "-" prefix to identify arguments.
Definition at line 562 of file argumentparser.h.
|
inline |
Sets a callback function which will be called by the parser if the argument could be found and no parsing errors occurred.
Definition at line 952 of file argumentparser.h.
|
inline |
Sets whether this argument can be combined.
The parser will complain if two arguments labeled as uncombinable are present at the same time.
Definition at line 908 of file argumentparser.h.
|
inline |
Sets the allowed number of occurrences.
Definition at line 815 of file argumentparser.h.
|
inline |
Sets whether the argument denotes the operation.
Definition at line 932 of file argumentparser.h.
|
inline |
Sets the description of the argument.
The parser uses the description when printing help information.
Definition at line 602 of file argumentparser.h.
|
inline |
Sets the environment variable queried when firstValue() is called.
Definition at line 582 of file argumentparser.h.
|
inline |
Sets the a usage example for the argument.
The parser uses the description when printing help information.
Definition at line 622 of file argumentparser.h.
|
inline |
Replaces all Argument::Flags for the argument with the flags.
Definition at line 872 of file argumentparser.h.
|
inline |
Adds or removes the specified flags.
Definition at line 880 of file argumentparser.h.
|
inline |
Sets whether the argument is an implicit argument.
Definition at line 737 of file argumentparser.h.
|
inline |
Sets the name of the argument.
The name mustn't be empty, start with a minus or contain white spaces, equation chars, quotes and newlines.
The parser compares the name with the characters following a "--" prefix to identify arguments.
Definition at line 532 of file argumentparser.h.
|
inline |
Assigns the values to be used when generating completion for the values.
Definition at line 1038 of file argumentparser.h.
|
inline |
Sets whether this argument is mandatory or not.
The parser will complain if a mandatory argument is not present.
Definition at line 850 of file argumentparser.h.
|
inline |
Sets the number of values which are required to be given for this argument.
The parser will expect that many values when parsing command line arguments. Pass Argument::varValueCount for a variable number of arguments to be expected.
Definition at line 668 of file argumentparser.h.
void CppUtilities::Argument::setSubArguments | ( | const ArgumentInitializerList & | subArguments | ) |
Sets the secondary arguments for this argument.
The given arguments will be considered as secondary arguments of this argument by the argument parser. This means that the parser will complain if these arguments are given, but not this argument. If secondary arguments are labeled as mandatory their parent is also mandatory.
The Argument does not take ownership. Do not destroy the given arguments as long as they are used as secondary arguments.
Definition at line 617 of file argumentparser.cpp.
|
inline |
Sets the items to be considered when generating completion for the values.
By default, files and directories are considered, unless pre-defined values have been specified using setPreDefinedCompletionValues().
Definition at line 1022 of file argumentparser.h.
|
inline |
Sets the names of the required values.
These names will be used when printing information about the argument.
If the number of value names is higher than the number of required values the additional value names will be ignored. If the number of value names is lesser than the number of required values generic values will be used for the missing names.
Definition at line 699 of file argumentparser.h.
Argument * CppUtilities::Argument::specifiedOperation | ( | ) | const |
Returns the first operation argument specified by the user or nullptr if no operation has been specified.
Definition at line 734 of file argumentparser.cpp.
|
inline |
Returns the secondary arguments for this argument.
Definition at line 963 of file argumentparser.h.
|
inline |
Returns the items to be considered when generating completion for the values.
By default, files and directories are considered, unless pre-defined values have been specified using setPreDefinedCompletionValues().
Definition at line 1011 of file argumentparser.h.
|
inline |
Returns the names of the required values.
These names will be shown when printing information about the argument.
Definition at line 681 of file argumentparser.h.
|
inline |
Returns the parameter values for the specified occurrence of argument.
Definition at line 633 of file argumentparser.h.
std::tuple< TargetType... > CppUtilities::Argument::valuesAs | ( | std::size_t | occurrence = 0 | ) | const |
Converts the present values for the specified occurrence to the specified target types.
There must be as many values present as types are specified.
Throws | ArgumentUtilities::Failure when the number of present values is not sufficient or a conversion error occurs. |
Definition at line 411 of file argumentparser.h.
Argument * CppUtilities::Argument::wouldConflictWithArgument | ( | ) | const |
Checks if this argument would conflict with other arguments if it was present.
If the argument is in conflict with an other argument this argument will be returned. Otherwise nullptr will be returned.
Definition at line 715 of file argumentparser.cpp.
|
staticconstexpr |
Denotes a variable number of values.
Definition at line 368 of file argumentparser.h.