OS-independent wrapper for shlex and mslex
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
- gotcha Prior to version 0.1.3, `oslex` had compatibility issues with Python versions 3.6 through 3.8. Users on these Python versions might encounter unexpected errors.
- gotcha While `oslex` itself is distributed under the MIT license, it depends on the `mslex` library for its Windows functionality, which is licensed under Apache 2.0. Users should be aware of this dual licensing for their projects.
- gotcha The `oslex` library acts as a wrapper. While it provides a unified API, the underlying behavior (especially quoting rules) for Windows systems is governed by `mslex`, which may differ subtly from `shlex` on POSIX. Always test complex shell commands in your target environment.
Install
-
pip install oslex
Imports
- oslex
import oslex
Quickstart
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}'")