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