engine.h
Go to the documentation of this file.
#ifndef WIBBLE_COMMANDLINE_ENGINE_H
#define WIBBLE_COMMANDLINE_ENGINE_H
#include <wibble/commandline/options.h>
#include <string>
#include <vector>
#include <map>
#include <iosfwd>
namespace wibble {
namespace commandline {
#if 0
-- This help is left around to be reintegrated when I found something
appropriate. It documents the general behavior of functions in the form
ArgList::iterator parse(ArgList& list, ArgList::iterator begin);
#endif
class Engine : public Managed
{
MemoryManager* m_manager;
std::string m_name;
protected:
// Elements added to this engine
std::vector<OptionGroup*> m_groups;
std::vector<Option*> m_options;
std::vector<Engine*> m_commands;
// Parse tables for commandline options
std::map<char, Option*> m_short;
std::map<std::string, Option*> m_long;
std::map<std::string, Engine*> m_aliases;
// Command selected with the non-switch command, if any were found, else
// NULL
Engine* m_found_command;
void addWithoutAna(Option* o);
void addWithoutAna(const std::vector<Option*>& o);
void add(const std::string& alias, Engine* o);
// Rebuild the parse tables
void rebuild();
std::pair<ArgList::iterator, bool> parseFirstIfKnown(ArgList& list, ArgList::iterator begin);
#if 0
ArgList::iterator parseConsecutiveSwitches(ArgList& list, ArgList::iterator begin);
#endif
ArgList::iterator parseKnownSwitches(ArgList& list, ArgList::iterator begin);
ArgList::iterator parseList(ArgList& list) { return parse(list, list.begin()); }
ArgList::iterator parse(ArgList& list, ArgList::iterator begin);
Engine(MemoryManager* mman = 0, const std::string& name = std::string(),
const std::string& usage = std::string(),
const std::string& description = std::string(),
const std::string& longDescription = std::string())
: m_manager(mman), m_name(name), m_found_command(0), primaryAlias(name),
usage(usage), description(description), longDescription(longDescription), hidden(false),
no_switches_after_first_arg(false) {}
public:
const std::string& name() const { return m_name; }
Option* add(Option* o);
OptionGroup* add(OptionGroup* group);
Engine* add(Engine* o);
template<typename T>
T* create(const std::string& name,
char shortName,
const std::string& longName,
const std::string& usage = std::string(),
const std::string& description = std::string())
{
T* item = new T(name, shortName, longName, usage, description);
if (m_manager) m_manager->add(item);
return item;
}
template<typename T>
T* add(const std::string& name,
char shortName,
const std::string& longName,
const std::string& usage = std::string(),
const std::string& description = std::string())
{
T* res = create<T>(name, shortName, longName, usage, description);
add(res);
return res;
}
OptionGroup* createGroup(const std::string& description)
{
OptionGroup* g = new OptionGroup(m_manager, description);
if (m_manager) m_manager->add(g);
return g;
}
OptionGroup* addGroup(const std::string& description)
{
return add(createGroup(description));
}
Engine* createEngine(const std::string& name,
const std::string& usage = std::string(),
const std::string& description = std::string(),
const std::string& longDescription = std::string())
{
Engine* item = new Engine(m_manager, name, usage, description, longDescription);
if (m_manager) m_manager->add(item);
return item;
}
Engine* addEngine(const std::string& name,
const std::string& usage = std::string(),
const std::string& description = std::string(),
const std::string& longDescription = std::string())
{
return add(createEngine(name, usage, description, longDescription));
}
const std::vector<OptionGroup*>& groups() const { return m_groups; }
const std::vector<Option*>& options() const { return m_options; }
const std::vector<Engine*>& commands() const { return m_commands; }
Engine* command(const std::string& name) const
{
std::map<std::string, Engine*>::const_iterator i = m_aliases.find(name);
if (i == m_aliases.end())
return 0;
else
return i->second;
}
bool hasOptions() const { return !m_groups.empty() || !m_options.empty(); }
Engine* foundCommand() const { return m_found_command; }
void dump(std::ostream& out, const std::string& prefix = std::string());
std::string primaryAlias;
std::vector<std::string> aliases;
std::string usage;
std::string description;
std::string longDescription;
std::string examples;
// Set to true if the engine should not be documented
bool hidden;
// Set to true if no switches should be parsed after the first
// non-switch argument, and they should be just left in the argument
// list
bool no_switches_after_first_arg;
friend class Parser;
};
}
}
// vim:set ts=4 sw=4:
#endif
wibble::commandline::Engine::command
Engine * command(const std::string &name) const
Definition: engine.h:199
wibble::commandline::Engine::hasOptions
bool hasOptions() const
Returns true if this Engine has options to parse.
Definition: engine.h:209
wibble::commandline::MemoryManager::add
T * add(T *item)
Definition: core.h:76
wibble::iterator
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
wibble::commandline::Engine::Engine
Engine(MemoryManager *mman=0, const std::string &name=std::string(), const std::string &usage=std::string(), const std::string &description=std::string(), const std::string &longDescription=std::string())
Definition: engine.h:98
wibble::commandline::Parser
Generic parser for commandline arguments.
Definition: parser.h:13
wibble::commandline::Engine::commands
const std::vector< Engine * > & commands() const
Get the Engines that have been added to this engine.
Definition: engine.h:197
wibble::commandline::Engine::foundCommand
Engine * foundCommand() const
Return the command that has been found in the commandline, or NULL if none have been found...
Definition: engine.h:215
wibble::commandline::Engine::groups
const std::vector< OptionGroup * > & groups() const
Get the OptionGroups that have been added to this engine.
Definition: engine.h:191
wibble::commandline::Engine::longDescription
std::string longDescription
Definition: engine.h:224
wibble::commandline::Engine::createGroup
OptionGroup * createGroup(const std::string &description)
Create an OptionGroup.
Definition: engine.h:151
wibble::commandline::Engine::hidden
bool hidden
Definition: engine.h:228
wibble::commandline::Engine::create
T * create(const std::string &name, char shortName, const std::string &longName, const std::string &usage=std::string(), const std::string &description=std::string())
Create an option.
Definition: engine.h:122
wibble::commandline::Engine::examples
std::string examples
Definition: engine.h:225
wibble::commandline::Engine::m_found_command
Engine * m_found_command
Definition: engine.h:56
wibble::commandline::Engine::m_aliases
std::map< std::string, Engine * > m_aliases
Definition: engine.h:52
wibble::list::begin
ListIterator< List > begin(List l)
Definition: list.h:420
wibble::commandline::Engine::m_commands
std::vector< Engine * > m_commands
Definition: engine.h:47
wibble::commandline::Engine::add
void add(const std::string &alias, Engine *o)
Definition: engine.cpp:39
wibble::commandline::Engine::add
T * add(const std::string &name, char shortName, const std::string &longName, const std::string &usage=std::string(), const std::string &description=std::string())
Create an option and add to this engine.
Definition: engine.h:137
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::Engine::m_options
std::vector< Option * > m_options
Definition: engine.h:46
wibble::commandline::Engine
Parse commandline options.
Definition: engine.h:38
wibble::commandline::Engine::parseKnownSwitches
ArgList::iterator parseKnownSwitches(ArgList &list, ArgList::iterator begin)
Parse all known Options and leave the rest in list.
Definition: engine.cpp:130
wibble::commandline::Option
Interface for a parser for one commandline option.
Definition: options.h:55
wibble::commandline::Engine::name
const std::string & name() const
Definition: engine.h:107
wibble::commandline::Engine::parseFirstIfKnown
std::pair< ArgList::iterator, bool > parseFirstIfKnown(ArgList &list, ArgList::iterator begin)
Handle the commandline switch at &#39;begin&#39;.
Definition: engine.cpp:74
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::Engine::aliases
std::vector< std::string > aliases
Definition: engine.h:221
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::Engine::parse
ArgList::iterator parse(ArgList &list, ArgList::iterator begin)
Parse all the switches in list, leaving only the non-switch arguments or the arguments following "--"...
Definition: engine.cpp:250
wibble::commandline::OptionGroup
Group related commandline options.
Definition: options.h:359
wibble::commandline::Engine::no_switches_after_first_arg
bool no_switches_after_first_arg
Definition: engine.h:233
wibble::commandline::Engine::usage
std::string usage
Definition: engine.h:222
wibble::commandline::Engine::primaryAlias
std::string primaryAlias
Definition: engine.h:220
wibble::commandline::Engine::createEngine
Engine * createEngine(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.
Definition: engine.h:169
wibble
Definition: amorph.h:17
wibble::commandline::Engine::m_short
std::map< char, Option * > m_short
Definition: engine.h:50
wibble::commandline::Engine::description
std::string description
Definition: engine.h:223
wibble::commandline::Engine::m_long
std::map< std::string, Option * > m_long
Definition: engine.h:51
wibble::commandline::Managed
Definition: core.h:51
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
options.h
wibble::commandline::Engine::addWithoutAna
void addWithoutAna(Option *o)
Definition: engine.cpp:9
wibble::commandline::Engine::dump
void dump(std::ostream &out, const std::string &prefix=std::string())
Definition: engine.cpp:315
wibble::commandline::Engine::rebuild
void rebuild()
Definition: engine.cpp:48
wibble::commandline::Engine::m_groups
std::vector< OptionGroup * > m_groups
Definition: engine.h:45
wibble::commandline::Engine::options
const std::vector< Option * > & options() const
Get the Options that have been added to this engine.
Definition: engine.h:194