![]() |
Python shell help
Hi everyone,
For the fun of it, I am trying to write a shell program similar to Linux's bash or Windows' command. I'm writing the program on Windows, and this is what I have so far: :
import osSo far, it waits for the user's input by looping C:\>>. My next step is to actually make the shell respond to user commands. For example, if the user inputs put "Text here" the shell will interpret the statement as print "Text here". I think the best thing to use is a parser or string methods. The problem is that I haven't found many docs on either item, and am not sure how to implement this. Does anyone here have a tutorial or an example on how I would parse text so that python can interpret it and do what I want? (I also want to do things such as change directory with a command like dir <dir here> and so on.) Thanks |
I would read up on parsers or interpreters. Complexity is also a big part of it. You might think of having a tokenizer if you plan to have long or complex commands. Otherwise a simple <command attrib1 attrib2 attrib3> could be done with simple string parsing.
I'm not familiar with Python myself but read the string and seperate each word in an array. Item zero would be your command and the rest would be the attributes passed to it. From there you can use a series of if-statements to determine what functions to use. |
The str.split function is extremely useful. For example, "slice me up".split() produces the list ['slice', 'me', 'up'].
|
.split() may work, but how would I use that to determine if the user wrote put or not?
|
This might help. You simply use an if-statement on your array.
http://www.ibiblio.org/g2swap/byteof...statement.html |
Quote:
I'm sure that wasn't actually your question. You should probably specify what you mean by that. Are you talking about case-sensitive circumstances, or situations where there may be other garbage tagged along with it? Or are you talking about how you could elegantly link each command to a different function call, without repetitive code? I'd look into the idea of using a dictionary to map each command to a function call: :
def output (*args): |
Quote:
My advice is to look into a parser generator like PyParsing. You could use this to define a simple grammar: :
Aha, you might very well say, but why does it say OneOrMore(string + Empty()), rather than OneOrMore(string)? Unfortunately, I forget the exact reason, as I've lost the email where I quizzed the creator of PyParsing about it. However, IIRC it has to do with conflicts between PyParsing's whitespace handling and any sufficiently broad CharsNotIn - the Empty() object ensures that the grammar is parsed unambiguously. Anyway, once you have your grammar, you can define some commands to go with it. In Unix, there are functions to resolve the command PATH, but in Windows those functions appear to be absent, so I think we have to create our own. If anyone finds a better solution, please tell me: :
The exec_command function uses os.spawnv to execute an external command with a set of arguments. Of course, this function could just as easily execute internal functions as well, or indeed do whatever you wish. The next step is to associate exec_command with the command grammar object: :
:
|
| All times are GMT -5. The time now is 12:40 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC