1 #ifndef ARGUMENTPARSER_H
2 #define ARGUMENTPARSER_H
9 #include <initializer_list>
34 Argument(
const std::string &name,
const std::string abbreviation = std::string(),
const std::string &description = std::string());
35 Argument(
const char *name,
const char *abbreviation =
nullptr,
const char *description =
nullptr);
38 const std::string &name()
const;
39 void setName(
const std::string &name);
40 const std::string &abbreviation()
const;
41 void setAbbreviation(
const std::string &abbreviation);
42 const std::string &description()
const;
43 void setDescription(
const std::string &description);
44 const StringVector &values()
const;
45 const std::string &value(StringVector::size_type index)
const;
46 StringVector::size_type valueCount()
const;
47 int requiredValueCount()
const;
48 void setRequiredValueCount(
int requiredValueCount);
49 const StringList &valueNames()
const;
50 void setValueNames(std::initializer_list<std::string> valueNames);
51 void appendValueName(
const char *valueName);
52 void appendValueName(
const std::string &valueName);
53 bool allRequiredValuesPresent()
const;
54 bool isPresent()
const;
55 bool isRequired()
const;
56 void setRequired(
bool value);
57 bool isCombinable()
const;
58 void setCombinable(
bool value);
59 void setCallback(CallbackFunction callback);
60 void printInfo(std::ostream &os)
const;
61 const ArgumentVector &secondaryArguments()
const;
62 void setSecondaryArguments(
const ArgumentInitializerList &secondaryArguments);
63 bool hasSecondaryArguments()
const;
64 const ArgumentVector parents()
const;
65 bool isMainArgument()
const;
66 std::string parentNames()
const;
67 bool isParentPresent()
const;
68 Argument *conflictsWithArgument()
const;
72 std::string m_abbreviation;
73 std::string m_description;
76 int m_requiredValueCount;
77 StringList m_valueNames;
79 StringVector m_values;
80 ArgumentVector m_secondaryArgs;
81 CallbackFunction m_callbackFunction;
82 ArgumentVector m_parents;
107 if(name.empty() || name.find(
' ') != std::string::npos || name.find(
'=') != std::string::npos) {
108 throw std::invalid_argument(
"name mustn't be empty or contain white spaces or equation chars");
121 return m_abbreviation;
135 if(!abbreviation.empty() && (abbreviation.find(
' ') != std::string::npos || abbreviation.find(
'=') != std::string::npos)) {
136 throw std::invalid_argument(
"abbreviation mustn't contain white spaces or equation chars");
148 return m_description;
176 return m_values.at(index);
185 return m_values.size();
201 return m_requiredValueCount;
246 m_valueNames = std::list<std::string>(
valueNames);
255 m_valueNames.emplace_back(valueName);
267 m_valueNames.push_back(valueName);
275 if(m_requiredValueCount > 0) {
276 return m_values.size() >=
static_cast<unsigned int>(m_requiredValueCount);
338 m_combinable =
value;
347 m_callbackFunction = callback;
357 return m_secondaryArgs;
368 return !m_secondaryArgs.empty();
400 void setMainArguments(
const ArgumentInitializerList &mainArguments);
401 void printHelp(std::ostream &os)
const;
402 Argument *findArg(
const ArgumentPredicate &predicate)
const;
403 Argument *findArg(
const ArgumentVector &arguments,
const ArgumentPredicate &predicate)
const;
404 void verifySetup()
const;
405 void parseArgs(
int argc,
char *argv[]);
406 unsigned int actualArgumentCount()
const;
407 const std::string ¤tDirectory()
const;
408 bool areUnknownArgumentsIgnored()
const;
409 void setIgnoreUnknownArguments(
bool ignore);
412 ArgumentVector m_mainArgs;
413 unsigned int m_actualArgc;
414 std::string m_currentDirectory;
415 bool m_ignoreUnknownArgs;
431 return m_currentDirectory;
445 return m_ignoreUnknownArgs;
457 m_ignoreUnknownArgs = ignore;
464 #endif // ARGUMENTPARSER_H
std::initializer_list< Argument * > ArgumentInitializerList
StringVector::size_type valueCount() const
Returns the number of values which could be found when parsing the command line arguments.
void setDescription(const std::string &description)
Sets the description of the argument.
bool isPresent() const
Returns an indication whether the argument could be detected when parsing.
void setCombinable(bool value)
Sets if this argument can be combined.
const StringVector & values() const
Returns the additional values for the argument.
const ArgumentVector & secondaryArguments() const
Returns the secondary arguments for this argument.
const ArgumentVector parents() const
Returns the parents of this argument.
Contains currently only ArgumentParser and related classes.
const std::string & abbreviation() const
Returns the abbreviation of the argument.
Argument * firstPresentUncombinableArg(const ArgumentVector &args, const Argument *except)
This function return the first present and uncombinable argument of the given list of arguments...
std::list< std::string > StringList
#define LIB_EXPORT
This macro marks a symbol for shared library export.
std::vector< std::string > StringVector
void setRequired(bool value)
Sets if this argument is mandatory or not.
const std::string & value(StringVector::size_type index) const
Returns the value with the give index.
std::function< bool(Argument *)> ArgumentPredicate
bool isRequired() const
Returns an indication whether the argument is mandatory.
bool isCombinable() const
Returns an indication whether the argument is combinable.
void setRequiredValueCount(int requiredValueCount)
Sets the number of values which are required to be given for this argument.
void setName(const std::string &name)
Sets the name of the argument.
void setAbbreviation(const std::string &abbreviation)
Sets the abbreviation of the argument.
const std::string & name() const
Returns the name of the argument.
bool isMainArgument() const
Returns an indication whether the argument is used as main argument.
void appendValueName(const char *valueName)
Appends a value name.
void setValueNames(std::initializer_list< std::string > valueNames)
Sets the names of the requried values.
bool allRequiredValuesPresent() const
Returns an indication whether all required values are present.
The Argument class is a wrapper for command line argument information.
const std::string & currentDirectory() const
Returns the current directory.
void setCallback(CallbackFunction callback)
Sets a callback function which will be called by the parser if the argument could be found and no par...
unsigned int actualArgumentCount() const
Returns the actual number of arguments that could be found when parsing.
const StringList & valueNames() const
Returns the names of the requried values.
const std::string & description() const
Returns the description of the argument.
std::function< void(const StringVector &)> CallbackFunction
int requiredValueCount() const
Returns the number of values which are required to be given for this argument.
bool hasSecondaryArguments() const
Returns an indication whether the argument has secondary arguments.
void setIgnoreUnknownArguments(bool ignore)
Set to true if the parser should ignore unknown arguments when parsing.
The ArgumentParser class provides a means for handling command line arguments.
bool areUnknownArgumentsIgnored() const
Returns an indication whether unknown arguments detected when parsing should be ignored.
std::vector< Argument * > ArgumentVector