CommandParser.h
1 /***************************************************************************
2  *
3  * Copyright (C) 2002, 2006 by Willem van Straten
4  * Licensed under the Academic Free License version 2.1
5  *
6  ***************************************************************************/
7 #ifndef __CommandParser_h
8 #define __CommandParser_h
9 
10 #include "TextInterfaceParser.h"
11 #include "Error.h"
12 
13 #include <vector>
14 #include <string>
15 #include <iostream>
16 
17 class CommandParser : public Reference::Able {
18 
19  public:
20 
22  static bool debug;
23 
25  std::string prompt;
26 
28  std::ostream* out;
29 
31  CommandParser ();
32 
34  ~CommandParser ();
35 
37  void initialize_readline (const char*);
38 
40  std::string readline ();
41 
43  virtual std::string script (const std::string& filename);
44 
46  virtual std::string script (const std::vector<std::string>& commands);
47 
49  void standard_input (const std::string& prompt);
50 
52  std::string help (const std::string& command = "");
53 
55  std::string parse (const std::string& commandargs);
56 
58  std::string parse2 (const std::string& command, const std::string& args);
59 
61  std::string if_command (const std::string& condition_command);
62 
64  std::string while_command (const std::string& condition_command);
65 
67  std::string loop (const std::string& indeces_command);
68 
70  virtual bool evaluate (const std::string& expression);
71 
73  virtual std::string empty ();
74 
76  bool quit = false;
77 
79  int verbose = 0;
80 
82  bool abort = false;
83 
85  class Method;
86 
87  protected:
88 
90  bool interactive = true;
91 
93  bool fault = false;
94 
96  std::string nested;
97 
99  template <class Parser>
100  void add_command (std::string (Parser::*method)(const std::string&),
101  const std::string& command,
102  const std::string& help,
103  const std::string& detailed_help = "",
104  char shortcut = 0);
105 
107  template <class Parser>
108  void add_command (std::string (Parser::*method)(const std::string&),
109  char shortcut,
110  const std::string& command,
111  const std::string& help,
112  const std::string& detailed_help = "")
113  { add_command (method, command, help, detailed_help, shortcut); }
114 
116  void import (CommandParser*);
117 
119  void import (CommandParser*,
120  const std::string& command,
121  const std::string& help,
122  char shortcut = 0);
123 
125  class Nested;
126 
128  void add_command (Method*);
129 
131  std::string get_command () const { return current_command; }
132 
134  std::string usage () { return help(current_command); }
135 
137  void conditional (const std::string& text,
138  std::string& condition,
139  std::string& command);
140 
141  virtual TextInterface::Parser* get_interface () { return 0; }
142 
143  private:
144 
146  std::vector<Method*> commands;
147 
149  std::string current_command;
150 
152  bool startCommand, endCommand;
153 
154  // readline interface
155  static char** completion (const char *text, int start, int end);
156 
157  // readline interface
158  static char* command_generator (const char* text, int state);
159 
161  std::string loop_command;
163  std::string loop_result;
165  void loop_parse ();
166 };
167 
170  public:
171  Method() {}
172  virtual ~Method () {}
173 
174  virtual std::string execute (const std::string& command) = 0;
175 
176  virtual std::string detail () const = 0;
177 
179  std::string command;
181  std::string help;
183  char shortcut;
184 };
185 
188  public:
189  Nested( CommandParser*,
190  const std::string& command,
191  const std::string& help,
192  char shortcut );
193  std::string execute (const std::string& command);
194  std::string detail () const;
195  protected:
197 };
198 
200 template <class Parser> class Command : public CommandParser::Method
201 {
202  friend class CommandParser;
203 
204  typedef std::string (Parser::*Method) (const std::string&);
205 
206  public:
207 
208  Command (Parser* _instance, Method _method, const std::string& _command,
209  const std::string& _help, const std::string& _detailed_help,
210  char _shortcut)
211  {
212  command = _command;
213  help = _help;
214  shortcut = _shortcut;
215 
216  detailed_help = _detailed_help;
217  instance = _instance;
218  method = _method;
219  }
220 
222  std::string execute (const std::string& args)
223  { return (instance->*method) (args); }
224 
225  std::string detail () const
226  { return detailed_help; }
227 
228  protected:
230  Method method;
231 
233  Parser* instance;
234 
236  std::string detailed_help;
237 };
238 
240 template<class P>
241 void CommandParser::add_command (std::string (P::*method) (const std::string&),
242  const std::string& cmd,
243  const std::string& help,
244  const std::string& detailed_help,
245  char shortcut)
246 {
247  if (debug)
248  std::cerr << "CommandParser::add_command \"" << cmd << "\"" << std::endl;
249 
250  P* instance = dynamic_cast<P*> (this);
251  if (!instance)
252  throw Error (InvalidState, "CommandParser::add_command",
253  "instance/method mis-match");
254 
255  if (debug)
256  std::cerr << "CommandParser::add_command new Command<P>" << std::endl;
257 
258  add_command (new Command<P>
259  (instance, method, cmd, help, detailed_help, shortcut));
260 }
261 
262 
263 #endif
void add_index(TextIndex *)
Add an index over which to loop.
Definition: TextLoop.C:18
char shortcut
The shortcut character corresponding to this method.
Definition: CommandParser.h:183
Pure virtual base class of the template class Command.
Definition: CommandParser.h:169
std::string command
The command string corresponding to this method.
Definition: CommandParser.h:179
void loop()
Execute the job for each index in the stack.
Definition: TextLoop.C:30
A convenient exception handling class.
Definition: Error.h:54
ErrorCode get_code() const
Get the error code.
Definition: Error.h:92
std::string detailed_help
The detailed help string for this method.
Definition: CommandParser.h:236
An array of Value interfaces.
Definition: TextInterfaceParser.h:35
std::string execute(const std::string &args)
Execute method.
Definition: CommandParser.h:222
Template class manages Reference::Able objects.
Definition: Reference.h:74
Parser * instance
Instance through which method is called.
Definition: CommandParser.h:233
Manages Reference::To references to the instance.
Definition: ReferenceAble.h:40
Nested CommandParser Method implementation.
Definition: CommandParser.h:187
std::string help
The help string for this method.
Definition: CommandParser.h:181
void set_container(TextInterface::Parser *)
Set the interface of the container to which the named indeces apply.
Definition: TextLoop.C:24
const std::string get_message() const
Get the error message.
Definition: Error.C:133
Functor< void() > job
Job defined by derived types.
Definition: TextLoop.h:41
Method method
Method of the sub-class to execute.
Definition: CommandParser.h:230
Loop through ranges of indeces ascertained by a TextInterface.
Definition: TextLoop.h:19
Parses and manages a named set of indeces.
Definition: TextIndex.h:17
Stores a pointer to a CommandParser sub-class and one of its methods.
Definition: CommandParser.h:200

Generated using doxygen 1.8.17