parser.h
Go to the documentation of this file.
#ifndef WIBBLE_COMMANDLINE_PARSER_H
#define WIBBLE_COMMANDLINE_PARSER_H
#include <wibble/commandline/engine.h>
#include <iosfwd>
namespace wibble {
namespace commandline {
class Parser : public Engine
{
protected:
ArgList m_args;
MemoryManager m_manager;
public:
Parser(const std::string& name,
const std::string& usage = std::string(),
const std::string& description = std::string(),
const std::string& longDescription = std::string())
: Engine(&m_manager, name, usage, description, longDescription) {}
bool parse(int argc, const char* argv[])
{
m_args.clear();
for (int i = 1; i < argc; i++)
m_args.push_back(argv[i]);
parseList(m_args);
return false;
}
bool hasNext() const { return !m_args.empty(); }
std::string next()
{
if (m_args.empty())
return std::string();
std::string res(*m_args.begin());
m_args.erase(m_args.begin());
return res;
}
};
class StandardParser : public Parser
{
protected:
std::string m_version;
public:
StandardParser(const std::string& appname, const std::string& version) :
Parser(appname), m_version(version)
{
helpGroup = addGroup("Help options");
help = helpGroup->add<BoolOption>("help", 'h', "help", "",
"print commandline help and exit");
help->addAlias('?');
this->version = helpGroup->add<BoolOption>("version", 0, "version", "",
"print the program version and exit");
}
void outputHelp(std::ostream& out);
bool parse(int argc, const char* argv[]);
OptionGroup* helpGroup;
BoolOption* help;
BoolOption* version;
};
class StandardParserWithManpage : public StandardParser
{
protected:
int m_section;
std::string m_author;
public:
StandardParserWithManpage(
const std::string& appname,
const std::string& version,
int section,
const std::string& author) :
StandardParser(appname, version),
m_section(section), m_author(author)
{
manpage = helpGroup->add<StringOption>("manpage", 0, "manpage", "[hooks]",
"output the " + name() + " manpage and exit");
}
bool parse(int argc, const char* argv[]);
StringOption* manpage;
};
class StandardParserWithMandatoryCommand : public StandardParserWithManpage
{
public:
StandardParserWithMandatoryCommand(
const std::string& appname,
const std::string& version,
int section,
const std::string& author) :
StandardParserWithManpage(appname, version, section, author)
{
helpCommand = addEngine("help", "[command]", "print help information",
"With no arguments, print a summary of available commands. "
"If given a command name as argument, print detailed informations "
"about that command.");
}
bool parse(int argc, const char* argv[]);
Engine* helpCommand;
};
}
}
// vim:set ts=4 sw=4:
#endif
wibble::commandline::StandardParserWithManpage
Parser for commandline arguments, with builting help functions and manpage generation.
Definition: parser.h:87
wibble::commandline::Option::addAlias
void addAlias(char c)
Definition: options.h:113
engine.h
wibble::commandline::Parser
Generic parser for commandline arguments.
Definition: parser.h:13
wibble::commandline::StandardParser::helpGroup
OptionGroup * helpGroup
Definition: parser.h:78
wibble::commandline::Engine::longDescription
std::string longDescription
Definition: engine.h:224
wibble::commandline::StandardParserWithMandatoryCommand::helpCommand
Engine * helpCommand
Definition: parser.h:133
wibble::commandline::Parser::next
std::string next()
Definition: parser.h:44
wibble::commandline::StandardParserWithManpage::m_section
int m_section
Definition: parser.h:90
wibble::commandline::Parser::m_manager
MemoryManager m_manager
Definition: parser.h:18
wibble::commandline::StandardParser::version
BoolOption * version
Definition: parser.h:80
wibble::commandline::StandardParserWithMandatoryCommand
Parser for commandline arguments, with builting help functions and manpage generation, and requiring a mandatory command.
Definition: parser.h:115
wibble::commandline::Engine::add
void add(const std::string &alias, Engine *o)
Definition: engine.cpp:39
wibble::commandline::StandardParser
Parser for commandline arguments, with builting help functions.
Definition: parser.h:57
wibble::commandline::Engine::addGroup
OptionGroup * addGroup(const std::string &description)
Create an OptionGroup and add it to this engine.
Definition: engine.h:161
wibble::commandline::SingleOption
Definition: options.h:161
wibble::commandline::StandardParserWithManpage::StandardParserWithManpage
StandardParserWithManpage(const std::string &appname, const std::string &version, int section, const std::string &author)
Definition: parser.h:94
wibble::commandline::Parser::hasNext
bool hasNext() const
Definition: parser.h:42
wibble::commandline::Engine
Parse commandline options.
Definition: engine.h:38
wibble::commandline::Parser::parse
bool parse(int argc, const char *argv[])
Parse the commandline.
Definition: parser.h:33
wibble::commandline::Engine::name
const std::string & name() const
Definition: engine.h:107
wibble::commandline::StandardParser::StandardParser
StandardParser(const std::string &appname, const std::string &version)
Definition: parser.h:63
wibble::commandline::StandardParserWithManpage::m_author
std::string m_author
Definition: parser.h:91
wibble::commandline::BoolOption
Boolean option.
Definition: options.h:134
wibble::commandline::StandardParserWithMandatoryCommand::StandardParserWithMandatoryCommand
StandardParserWithMandatoryCommand(const std::string &appname, const std::string &version, int section, const std::string &author)
Definition: parser.h:118
wibble::commandline::MemoryManager
Keep track of various wibble::commandline components, and deallocate them at object destruction...
Definition: core.h:62
wibble::commandline::ArgList
Definition: core.h:29
wibble::commandline::Parser::m_args
ArgList m_args
Definition: parser.h:16
wibble::commandline::Parser::Parser
Parser(const std::string &name, const std::string &usage=std::string(), const std::string &description=std::string(), const std::string &longDescription=std::string())
Definition: parser.h:21
wibble::commandline::Engine::parseList
ArgList::iterator parseList(ArgList &list)
Parse the list of arguments, starting at the beginning and removing the arguments it successfully par...
Definition: engine.h:89
wibble::commandline::OptionGroup
Group related commandline options.
Definition: options.h:359
wibble::commandline::StandardParser::m_version
std::string m_version
Definition: parser.h:60
wibble::commandline::Engine::usage
std::string usage
Definition: engine.h:222
wibble::commandline::StandardParserWithManpage::manpage
StringOption * manpage
Definition: parser.h:108
wibble
Definition: amorph.h:17
wibble::commandline::Engine::description
std::string description
Definition: engine.h:223
wibble::commandline::Engine::addEngine
Engine * addEngine(const std::string &name, const std::string &usage=std::string(), const std::string &description=std::string(), const std::string &longDescription=std::string())
Create a Engine and add it to this engine as a command.
Definition: engine.h:182
wibble::commandline::StandardParser::help
BoolOption * help
Definition: parser.h:79