wibble  1.1
engine.h
Go to the documentation of this file.
1 #ifndef WIBBLE_COMMANDLINE_ENGINE_H
2 #define WIBBLE_COMMANDLINE_ENGINE_H
3 
5 #include <string>
6 #include <vector>
7 #include <map>
8 #include <iosfwd>
9 
10 namespace wibble {
11 namespace commandline {
12 
13 #if 0
14  -- This help is left around to be reintegrated when I found something
15  appropriate. It documents the general behavior of functions in the form
16  ArgList::iterator parse(ArgList& list, ArgList::iterator begin);
17 
27 #endif
28 
38 class Engine : public Managed
39 {
40  MemoryManager* m_manager;
41  std::string m_name;
42 
43 protected:
44  // Elements added to this engine
45  std::vector<OptionGroup*> m_groups;
46  std::vector<Option*> m_options;
47  std::vector<Engine*> m_commands;
48 
49  // Parse tables for commandline options
50  std::map<char, Option*> m_short;
51  std::map<std::string, Option*> m_long;
52  std::map<std::string, Engine*> m_aliases;
53 
54  // Command selected with the non-switch command, if any were found, else
55  // NULL
57 
58  void addWithoutAna(Option* o);
59  void addWithoutAna(const std::vector<Option*>& o);
60  void add(const std::string& alias, Engine* o);
61 
62  // Rebuild the parse tables
63  void rebuild();
64 
72  std::pair<ArgList::iterator, bool> parseFirstIfKnown(ArgList& list, ArgList::iterator begin);
73 
74 #if 0
75  ArgList::iterator parseConsecutiveSwitches(ArgList& list, ArgList::iterator begin);
77 #endif
78 
81 
89  ArgList::iterator parseList(ArgList& list) { return parse(list, list.begin()); }
90 
96 
97 
98  Engine(MemoryManager* mman = 0, const std::string& name = std::string(),
99  const std::string& usage = std::string(),
100  const std::string& description = std::string(),
101  const std::string& longDescription = std::string())
102  : m_manager(mman), m_name(name), m_found_command(0), primaryAlias(name),
105 
106 public:
107  const std::string& name() const { return m_name; }
108 
110  Option* add(Option* o);
111 
113  OptionGroup* add(OptionGroup* group);
114 
116  Engine* add(Engine* o);
117 
121  template<typename T>
122  T* create(const std::string& name,
123  char shortName,
124  const std::string& longName,
125  const std::string& usage = std::string(),
126  const std::string& description = std::string())
127  {
128  T* item = new T(name, shortName, longName, usage, description);
129  if (m_manager) m_manager->add(item);
130  return item;
131  }
132 
136  template<typename T>
137  T* add(const std::string& name,
138  char shortName,
139  const std::string& longName,
140  const std::string& usage = std::string(),
141  const std::string& description = std::string())
142  {
143  T* res = create<T>(name, shortName, longName, usage, description);
144  add(res);
145  return res;
146  }
147 
151  OptionGroup* createGroup(const std::string& description)
152  {
153  OptionGroup* g = new OptionGroup(m_manager, description);
154  if (m_manager) m_manager->add(g);
155  return g;
156  }
157 
161  OptionGroup* addGroup(const std::string& description)
162  {
163  return add(createGroup(description));
164  }
165 
169  Engine* createEngine(const std::string& name,
170  const std::string& usage = std::string(),
171  const std::string& description = std::string(),
172  const std::string& longDescription = std::string())
173  {
174  Engine* item = new Engine(m_manager, name, usage, description, longDescription);
175  if (m_manager) m_manager->add(item);
176  return item;
177  }
178 
182  Engine* addEngine(const std::string& name,
183  const std::string& usage = std::string(),
184  const std::string& description = std::string(),
185  const std::string& longDescription = std::string())
186  {
188  }
189 
191  const std::vector<OptionGroup*>& groups() const { return m_groups; }
192 
194  const std::vector<Option*>& options() const { return m_options; }
195 
197  const std::vector<Engine*>& commands() const { return m_commands; }
198 
199  Engine* command(const std::string& name) const
200  {
201  std::map<std::string, Engine*>::const_iterator i = m_aliases.find(name);
202  if (i == m_aliases.end())
203  return 0;
204  else
205  return i->second;
206  }
207 
209  bool hasOptions() const { return !m_groups.empty() || !m_options.empty(); }
210 
215  Engine* foundCommand() const { return m_found_command; }
216 
217 
218  void dump(std::ostream& out, const std::string& prefix = std::string());
219 
220  std::string primaryAlias;
221  std::vector<std::string> aliases;
222  std::string usage;
223  std::string description;
224  std::string longDescription;
225  std::string examples;
226 
227  // Set to true if the engine should not be documented
228  bool hidden;
229 
230  // Set to true if no switches should be parsed after the first
231  // non-switch argument, and they should be just left in the argument
232  // list
234 
235 
236  friend class Parser;
237 };
238 
239 }
240 }
241 
242 // vim:set ts=4 sw=4:
243 #endif
Engine * command(const std::string &name) const
Definition: engine.h:199
bool hasOptions() const
Returns true if this Engine has options to parse.
Definition: engine.h:209
T * add(T *item)
Definition: core.h:76
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
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
Generic parser for commandline arguments.
Definition: parser.h:13
const std::vector< Engine * > & commands() const
Get the Engines that have been added to this engine.
Definition: engine.h:197
Engine * foundCommand() const
Return the command that has been found in the commandline, or NULL if none have been found...
Definition: engine.h:215
const std::vector< OptionGroup * > & groups() const
Get the OptionGroups that have been added to this engine.
Definition: engine.h:191
std::string longDescription
Definition: engine.h:224
OptionGroup * createGroup(const std::string &description)
Create an OptionGroup.
Definition: engine.h:151
bool hidden
Definition: engine.h:228
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
std::string examples
Definition: engine.h:225
Engine * m_found_command
Definition: engine.h:56
std::map< std::string, Engine * > m_aliases
Definition: engine.h:52
ListIterator< List > begin(List l)
Definition: list.h:420
std::vector< Engine * > m_commands
Definition: engine.h:47
void add(const std::string &alias, Engine *o)
Definition: engine.cpp:39
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
OptionGroup * addGroup(const std::string &description)
Create an OptionGroup and add it to this engine.
Definition: engine.h:161
std::vector< Option * > m_options
Definition: engine.h:46
Parse commandline options.
Definition: engine.h:38
ArgList::iterator parseKnownSwitches(ArgList &list, ArgList::iterator begin)
Parse all known Options and leave the rest in list.
Definition: engine.cpp:130
Interface for a parser for one commandline option.
Definition: options.h:55
const std::string & name() const
Definition: engine.h:107
std::pair< ArgList::iterator, bool > parseFirstIfKnown(ArgList &list, ArgList::iterator begin)
Handle the commandline switch at &#39;begin&#39;.
Definition: engine.cpp:74
Keep track of various wibble::commandline components, and deallocate them at object destruction...
Definition: core.h:62
Definition: core.h:29
std::vector< std::string > aliases
Definition: engine.h:221
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
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
Group related commandline options.
Definition: options.h:359
bool no_switches_after_first_arg
Definition: engine.h:233
std::string usage
Definition: engine.h:222
std::string primaryAlias
Definition: engine.h:220
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
Definition: amorph.h:17
std::map< char, Option * > m_short
Definition: engine.h:50
std::string description
Definition: engine.h:223
std::map< std::string, Option * > m_long
Definition: engine.h:51
Definition: core.h:51
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
void addWithoutAna(Option *o)
Definition: engine.cpp:9
void dump(std::ostream &out, const std::string &prefix=std::string())
Definition: engine.cpp:315
void rebuild()
Definition: engine.cpp:48
std::vector< OptionGroup * > m_groups
Definition: engine.h:45
const std::vector< Option * > & options() const
Get the Options that have been added to this engine.
Definition: engine.h:194