Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python command line interaction library?

Tags:

python

I want to develop a small Python app that interacts with the user via the console/command line. Are there any good libraries I can use to get user input and display the output in a nice-looking way? I tried searching but most of what I found was command-line argument processing, didn't see anything about user interaction in the shell.

It should be crossplatform (Windows and Linux)

like image 810
rdodev Avatar asked Oct 06 '12 09:10

rdodev


People also ask

What is CLI PY?

The cli package is a framework for making simple, correct command line applications in Python. With cli, you can quickly add standard command line parsing; logging; unit and functional testing; and profiling to your CLI apps.

How do I run a Python module from the command line?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

What are the applications of CLI?

Command Line Applications (aka CLI applications or simply CLIs - for Command Line Interface) are programs that you interact with entirely through your terminal and shell. They have no graphics or visual interface beyond what you see in your terminal after you run the program.


1 Answers

A really excellent library is cmd which is part of the python standard library. It is cross platform Windows, Linux, Mac. You just have to implement one class and it offers so many great features:

  1. provides list of supported commands(they end up being implemented as methods)
  2. help command that can be called to explain one of your commands
  3. handles the whole entering a command, checking syntax, and calling your method command loop cycle.
  4. users can save the commands they have run in an interactive session and run them as a script. check out the example below.

If you take the turtle shell example and save it as turtleshell.py and save the below turtle script file as circles.txt

circle 20
circle 50
circle 100
bye

then you could run the turtle script with the following command:

cat circles.txt | ./turtleshell.py

so in the simple example shown in the docs the developer has essentially made a simple mini-language that can be used as an easier interface to to the turtle module making it even easier to introduce programming to kids. The examples above have been taken from the python3 docs because they have included a detailed example in their docs which wasn't there in the 2.7 docs, but cmd is available and fully functional in python 2.3 and later.

like image 58
Marwan Alsabbagh Avatar answered Nov 04 '22 23:11

Marwan Alsabbagh