added example

This commit is contained in:
Martchus 2015-10-06 22:27:16 +02:00
parent 9f4d1daacf
commit 6e1f2d4d56
2 changed files with 40 additions and 5 deletions

View File

@ -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);
}
}

View File

@ -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.
*