mslex is a Python library that provides `shlex`-like functionality specifically tailored for Windows shell command-line parsing and quoting. It addresses the complex and often inconsistent behaviors of `cmd.exe`, `CommandLineToArgvW`, and older `msvcrt.dll` runtimes on Windows. The library offers `split`, `quote`, and `join` functions, making it easier to construct and deconstruct command lines for Windows environments. It is currently at version 1.3.0, released on October 16, 2024, and is considered stable and actively maintained for its niche use case.
pip install mslexVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the core `mslex.quote` and `mslex.split` functions. It shows how to quote a path with spaces for Windows command lines and how to parse a command string into arguments. A note on complex Windows quoting behavior is included, highlighting mslex's ability to handle nuances and potential ambiguities.
Use the `oslex` or `oslex2` library if you need cross-platform `shlex`-like behavior that automatically adapts to the operating system. Alternatively, explicitly gate `mslex` usage with `if sys.platform == 'win32':` checks.
If `mslex.split()` raises a `ValueError` due to ambiguity, consult the Windows command-line parsing rules and specify `ucrt=True` for modern C runtime behavior or `ucrt=False` for older `msvcrt.dll` emulation, based on the target program's compilation. Also, consider the `like_cmd=True` parameter for `cmd.exe` specific parsing behavior.
Ensure the `for_cmd` argument in `mslex.quote()` is set appropriately for your use case. If the quoted string will be passed directly to a program that uses `CommandLineToArgvW` (e.g., via `subprocess.Popen` without `shell=True`), `for_cmd=False` might be more suitable. If it's intended for a `cmd.exe` command, `for_cmd=True` is typically correct.
pip install mslex
from mslex import split
Use 'mslex.quote' for Windows-specific quoting.
Ensure that the argument passed to 'mslex' functions is a string, e.g., 'mslex.split("command line")'.No dependency data recorded yet.