Monday, July 27, 2009

C++ Command Interpreter?

I am wondering if this abstract base class (and supporting definitions) is sufficient enough to create a good command line interpreter:





union Result{


unsigned long lresult;


unsigned short wresult;


};





class Command{


public:


virtual Result Exec(Result [])=0;


protected:


virtual char *Syntax()=0;


virtual char *Name()=0;


private: //To Command only


Result Run(Result []);


};





Exec will run the command, which should be derived from Command.





Syntax gives a string representation of a syntax (like "llw" for a lresult, lresult, and a wresult in the array).





Name is for Run. It outputs the name by which this command should be called.





Run, only available by Command, will actually run a line of code. It is respnsible for the function of the interpreter itself, making it the "true interpreter".





Is this sufficient?

C++ Command Interpreter?
It appears complete enough. However, I would add a few methods like:


1. PreCommandHook: Allows redirection/forwarding of commands. If user wants to execute something else... Aliases could be implemented this way.


2. PostCommandHook: To allow better control for someone wanting to extend your interpreter (like updating the prompt based on result for commands like 'chdir')


3. InterruptHandler: To handle control-c by user to stop a long running command


4. ReadConfiguration: If you want to have an initialization file...
Reply:Seems like a pretty good start to me.





What about parameters that are passed? How about the environment the command runs in?


No comments:

Post a Comment