diff --git a/application/argumentparser.cpp b/application/argumentparser.cpp index b3b36ce..26ea765 100644 --- a/application/argumentparser.cpp +++ b/application/argumentparser.cpp @@ -67,35 +67,6 @@ Argument::Argument(const char *name, const char *abbreviation, const char *descr Argument::~Argument() {} -///*! -// * \brief Checks whether the argument is ambigious. -// * \returns Returns zero if the argument is not ambigious. Returns 1 if the name -// * is ambiguous and 2 if the abbreviation is ambiguous. -// */ -//unsigned char Argument::isAmbiguous(const ArgumentParser &parser) const -//{ -// function check; -// check = [&check] (const ArgumentVector &args, const Argument *toCheck) -> unsigned char { -// for(const auto *arg : args) { -// if(arg != toCheck) { -// if(!toCheck->name().empty() && arg->name() == toCheck->name()) { -// return 1; -// } else if(!toCheck->abbreviation().empty() && arg->abbreviation() == toCheck->abbreviation()) { -// return 2; -// } -// } -// if(auto res = check(arg->parents(), toCheck)) { -// return res; -// } -// } -// return 0; -// }; -// if(auto res = check(parser.mainArguments(), this)) { -// return res; -// } -// return check(parents(), this); -//} - /*! * \brief Appends the name, the abbreviation and the description of the Argument to the give ostream. */ @@ -142,7 +113,7 @@ void Argument::printInfo(ostream &os, unsigned char indentionLevel) const os << endl << "Usage: " << example(); } os << endl; - for(const auto *arg : secondaryArguments()) { + for(const auto *arg : subArguments()) { arg->printInfo(os, indentionLevel + 1); } } @@ -176,17 +147,17 @@ Argument *firstPresentUncombinableArg(const ArgumentVector &args, const Argument * \sa addSecondaryArgument() * \sa hasSecondaryArguments() */ -void Argument::setSecondaryArguments(const ArgumentInitializerList &secondaryArguments) +void Argument::setSubArguments(const ArgumentInitializerList &secondaryArguments) { // remove this argument from the parents list of the previous secondary arguments - for(Argument *arg : m_secondaryArgs) { + for(Argument *arg : m_subArgs) { arg->m_parents.erase(remove(arg->m_parents.begin(), arg->m_parents.end(), this), arg->m_parents.end()); } // assign secondary arguments - m_secondaryArgs.assign(secondaryArguments); + m_subArgs.assign(secondaryArguments); // add this argument to the parents list of the assigned secondary arguments // and set the parser - for(Argument *arg : m_secondaryArgs) { + for(Argument *arg : m_subArgs) { if(find(arg->m_parents.cbegin(), arg->m_parents.cend(), this) == arg->m_parents.cend()) { arg->m_parents.push_back(this); } @@ -200,10 +171,10 @@ void Argument::setSecondaryArguments(const ArgumentInitializerList &secondaryArg * \sa setSecondaryArguments() * \sa hasSecondaryArguments() */ -void Argument::addSecondaryArgument(Argument *arg) +void Argument::addSubArgument(Argument *arg) { - if(find(m_secondaryArgs.cbegin(), m_secondaryArgs.cend(), arg) == m_secondaryArgs.cend()) { - m_secondaryArgs.push_back(arg); + if(find(m_subArgs.cbegin(), m_subArgs.cend(), arg) == m_subArgs.cend()) { + m_subArgs.push_back(arg); if(find(arg->m_parents.cbegin(), arg->m_parents.cend(), this) == arg->m_parents.cend()) { arg->m_parents.push_back(this); } @@ -253,7 +224,7 @@ Argument *Argument::conflictsWithArgument() const { if(!isCombinable() && isPresent()) { for(Argument *parent : m_parents) { - for(Argument *sibling : parent->secondaryArguments()) { + for(Argument *sibling : parent->subArguments()) { if(sibling != this && sibling->isPresent() && !sibling->isCombinable()) { return sibling; } @@ -360,7 +331,7 @@ Argument *ArgumentParser::findArg(const ArgumentVector &arguments, const Argumen for(Argument *arg : arguments) { if(predicate(arg)) { return arg; // argument matches - } else if(Argument *subarg = findArg(arg->secondaryArguments(), predicate)) { + } else if(Argument *subarg = findArg(arg->subArguments(), predicate)) { return subarg; // a secondary argument matches } } @@ -418,7 +389,7 @@ void ArgumentParser::verifySetup() const names.push_back(arg->name()); } verifiedArgs.push_back(arg); - checkArguments(arg->secondaryArguments()); + checkArguments(arg->subArguments()); } }; checkArguments(m_mainArgs); @@ -456,7 +427,7 @@ void ArgumentParser::parseArgs(int argc, char *argv[]) foreachArg = [&foreachArg] (Argument *parent, const ArgumentVector &args, const function &proc) { for(Argument *arg : args) { proc(parent, arg); - foreachArg(arg, arg->secondaryArguments(), proc); + foreachArg(arg, arg->subArguments(), proc); } }; // parse given arguments @@ -591,7 +562,7 @@ void ArgumentParser::parseArgs(int argc, char *argv[]) // the argument might be flagged as present if its a default argument if(arg->isDefault() && (arg->isMainArgument() || (parent && parent->isPresent()))) { arg->m_present = true; - if(firstPresentUncombinableArg(arg->isMainArgument() ? m_mainArgs : parent->secondaryArguments(), arg)) { + if(firstPresentUncombinableArg(arg->isMainArgument() ? m_mainArgs : parent->subArguments(), arg)) { arg->m_present = false; } } @@ -645,7 +616,8 @@ void ArgumentParser::parseArgs(int argc, char *argv[]) // only invoke if its a main argument or the parent is present if(arg->isPresent()) { if(arg->isDefault() && !arg->values().size()) { - arg->m_callbackFunction(arg->defaultValues()); + vector defaultValues(arg->defaultValues().cbegin(), arg->defaultValues().cend()); + arg->m_callbackFunction(defaultValues); } else { arg->m_callbackFunction(arg->values()); } @@ -667,7 +639,7 @@ void ArgumentParser::parseArgs(int argc, char *argv[]) HelpArgument::HelpArgument(ArgumentParser &parser) : Argument("help", "h", "shows this information") { - setCallback([&parser] (const StringVector &) { + setCallback([&parser] (const std::vector &) { CMD_UTILS_START_CONSOLE; parser.printHelp(cout); }); diff --git a/application/argumentparser.h b/application/argumentparser.h index 88ac508..250e2b6 100644 --- a/application/argumentparser.h +++ b/application/argumentparser.h @@ -28,8 +28,6 @@ class ArgumentParser; typedef std::initializer_list ArgumentInitializerList; typedef std::vector ArgumentVector; -typedef std::vector StringVector; -typedef std::list StringList; typedef std::function ArgumentPredicate; Argument LIB_EXPORT *firstPresentUncombinableArg(const ArgumentVector &args, const Argument *except); @@ -39,7 +37,7 @@ class LIB_EXPORT Argument friend class ArgumentParser; public: - typedef std::function CallbackFunction; + typedef std::function &)> CallbackFunction; Argument(const char *name, const char *abbreviation = nullptr, const char *description = nullptr, const char *example = nullptr); ~Argument(); @@ -48,25 +46,23 @@ public: void setName(const char *name); const char *abbreviation() const; void setAbbreviation(const char *abbreviation); - //unsigned char isAmbiguous(const ArgumentParser &parser) const; const char *description() const; void setDescription(const char *description); const char *example() const; void setExample(const char *example); - const StringVector &values() const; - const std::string &value(StringVector::size_type index) const; - StringVector::size_type valueCount() const; + const std::vector &values() const; + const std::string &value(std::size_t index) const; + std::size_t valueCount() const; int requiredValueCount() const; void setRequiredValueCount(int requiredValueCount); - const StringList &valueNames() const; - void setValueNames(std::initializer_list valueNames); + const std::list &valueNames() const; + void setValueNames(std::initializer_list valueNames); void appendValueName(const char *valueName); - void appendValueName(const std::string &valueName); bool allRequiredValuesPresent() const; bool isDefault() const; void setDefault(bool value); - const StringVector &defaultValues() const; - void setDefaultValues(const StringVector &defaultValues); + const std::list &defaultValues() const; + void setDefaultValues(const std::list &defaultValues); bool isPresent() const; bool isRequired() const; void setRequired(bool value); @@ -78,10 +74,10 @@ public: void setDenotesOperation(bool denotesOperation); void setCallback(CallbackFunction callback); void printInfo(std::ostream &os, unsigned char indentionLevel = 0) const; - const ArgumentVector &secondaryArguments() const; - void setSecondaryArguments(const ArgumentInitializerList &secondaryArguments); - void addSecondaryArgument(Argument *arg); - bool hasSecondaryArguments() const; + const ArgumentVector &subArguments() const; + void setSubArguments(const ArgumentInitializerList &subArguments); + void addSubArgument(Argument *arg); + bool hasSubArguments() const; const ArgumentVector parents() const; bool isMainArgument() const; std::string parentNames() const; @@ -98,12 +94,12 @@ private: bool m_implicit; bool m_denotesOperation; int m_requiredValueCount; - StringList m_valueNames; + std::list m_valueNames; bool m_default; - StringVector m_defaultValues; + std::list m_defaultValues; bool m_present; - StringVector m_values; - ArgumentVector m_secondaryArgs; + std::vector m_values; + ArgumentVector m_subArgs; CallbackFunction m_callbackFunction; ArgumentVector m_parents; bool m_isMainArg; @@ -227,7 +223,7 @@ inline void Argument::setExample(const char *example) * * These values set by the parser when parsing the command line arguments. */ -inline const StringVector &Argument::values() const +inline const std::vector &Argument::values() const { return m_values; } @@ -237,7 +233,7 @@ inline const StringVector &Argument::values() const * * These values set by the parser when parsing the command line arguments. */ -inline const std::string &Argument::value(StringVector::size_type index) const +inline const std::string &Argument::value(std::size_t index) const { return m_values[index]; } @@ -246,7 +242,7 @@ inline const std::string &Argument::value(StringVector::size_type index) const * Returns the number of values which could be found when parsing * the command line arguments. */ -inline StringVector::size_type Argument::valueCount() const +inline std::size_t Argument::valueCount() const { return m_values.size(); } @@ -293,7 +289,7 @@ inline void Argument::setRequiredValueCount(int requiredValueCount) * \sa setValueNames() * \sa appendValueNames() */ -inline const StringList &Argument::valueNames() const +inline const std::list &Argument::valueNames() const { return m_valueNames; } @@ -311,7 +307,7 @@ inline const StringList &Argument::valueNames() const * \sa valueNames() * \sa requiredValueCount() */ -inline void Argument::setValueNames(std::initializer_list valueNames) +inline void Argument::setValueNames(std::initializer_list valueNames) { m_valueNames.assign(valueNames); } @@ -319,24 +315,14 @@ inline void Argument::setValueNames(std::initializer_list valueName /*! * \brief Appends a value name. The value names names will be shown * when printing information about the argument. + * \sa setValueNames() + * \sa valueNames() */ inline void Argument::appendValueName(const char *valueName) { m_valueNames.emplace_back(valueName); } -/*! - * \brief Appends a value name. The value names names will be shown - * when printing information about the argument. - * - * \sa setValueNames() - * \sa valueNames() - */ -inline void Argument::appendValueName(const std::string &valueName) -{ - m_valueNames.push_back(valueName); -} - /*! * \brief Returns an indication whether all required values are present. */ @@ -384,7 +370,7 @@ inline void Argument::setDefault(bool value) * \sa setDefault() * \sa setDefaultValues() */ -inline const StringVector &Argument::defaultValues() const +inline const std::list &Argument::defaultValues() const { return m_defaultValues; } @@ -399,7 +385,7 @@ inline const StringVector &Argument::defaultValues() const * \sa setDefault() * \sa defaultValues() */ -inline void Argument::setDefaultValues(const StringVector &defaultValues) +inline void Argument::setDefaultValues(const std::list &defaultValues) { m_defaultValues = defaultValues; } @@ -528,9 +514,9 @@ inline void Argument::setCallback(Argument::CallbackFunction callback) * \sa setSecondaryArguments() * \sa hasSecondaryArguments() */ -inline const ArgumentVector &Argument::secondaryArguments() const +inline const ArgumentVector &Argument::subArguments() const { - return m_secondaryArgs; + return m_subArgs; } /*! @@ -539,9 +525,9 @@ inline const ArgumentVector &Argument::secondaryArguments() const * \sa secondaryArguments() * \sa setSecondaryArguments() */ -inline bool Argument::hasSecondaryArguments() const +inline bool Argument::hasSubArguments() const { - return !m_secondaryArgs.empty(); + return !m_subArgs.empty(); } /*! diff --git a/tests/testutils.h b/tests/testutils.h index a26b819..fe2f0c4 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -18,7 +18,7 @@ public: #ifdef PLATFORM_UNIX std::string workingCopyPath(const std::string &name) const; #endif - const ApplicationUtilities::StringVector &units() const; + const std::vector &units() const; static const TestApplication *instance(); private: @@ -56,7 +56,7 @@ inline const TestApplication *TestApplication::instance() /*! * \brief Returns the specified test units. */ -inline const ApplicationUtilities::StringVector &TestApplication::units() const +inline const std::vector &TestApplication::units() const { return m_unitsArg.values(); }