0

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?

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Josh Lee, yes it does have some nice enhancements. I'll edit my answer to include cmd2 as well.
Brilliant, thanks very much for putting me on the right track. Will try it out tonight!
0

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:

  • IPython: Pretty solid shell. It's used by Sage and a bunch of other big projects.
  • Python's native cmd class: self explanatory. Not very feature rich, but it's the Python shell.

8 Comments

You did? Did it also use vt100 control chars?
Go easy on me. I coded the features that I actually used, which was enough of an implementation for me.
I only ask because I'm usually coding on windoze boxes, where the shell is lousy. Since I can't find a decent replacement that is more of a "first class citizen" than interix or cygwin, I've decided to write a python one. With a little help from termEmulator I can get away from the terrible terminal too.
@Spencer: Have you tried the console application recommended by IPython? ipython.org/ipython-doc/stable/install/install.html and search for "console" It's not perfect, but you might find it useful.
@JS. yep, and it's fantastic at what it does. Which doesn't fit what I want, namely, a bash clone with features stolen from fish and msfconsole. Ipython is a python interpreter with shell convenience features. I'm coming from a different direction, and while I could probably get ipython to do what I want, I think it would be a fun project to work on.
|
0

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.

1 Comment

Windows based here. Thanks anyway for the response!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.