Pexpect might be useful for you: http://sourceforge.net/projects/pexpect/

Description from the webpage: "Pexpect is a Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. Pexpect is pure Python."

Answer from Filip Korling on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › good example of cli wrapper?
r/learnpython on Reddit: Good example of CLI wrapper?
September 12, 2014 -

I'd like to write a python app that wraps an existing command line interface.

Does anyone know of good example apps to look at?

The CLI is fairly minimal but I am not quite sure how to approach this as I've mainly used Python for data analysis.

Thanks.

Edit (full example, from comment below):

Yes, so the CLI is for interacting with specific database/files. Basically I just use it to grab data, then I use/edit/analyze the data, and sometimes want to write back to the DB.

Examples:

mycli pull mydatabase (grab the database as csv for local use)
mycli import new_data.csv (import new csv data into DB)

Right now I just do this with call commands in my Python scripts:

subprocess.call(['mycli', 'pull', 'mydatabase'])

I'd like to just make a simple Python Module w/ an API to do this instead.

from mycli import DbManager
db = DbManager()
db.pull('mydatabase')

I can write this code now in a not DRY way. Basically just making a function for each thing the CLI can do.

I'd like to see an example of a larger application, so I can just understand how to best organize something like this in case it gets larger.

🌐
PyPI
pypi.org › project › cli-wrapper
cli-wrapper · PyPI
Tool to represent CLI tools as native python objects
      » pip install cli-wrapper
    
Published: May 08, 2025
Version: 0.1.0
Discussions

select - How to wrap a CLI program in Python (keeping the interactivity)? - Stack Overflow
I'd like to write a wrapper for an interactive CLI Program (the Asterisk CLI). Basically, I need to keep the interaction with the CLI (including tab-completion) but I want to filter the output of More on stackoverflow.com
🌐 stackoverflow.com
subprocess - Help with wrapping a command line tool in Python - Stack Overflow
I'm really stuck with a problem I'm hoping someone can help me with. I'm trying to create a wrapper in Python3.1 for a command line program called spooky. I can successfully run this program on the More on stackoverflow.com
🌐 stackoverflow.com
Wrapping an interactive command line application in a Python script - Stack Overflow
I am interested in controlling an interactive CLI application from Python calls. I guess at the most basic level I need a Python script that will start a CLI application on the host operating system. More on stackoverflow.com
🌐 stackoverflow.com
Using Python, what's an effective way to wrap a command line utility? - Software Engineering Stack Exchange
I want to write a Python application that "wraps" a command line utility, passing through all parameters intact, except for the parameters that I choose to modify. I imagine there's an easy way to... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
🌐
GitHub
github.com › danbradham › scrim
GitHub - danbradham/scrim: Opaque Python CLI Wrapper · GitHub
That's why tools like virtualenvwrapper are written in shell scripting languages instead of pure python. Scrim provides opaque shell scripts that wrap your python cli, so you can continue writing the interfaces for your python programs in python.
Author: danbradham
🌐
Medium
medium.com › @shamir.stav_83310 › lets-create-a-cli-with-python-part-1-ae4fe9e0258b
Let’s Create A CLI With Python — Part 1 | by Stav Shamir | Medium
April 11, 2019 - In this post we will start building together a CLI program with python — by the end of the series you will be able to build an easy to use CLI wrapping a public API, install it in bash, and have auto-completion for it!
🌐
Readthedocs
nipype.readthedocs.io › en › 0.11.0 › devel › cmd_interface_devel.html
How to wrap a command line tool — nipy pipeline and interfaces package
The aim of this section is to describe how external programs and scripts can be wrapped for use in Nipype either as interactive interfaces or within the workflow/pipeline environment. Currently, there is support for command line executables/scripts and matlab scripts. One can also create pure Python interfaces.
Find elsewhere
🌐
Medium
medium.com › @shamir.stav_83310 › lets-create-a-cli-with-python-part-2-750f21f36b4a
Let’s Create A CLI With Python — Part 2 | by Stav Shamir | Medium
April 11, 2019 - Let’s Create A CLI With Python — Part 2 This part is mostly about how to use the requests library to wrap an API with python functions. In the last part we’ve created a skeleton for a CLI …
🌐
PyPI
pypi.org › project › exec-wrappers
exec-wrappers · PyPI
This will create in /tmp/conda_wrappers a wrapper for each executable found in ~/miniconda/envs/test/bin. So if you run the python wrapper:
🌐
The Hitchhiker's Guide to Python
docs.python-guide.org › scenarios › cli
Command-line Applications — The Hitchhiker's Guide to Python
Python Fire is a library for automatically generating command-line interfaces from absolutely any Python object. It can help debug Python code more easily from the command line, create CLI interfaces to existing code, allow you to interactively ...
🌐
Simon Willison
simonwillison.net › 2023 › Sep › 30 › cli-tools-python
Things I’ve learned about building CLI tools in Python
September 30, 2023 - I build a lot of command-line tools in Python. It’s become my favorite way of quickly turning a piece of code into something I can use myself and package up for other people to use too. My biggest CLI projects are sqlite-utils, LLM, shot-scraper and Datasette—but I have dozens of others and I build new ones at the rate of at least one a month. A fun recent example is blip-caption, a tiny CLI wrapper ...
🌐
GitHub
github.com › wh00hw › pyFlipper
GitHub - wh00hw/pyFlipper: Flipper Zero Python CLI Wrapper · GitHub
This release brings the wrapper in line with the current Flipper Zero CLI (validated on real hardware, firmware unlshd-089e):
Author: wh00hw
🌐
PyPI
pypi.org › project › pycliwrapper
pycliwrapper · PyPI
May 26, 2023 - Wrap any CLI tool to Python like syntax · These details have been verified by PyPI · jordeu · These details have not been verified by PyPI · License: Apache Software License (Apache License 2.0) Author: Jordi Deu-Pons · Tags cli , wrapper · Development Status ·
      » pip install pycliwrapper
    
Published: May 26, 2023
Version: 1.1
🌐
GitHub
github.com › senko › python-cli-runner
GitHub - senko/python-cli-runner: Command line runner wrapper for Python · GitHub
Command line runner wrapper for Python. Contribute to senko/python-cli-runner development by creating an account on GitHub.
Author: senko
🌐
Python
svn.python.org › projects › sandbox › branches › setuptools-0.6 › setuptools › tests › win_script_wrapper.txt
Python Script Wrapper for Windows =================================
setuptools includes wrappers for Python scripts that allows them to be executed like regular windows programs. There are 2 wrappers, once for command-line programs, cli.exe, and one for graphica programs, gui.exe. These programs are almost identical, function pretty much the same way, and are ...
🌐
PyPI
pypi.org › project › commandwrapper
commandwrapper · PyPI
Command Wrapper (make subprocess.Popen() easy) pip install commandwrapper Copy PIP instructions · Author : Yves-Gwenael Bourhis ================================================== Wrap a shell comand into a python threaded object.
🌐
PyPI
pypi.org › project › process-wrapper
process-wrapper · PyPI
Process Wrapper is a simple module that executes a command-line style program via a context manager. This is useful if you want your script to do something that relies on a background process.