oslex2 is an OS-independent wrapper for Python's shlex (POSIX) and mslex (Windows) modules.
Like
os.pathabstracts overposixpathandntpath,oslex2abstracts overshlexandmslexfor shell-quoting and splitting.
- Unified API for shell quoting, splitting, and joining on all platforms
- Automatically uses
shlexon POSIX andmslexon Windows - Drop-in replacement for
shlexin cross-platform code
pip install oslex2import oslex2
# Safely quote a string for the shell
cmd = oslex2.quote('foo; rm -rf /')
# Split a shell command into arguments
args = oslex2.split('python -m pip install "oslex2>=1.0.0"')
# Join arguments into a shell command
command = oslex2.join(["python", "-m", "pip", "install", "oslex2>=1.0.0"])oslex2.quote(s: str, **kwargs) -> str: Shell-escape a string for safe use as a single token.oslex2.split(s: str, **kwargs) -> List[str]: Split a shell command string into arguments.oslex2.join(args: List[str]) -> str: Join arguments into a shell-escaped command string.
Note: All functions accept **kwargs and pass them through to the underlying shlex or mslex implementation. This allows you to use platform-specific options (such as posix, punctuation_chars, etc.) as needed.
- On Windows, uses
mslexfor Windows shell syntax. - On all other platforms, uses the standard library
shlex.
- MIT License (this library)
mslexis Apache 2.0