Skip to main content

README

Install

Install with pip install cli-passthrough / pip install -e . if you have this repo.

How to Use

From the terminal:

$ cli-passthrough echo 'hi'
hi
$ echo 'hi'
hi
$ cli-passthrough python --error
Unknown option: --
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
$ python --error
Unknown option: --
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.

From Python:

In [1]: from cli_passthrough import cli_passthrough

In [2]: cli_passthrough("echo 'hi'")
hi
Out[2]: 0

In [3]: cli_passthrough("python --error")
Unknown option: --
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
Out[3]: 2

What does it do?

This project provides an entry point cli-passthrough in the terminal that accepts any amount of parameters, and runs those parameters as it's own command. Except in a few special cases, this will output to the terminal exactly what the command would have, including any formatting done with escape sequences. Both the combined stdout and stderr are logged, with order preserved, in logs/history.log, and the stderr by itself is logged in /logs/stderr.log. These log files are written to in realtime. The output to the terminal is also in realtime. The original intent was to dump all output back to the screen, while saving both stdout and stderr. Future work will be to return those outputs to Python as well.

This project was motivated by making a wrapper on another application which needed to be used over the CLI. I wanted to also use the wrapper from the CLI as well. I wanted to see the output of the program I was invoking in real-time, in the same formatting, and log everything. In other words, I wanted the following:

  1. ANY COMMAND: Run nearly any command given to it.
  2. FLEXIBLE LOGGING: Capture stdout and stderr independently for flexible logging.
  3. ORDER PESERVATION: Preserve order of stdout and stderr
  4. REALTIME OUTPUT: Output to the screen in realtime, i.e. don't wait for the command to exit before dumping to the screen.
  5. EXIT STATUS: Capture exit code
  6. FORMATTING PRESERVED: Preserve ANSI escape sequences so most things are still formatted as if not run through this passthrough.
1 2 3 4 5 6 note
os.system Y N Y Y Y Y Doesn't capture output at all
os.popen N N Y N N Y Deprecated & obsolete
os.exec* Y N - N N N Really not the intended use
pexpect Y N Y Y Y Y Bad logging
subprocess.popen + threads Y Y Y N Y Y/N Doesn't have real-time output because it's often dependent on buffer flushes of blocks.
subprocess.popen + pty Y Y Y Y Y Y We got it!

Known Limitations

This implementation of subprocess.popen + pty currently has the following limitations:

  1. It doesn't run every command. Commands that need input from the user do not work, such as ssh or ipython.

  2. It makes assumptions about the terminal size. It would be better if it detected the terminal the python is ran in, and use the same dimensions.

This is very much largely adapted (copied) from this SO post. I just wrapped it up into an importable function, gave it a CLI itself, and made basic logging to illustrate the point. Feel free to copy/paste/tweek it yourself.

If you find something better please let me know! I'd be more than happy to upgrade or replace this. This is simply the best I've found so far.

Metadata

Release files for CLI-passthrough 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for CLI-passthrough 0.1.3
File Size Uploaded Provenance
CLI-passthrough-0.1.3.linux-x86_64.tar.gz 6.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for CLI-passthrough 0.1.3
File Interpreter ABI Platform
CLI_passthrough-0.1.3-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 13.5 kB

Release files / CLI-passthrough-0.1.3.linux-x86_64.tar.gz

Download URL CLI-passthrough-0.1.3.linux-x86_64.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b71b7f777d4b18ed3b6d4b232e9b33a3e065291bc5e254c6df0b443a3eda617f
BLAKE2b-256 checksum
How to use checksums
f673a26855d13e32b14b00d2f093a1de881275f2a17146ca502bc076d7de7280
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0.post20191030 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.8.0

Release files / CLI_passthrough-0.1.3-py2.py3-none-any.whl

Download URL CLI_passthrough-0.1.3-py2.py3-none-any.whl
Size 6.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ab407862f520baacfedff6ab6bcb9985f56832e6bb1d2f7deb111d9bd305deb5
BLAKE2b-256 checksum
How to use checksums
53f9fab7e9a395d0f7825d6d19662ebad060ac3f0686d4b9b8ae70d1dd1a8212
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0.post20191030 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.8.0

Security for CLI-passthrough 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page