Registry / serialization / mslex
library1.3.0pypypi✓ verified 22d ago

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 mslex
INSTALL
IMPORT
SIG · MSLEX
M
mslex
serializationpythonv1.3.0
Install
1.5s avg
Import
12ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.008s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mslex
import mslex
The library exports its functions directly under the 'mslex' module name.
split
from mslex import split
quote
from mslex import quote
join
from mslex import join

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.

import mslex import sys # Example 1: Quoting a string for Windows command line path_with_spaces = r'C:\Program Files\My App\app.exe' quoted_path = mslex.quote(path_with_spaces) print(f"Original path: {path_with_spaces}") print(f"Quoted path: {quoted_path}") # Expected: C:\\Program\ Files\\My\ App\\app.exe (or similar depending on for_cmd) # Example 2: Splitting a Windows command line string command_string = 'dir "C:\\Program Files" /s' args = mslex.split(command_string) print(f"Command string: {command_string}") print(f"Split arguments: {args}") # Expected: ['dir', 'C:\\Program Files', '/s'] # Example 3: Handling complex Windows quoting nuances # mslex attempts to parse both UCRT and msvcrt.dll ways by default and can raise an error if they disagree. # You can specify ucrt=True/False or like_cmd=True/False for specific behaviors. # This example demonstrates a basic split without specific runtime flags. complex_command = 'program.exe arg1 "arg 2 with spaces" ^"escaped arg^"' if sys.platform == 'win32': try: parsed_complex_args = mslex.split(complex_command) print(f"Complex command (Windows): {complex_command}") print(f"Parsed complex arguments: {parsed_complex_args}") except ValueError as e: print(f"Could not parse complex command due to ambiguity: {e}") else: print(f"mslex is primarily for Windows; skipping complex example on {sys.platform}")
Debug
Known issues
gotchamslex specifically targets the non-POSIX, often inconsistent, command-line parsing and quoting rules of Windows (`cmd.exe`, `CommandLineToArgvW`, `msvcrt.dll`). Directly replacing the standard library `shlex` with `mslex` on non-Windows (POSIX) systems will result in incorrect behavior.
fix
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.
affects: All versions
gotchaWindows command-line parsing has historical ambiguities (e.g., differences between older `msvcrt.dll` and modern UCRT). By default, `mslex.split()` attempts to parse a string using both common Windows C runtime behaviors and raises a `ValueError` if they disagree, indicating an ambiguous input. This strictness ensures correctness but might require explicit `ucrt=True` or `ucrt=False` arguments for specific scenarios.
fix
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.
affects: All versions
gotchaThe `mslex.quote()` function provides a `for_cmd` parameter (default `True`) that dictates whether the output string should be correctly parsed by `cmd.exe` *and* `CommandLineToArgvW` (when `True`), or just `CommandLineToArgvW` directly (when `False`). Incorrectly setting this can lead to misinterpretation of quoted arguments by the target process.
fix
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'mslex'
The 'mslex' package is not installed in the current Python environment or there is a typo in the import statement.
fix
pip install mslex
from mslex import split_command
The function 'split_command' does not exist in the 'mslex' package; the correct function name is 'split'.
fix
from mslex import split
AttributeError: module 'mslex' has no attribute 'quote_unix'
The 'mslex' library is designed for Windows-specific command-line parsing and quoting and does not include Unix-specific functions like 'quote_unix' found in 'shlex'.
fix
Use 'mslex.quote' for Windows-specific quoting.
TypeError: 'int' object cannot be interpreted as a string
The 'mslex.split', 'quote', or 'join' functions expect string inputs, but a non-string type (like an integer) was provided.
fix
Ensure that the argument passed to 'mslex' functions is a string, e.g., 'mslex.split("command line")'.
Upgrade
Version history
1.3.0latest on PyPI · released Oct 16, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources