OS-independent wrapper for shlex and mslex

0.1.3 · active · verified Wed Apr 15

oslex is an OS-independent wrapper for shlex and mslex. Its main purpose is to provide functions similar in functionality to shlex.quote(), shlex.split(), and shlex.join() on both Windows and POSIX-compatible platforms. It achieves this by forwarding calls to the standard library's `shlex` on POSIX systems or the `mslex` library on Windows. The current version is 0.1.3, with the last release in July 2023.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core `quote`, `split`, and `join` functionalities of `oslex`, which behave consistently across operating systems by using `shlex` on POSIX and `mslex` on Windows.

import oslex
import os

# Example: Safely quote a string for the shell
command_part = "foo; rm -rf /"
quoted_command = oslex.quote(command_part)
print(f"Original: '{command_part}'\nQuoted: '{quoted_command}'")

# Example: Split a shell command into arguments
shell_command = 'python -m pip install "oslex>=0.1.3" --upgrade'
args = oslex.split(shell_command)
print(f"\nShell command: '{shell_command}'\nSplit args: {args}")

# Example: Join arguments into a shell command
args_to_join = [os.environ.get('PYTHON_EXECUTABLE', 'python'), '-c', 'print("Hello World")']
joined_command = oslex.join(args_to_join)
print(f"\nArguments: {args_to_join}\nJoined command: '{joined_command}'")

view raw JSON →