Reputation: 31
I am coding a data entry system in Python, in which the user should also be able to submit 'commands', exactly like the Python shell works, or for example SAS or R interfaces. I would like to know how to code a shell. For examnple, it should provide a prompt (which cannot be deleted by the user, e.g. the >>> prompt in Python) and receive input from the user. Furthermore, once an entry is submitted, the user cannot go back, like in the DOS prompt, where you cannot go up a line, so to speak.
Can anybody help with this?
Upvotes: 0
Views: 304
Reputation: 13350
The cmd.Cmd class is something that would help you build a shell-like application. Likewise, cmd2 is a nice upgrade from the above module. With these you can build a shell application that has command history, help menus, and smart command parsing. Don't build a REPL from scratch because these modules will probably suffice for your needs.
Upvotes: 5
Reputation: 8895
You might want to look into the readline library (gets you fancy features like completion and history as well). Sadly, it's UNIX only, like it's underlying C library.
Upvotes: 0
Reputation: 298196
I coded a whole sh
implementation in Python a while back, but sadly I don't have the source.
Your basic code would be this:
while True:
command = raw_input('>>> ')
print 'You entered "{command}".'.format(command = command)
But if you need something more complicated than a simple bash
-like shell, there are a few projects that might suit your needs:
cmd
class: self explanatory. Not very feature rich, but it's the Python shell.Upvotes: 0