Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a shell in Python

Tags:

I've written a small console application that can perform certain tasks. The user interface is similar to things like version control systems or yum etc. So basically you can think of it as a domain specific language. Now I'd like to write a (bash like) shell that can execute and auto-complete this language and has a command history (so I do not have to load and save the quite large xml files on each command). In a nutshell I want something like ipython but not for executing python code but my own DSL.

Are there any libraries that help me doing this? I see that there is a readline and rlcompleter module in python but its documentation seems to indicate that this is only for use with the python shell itself, or did I miss something there?

like image 238
panzi Avatar asked Apr 07 '10 17:04

panzi


People also ask

How do you write shell code in Python?

To run the Python Shell, open the command prompt or power shell on Windows and terminal window on mac, write python and press enter. A Python Prompt comprising of three greater-than symbols >>> appears, as shown below. Now, you can enter a single statement and get the result.

Can we write shell script in Python?

Python allows you to execute shell commands, which you can use to start other programs or better manage shell scripts that you use for automation. Depending on our use case, we can use os. system() , subprocess.

What is a shell for Python?

The Python Shell is the interpreter that executes your Python programs, other pieces of Python code, or simple commands.

How do I start python in shell?

To start the Python shell, simply type python and hit Enter in the terminal: C:\Users\Suchandra Datta>python Python 3.8.


1 Answers

You should check out the cmd and cmd2 modules. I think they will do what you want. There was a PyCon talk about these.

like image 160
Brian Lyttle Avatar answered Oct 12 '22 14:10

Brian Lyttle