1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <wibble/commandline/parser.h>
#include <wibble/commandline/doc.h>
#include <iostream>
using namespace std;
namespace wibble {
namespace commandline {
void StandardParser::outputHelp(std::ostream& out)
{
commandline::Help help(name(), m_version);
commandline::Engine* e = foundCommand();
if (e)
// Help on a specific command
help.outputHelp(out, *e);
else
// General help
help.outputHelp(out, *this);
}
bool StandardParser::parse(int argc, const char* argv[])
{
if (Parser::parse(argc, argv))
return true;
if (help->boolValue())
{
// Provide help as requested
outputHelp(cout);
return true;
}
if (version->boolValue())
{
// Print the program version
commandline::Help help(name(), m_version);
help.outputVersion(cout);
return true;
}
return false;
}
bool StandardParserWithManpage::parse(int argc, const char* argv[])
{
if (StandardParser::parse(argc, argv))
return true;
if (manpage->isSet())
{
// Output the manpage
commandline::Manpage man(name(), m_version, m_section, m_author);
string hooks(manpage->value());
if (!hooks.empty())
man.readHooks(hooks);
man.output(cout, *this);
return true;
}
return false;
}
bool StandardParserWithMandatoryCommand::parse(int argc, const char* argv[])
{
if (StandardParserWithManpage::parse(argc, argv))
return true;
if (!foundCommand())
{
commandline::Help help(name(), m_version);
help.outputHelp(cout, *this);
return true;
}
if (foundCommand() == helpCommand)
{
commandline::Help help(name(), m_version);
if (hasNext())
{
// Help on a specific command
string command = next();
if (Engine* e = this->command(command))
help.outputHelp(cout, *e);
else
throw exception::BadOption("unknown command " + command + "; run '" + argv[0] + " help' "
"for a list of all the available commands");
} else {
// General help
help.outputHelp(cout, *this);
}
return true;
}
return false;
}
}
}
// vim:set ts=4 sw=4:
|