Skip to main content

A Python library for capturing output from terminal commands with advanced features

Project description

Command Capture Library

A comprehensive Python library for capturing output from terminal commands with advanced features including timeouts, real-time output, parallel execution, and more.

Features

  • 🚀 Simple and intuitive API - Easy to use for basic command execution
  • ⏱️ Timeout support - Prevent commands from running indefinitely
  • 🔄 Real-time output - Stream command output as it happens
  • 🏃 Parallel execution - Run multiple commands simultaneously
  • 🌍 Environment control - Set custom environment variables
  • 📁 Working directory - Execute commands in specific directories
  • 💾 Input/Output handling - Send input to commands and capture all output
  • 🛡️ Error handling - Comprehensive error handling with custom exceptions
  • 📊 Detailed results - Rich result objects with execution metadata
  • 🖥️ Cross-platform - Works on Linux, macOS, and Windows
  • 📦 Zero dependencies - Uses only Python standard library

Installation

From source

git clone <repository-url>
cd CommandCapture 
pip install .

For development

pip install -e ".[dev]"

Quick Start

from cmdcapture import CommandCapture

# Create a CommandCapture instance
capture = CommandCapture()

# Execute a simple command
result = capture.run("echo 'Hello, World!'")
print(result.stdout)  # Hello, World!
print(result.success)  # True
print(result.return_code)  # 0

Basic Usage

Simple Command Execution

from cmdcapture import CommandCapture

capture = CommandCapture()
result = capture.run("ls -la")

if result.success:
    print("Command succeeded!")
    print(result.stdout)
else:
    print(f"Command failed with return code: {result.return_code}")
    print(result.stderr)

Error Handling

from cmdcapture import CommandCapture, CommandError

capture = CommandCapture()
try:
    # Use check=True to raise exception on command failure
    result = capture.run("ls /nonexistent", check=True)
except CommandError as e:
    print(f"Command failed: {e}")
    print(f"Return code: {e.return_code}")

Timeout Support

from cmdcapture import CommandCapture, TimeoutError

capture = CommandCapture()
try:
    result = capture.run("sleep 10", timeout=5.0)
except TimeoutError as e:
    print(f"Command timed out after {e.timeout} seconds")

Advanced Features

Real-time Output

def progress_callback(line):
    print(f"[OUTPUT] {line}")

result = capture.run("ping -c 3 google.com", progress_callback=progress_callback)

Environment Variables

result = capture.run("echo $MY_VAR", env={"MY_VAR": "Hello World"})
print(result.stdout)  # Hello World

Working Directory

result = capture.run("pwd", cwd="/tmp")
print(result.stdout)  # /tmp (or equivalent)

Input Data

result = capture.run("grep 'pattern'", input_data="line with pattern\nother line")
print(result.stdout)  # line with pattern

Multiple Commands

# Sequential execution
commands = ["echo 'first'", "echo 'second'", "echo 'third'"]
results = capture.run_multiple(commands)

# Parallel execution
results = capture.run_multiple(commands, parallel=True)

Command Availability

if capture.is_available("git"):
    result = capture.run("git --version")
    print(result.stdout)
else:
    print("Git is not available")

Command Line Interface

The library also provides a CLI interface:

# Basic usage
cmdcapture "echo 'Hello, World!'"

# With JSON output
cmdcapture "ls -la" --json

# With timeout
cmdcapture "sleep 5" --timeout 2

# With working directory
cmdcapture "pwd" --cwd /tmp

# Send input
cmdcapture "cat" --input "Hello, World!"

API Reference

CommandCapture

Main class for executing commands.

Constructor

CommandCapture(
    default_timeout: Optional[float] = None,
    default_cwd: Optional[Union[str, Path]] = None,
    default_env: Optional[Dict[str, str]] = None
)

Methods

run(command, **kwargs) -> CaptureResult

Execute a command and return results.

Parameters:

  • command: Command to execute (string or list)
  • timeout: Timeout in seconds
  • cwd: Working directory
  • env: Environment variables
  • check: Raise exception on command failure
  • shell: Execute through shell (default: True)
  • input_data: Data to send to stdin
  • progress_callback: Function to call with real-time output

run_multiple(commands, parallel=False, **kwargs) -> List[CaptureResult]

Execute multiple commands.

is_available(command) -> bool

Check if a command is available in the system PATH.

CaptureResult

Result object containing command execution details.

Attributes:

  • command: The executed command
  • return_code: Exit code
  • stdout: Standard output
  • stderr: Standard error output
  • execution_time: Execution time in seconds
  • success: Boolean indicating success (return_code == 0)
  • pid: Process ID

Exceptions

CommandError: Raised when command execution fails (when check=True) TimeoutError: Raised when command execution times out

Testing

Run the test suite:

python test_cmdcapture.py

Run the examples:

python example.py

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

cmdcapture-1.0.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

cmdcapture-1.0.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file cmdcapture-1.0.0.tar.gz.

File metadata

  • Download URL: cmdcapture-1.0.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for cmdcapture-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c258a78d52cd4220bcd1f36cc7c917937201864132d1cd7b8d4027d86909e8db
MD5 2708e2b13e90f7063eff6784a90e844d
BLAKE2b-256 3a40f5da39f87aa6e1f198988cec15154a2dae28641af8a0d15804cd0cb23025

See more details on using hashes here.

File details

Details for the file cmdcapture-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: cmdcapture-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for cmdcapture-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 294809eebad13571176ba083a4979f2a6d6a92faed67d038083a1f49db5c2db5
MD5 954d41999c18faa5d155712af6a251b1
BLAKE2b-256 82a88a795b1382e065061f3ca81b876a9bde691d559ab7aece63f40f584a0031

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