diff --git a/application/argumentparser.cpp b/application/argumentparser.cpp index d470235..b4dab88 100644 --- a/application/argumentparser.cpp +++ b/application/argumentparser.cpp @@ -71,9 +71,17 @@ Argument::Argument(const char *name, const char *abbreviation, const char *descr m_present(false), m_isMainArg(false) { - setName(name); - setAbbreviation(abbreviation); - setDescription(description); + if(name) { + setName(name); + } else { + setName(string()); + } + if(abbreviation) { + setAbbreviation(abbreviation); + } + if(description) { + setDescription(description); + } } /*! @@ -153,8 +161,12 @@ void Argument::printInfo(ostream &os, unsigned char indentionLevel) const for(unsigned char i = 0; i < indentionLevel; ++i) os << " "; os << "This argument is required."; } + if(!example().empty()) { + for(unsigned char i = 0; i < indentionLevel; ++i) os << " "; + os << endl << "Usage: " << example(); + } os << endl; - for(const Argument *arg : secondaryArguments()) { + for(const auto *arg : secondaryArguments()) { arg->printInfo(os, indentionLevel + 1); } } @@ -326,7 +338,7 @@ void ArgumentParser::printHelp(ostream &os) const } if(!m_mainArgs.empty()) { os << "Available arguments:\n"; - for(const Argument *arg : m_mainArgs) { + for(const auto *arg : m_mainArgs) { arg->printInfo(os); } } diff --git a/application/argumentparser.h b/application/argumentparser.h index d0673a7..cd7282d 100644 --- a/application/argumentparser.h +++ b/application/argumentparser.h @@ -52,6 +52,8 @@ public: //unsigned char isAmbiguous(const ArgumentParser &parser) const; const std::string &description() const; void setDescription(const std::string &description); + const std::string &example() const; + void setExample(const std::string &example); const StringVector &values() const; const std::string &value(StringVector::size_type index) const; StringVector::size_type valueCount() const; @@ -91,6 +93,7 @@ private: std::string m_name; std::string m_abbreviation; std::string m_description; + std::string m_example; bool m_required; bool m_combinable; bool m_implicit; @@ -182,6 +185,26 @@ inline void Argument::setDescription(const std::string &description) m_description = description; } +/*! + * \brief Returns the usage example of the argument. + * + * The parser uses the description when printing help information. + */ +inline const std::string &Argument::example() const +{ + return m_example; +} + +/*! + * \brief Sets the a usage example for the argument. + * + * The parser uses the description when printing help information. + */ +inline void Argument::setExample(const std::string &example) +{ + m_example = example; +} + /*! * \brief Returns the additional values for the argument. *