1 #ifndef APPLICATION_UTILITIES_ARGUMENTPARSER_H 2 #define APPLICATION_UTILITIES_ARGUMENTPARSER_H 4 #include "../conversion/stringconversion.h" 6 #include "../misc/traits.h" 9 #include <initializer_list> 33 #ifndef APP_STATICALLY_LINKED 34 #define SET_DEPENDENCY_INFO ::ApplicationUtilities::dependencyVersions2 = DEPENCENCY_VERSIONS 36 #define SET_DEPENDENCY_INFO ::ApplicationUtilities::dependencyVersions2 = STATIC_DEPENCENCY_VERSIONS 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; \ 88 return static_cast<ParseArgumentBehavior>(
static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs));
93 return static_cast<bool>(
static_cast<unsigned char>(lhs) & static_cast<unsigned char>(rhs));
119 return static_cast<ValueCompletionBehavior>(
static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs));
124 return static_cast<bool>(
static_cast<unsigned char>(lhs) & static_cast<unsigned char>(rhs));
135 namespace ValueConversion {
136 template <
typename TargetType> TargetType
convert(
const char *value);
138 template <
typename TargetType, Traits::EnableIf<std::is_same<TargetType, std::
string>>> TargetType
convert(
const char *value)
140 return std::string(value);
143 template <
typename TargetType, Traits::EnableIf<std::is_arithmetic<TargetType>>> TargetType
convert(
const char *value)
145 return ConversionUtilities::stringToNumber<TargetType>(value);
172 template <
typename TargetType> std::tuple<TargetType> convertValues()
const;
173 template <
typename FirstTargetType,
typename... RemainingTargetTypes> std::tuple<FirstTargetType, RemainingTargetTypes...> convertValues()
const;
176 template <
typename FirstTargetType,
typename... RemainingTargetTypes>
177 std::tuple<FirstTargetType, RemainingTargetTypes...> convertValues(std::vector<const char *>::const_iterator firstValue)
const;
186 return std::make_tuple<TargetType>(ValueConversion::convert<TargetType>(
values.front()));
193 template <
typename FirstTargetType,
typename... RemainingTargetTypes>
196 return std::tuple_cat(std::make_tuple<FirstTargetType>(ValueConversion::convert<FirstTargetType>(
values.front())),
201 template <
typename FirstTargetType,
typename... RemainingTargetTypes>
204 return std::tuple_cat(std::make_tuple<FirstTargetType>(ValueConversion::convert<FirstTargetType>(*firstValue)),
205 convertValues<RemainingTargetTypes...>(firstValue + 1));
230 path.push_back(parent);
242 Argument(
const char *name,
char abbreviation =
'\0',
const char *description =
nullptr,
const char *example =
nullptr);
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);
283 bool hasSubArguments()
const;
285 void printInfo(std::ostream &os,
unsigned char indentation = 0)
const;
290 const char *preDefinedCompletionValues()
const;
291 void setPreDefinedCompletionValues(
const char *preDefinedCompletionValues);
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;
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();
313 void resetRecursively();
323 bool matchesDenotation(
const char *denotation,
size_t denotationLength)
const;
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;
333 bool m_denotesOperation;
334 std::size_t m_requiredValueCount;
335 std::vector<const char *> m_valueNames;
337 std::vector<ArgumentOccurrence> m_occurrences;
343 const char *m_preDefinedCompletionValues;
352 return m_occurrences[occurrence].convertValues<TargetType...>();
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...>());
381 void addMainArgument(
Argument *argument);
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,
390 void readArgs(
int argc,
const char *
const *argv);
392 void checkConstraints();
393 void invokeCallbacks();
396 unsigned int actualArgumentCount()
const;
397 const char *executable()
const;
401 void setDefaultArgument(
Argument *argument);
402 Argument *specifiedOperation()
const;
403 bool isUncombinableMainArgPresent()
const;
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;
416 unsigned int m_actualArgc;
417 const char *m_executable;
443 assert(*
name !=
'-');
444 for (
const char *c =
name; *c; ++c) {
445 assert(*c !=
' ' && *c !=
'=' && *c !=
'\'' && *c !=
'\"' && *c !=
'\n' && *c !=
'\r');
459 return m_abbreviation;
482 return m_environmentVar;
501 return m_description;
542 return m_occurrences[occurrence].values;
560 return m_requiredValueCount;
619 m_valueNames.emplace_back(valueName);
628 || (m_occurrences[occurrence].values.size() >=
static_cast<std::size_t
>(m_requiredValueCount));
646 m_implicit = implicit;
654 return !m_occurrences.empty();
662 return m_occurrences.size();
670 return m_occurrences[occurrence].index;
680 return m_minOccurrences;
690 return m_maxOccurrences;
707 inline const std::vector<Argument *> &
Argument::path(std::size_t occurrence)
const 709 return m_occurrences[occurrence].path;
723 return m_minOccurrences;
736 if (!m_minOccurrences) {
737 m_minOccurrences = 1;
740 m_minOccurrences = 0;
767 m_combinable = value;
782 return m_denotesOperation;
800 return m_callbackFunction;
833 return !m_subArgs.empty();
872 return m_valueCompletionBehavior;
883 m_valueCompletionBehavior = completionValues;
891 return m_preDefinedCompletionValues;
909 m_occurrences.clear();
918 return m_occurrences;
929 return m_occurrences;
964 return m_unknownArgBehavior;
974 m_unknownArgBehavior = behavior;
992 m_defaultArg = argument;
1021 OperationArgument(
const char *name,
char abbreviation =
'\0',
const char *description =
nullptr,
const char *example =
nullptr);
1025 :
Argument(name, abbreviation, description, example)
1032 ConfigValueArgument(
const char *name,
char abbreviation =
'\0',
const char *description =
nullptr,
1033 std::initializer_list<const char *> valueNames = std::initializer_list<const char *>());
1040 const char *name,
char abbreviation,
const char *description, std::initializer_list<const char *> valueNames)
1041 :
Argument(name, abbreviation, description)
1054 static void apply();
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.
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)
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'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.
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)
std::vector< const char * > values
The parameter values which have been specified after the occurrence of the argument.