Skip to main content

Run shell commands with persistent directory context, logging, and color output.

Project description

cmdrun

Python License PyPI

Run shell commands with persistent directory context, full logging, color output, and log export.


Installation

pip install cmdrun

With color support:

pip install cmdrun[color]

Quick start

from cmdrun import cmd

cmd.cd(r"C:\Users\Fede\Desktop")
cmd.run("echo Hello from cmdrun")
cmd.run(["pip", "install", "numpy"])   # list form also works

print(cmd.log)
cmd.save_log("session.json", fmt="json")

API

cmd.cd(path)self

Changes the working directory for all subsequent run() calls.

  • Expands ~ and environment variables
  • Raises ValueError if the directory does not exist
  • Returns self so it can be chained: cmd.cd("/tmp").run("ls")

cmd.run(command, *, timeout, encoding, raise_on_error, env)CommandResult

Executes a command in the current working directory.

Parameter Type Default Description
command str or list[str] Command to run
timeout float None Kill after N seconds
raise_on_error bool False Raise RuntimeError on non-zero exit
env dict None Extra environment variables

CommandResult attributes:

Attribute Type Description
.stdout str Standard output
.stderr str Standard error
.returncode int Exit code (0 = success)
.ok bool True when returncode == 0
.cwd str Directory where the command ran

cmd.log (property)

Returns a formatted string with the full history of every cd() and run() call.

[1] 2026-01-15 10:30:00  |  cwd: /tmp
    $ echo Hello
    exit: 0
    stdout:
    Hello

cmd.save_log(path, *, fmt="txt")

Exports the log to a file. fmt can be "txt" or "json".

cmd.save_log("session.txt")
cmd.save_log("session.json", fmt="json")

cmd.log_entries (property)

Raw log as a list[dict] for programmatic access. Each entry has: timestamp, command, stdout, stderr, returncode, cwd.


cmd.clear_log()

Clears all log entries.


cmd.cwd (property)

Returns the current working directory.


Verbose mode

Prints each command and its result in real time as it runs:

from cmdrun import CommandRunner

cmd = CommandRunner(verbose=True)
cmd.cd("/tmp")
cmd.run("echo Hello")

Output:

  → cwd: /tmp
  $ echo Hello
  ✓ exit: 0
    Hello

Color support

Install colorama to get colored output in the log and verbose mode:

pip install cmdrun[color]
# or
pip install colorama

When saving logs to file with save_log(), ANSI color codes are automatically stripped.


License

MIT © Fede

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycmdrun-1.2.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycmdrun-1.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file pycmdrun-1.2.0.tar.gz.

File metadata

  • Download URL: pycmdrun-1.2.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for pycmdrun-1.2.0.tar.gz
Algorithm Hash digest
SHA256 ee3c618f40aa5ba52c067e3854805ce9cd9e2d17ec0d9f9976b835b983baad74
MD5 42d8f17023ae042e4bb97d9d70b09346
BLAKE2b-256 7bf7c53d78da3e526c355e4fe29cc6c2b04a9740c315bc02f9ca647d3ddcb020

See more details on using hashes here.

File details

Details for the file pycmdrun-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pycmdrun-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for pycmdrun-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2780b9590c67e78ff5cf928e2c4b92ab664a40b36a79454a3d0f465d0e139b76
MD5 b4e6b2c64143402652fe7f0d5c44395d
BLAKE2b-256 1868723e7b0991c4eb57907dfca10ccb451bfd4db8491bbe07e87fa7311b58f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page