![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jul 2004
Location: Location
Posts: 138
Rep Power: 5
![]() |
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 os
os.chdir('/') #Change the directory to Windows root
while True:
directory = os.path.abspath(os.curdir)
directory += '>>'
prompt = raw_input(directory) #Print the command line prompt as C:\>>So 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 |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
The str.split function is extremely useful. For example, "slice me up".split() produces the list ['slice', 'me', 'up'].
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jul 2004
Location: Location
Posts: 138
Rep Power: 5
![]() |
.split() may work, but how would I use that to determine if the user wrote put or not?
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
This might help. You simply use an if-statement on your array.
http://www.ibiblio.org/g2swap/byteof...statement.html |
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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):
print args[0][0]
def list_of_files (*args):
pass
def run_file (*args):
pass
# parse the input in to a command and list of parameters
command = 'put'
params = ['"Text Here"']
# turn the command into a function pointer
commands = {"put":output, "dir":list_of_files, "start":run_file}
function = commands[command]
# call the function pointer with the parsed parameters
function (params) |
|
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
My advice is to look into a parser generator like PyParsing. You could use this to define a simple grammar: python Syntax (Toggle Plain Text)
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: python Syntax (Toggle Plain Text)
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: python Syntax (Toggle Plain Text)
python Syntax (Toggle Plain Text)
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [tutorial] Python for programming beginners | coldDeath | Python | 30 | Dec 14th, 2005 11:35 AM |
| Convert Python script to C++ code | clanotheduck | Python | 17 | Sep 25th, 2005 8:55 AM |
| Advanced Python Tricks | Arevos | Python | 19 | Sep 24th, 2005 7:39 AM |
| Hybrid python shell | Arevos | Existing Project Development | 0 | Sep 22nd, 2005 4:37 PM |
| Python - A Programmers Introduction | coldDeath | Python | 17 | Aug 19th, 2005 12:41 PM |