{"id":5331,"library":"mslex","title":"mslex","description":"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.","status":"active","version":"1.3.0","language":"en","source_language":"en","source_url":"https://github.com/smoofra/mslex","tags":["windows","shlex","shell","command-line","quoting","parsing"],"install":[{"cmd":"pip install mslex","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The library exports its functions directly under the 'mslex' module name.","symbol":"mslex","correct":"import mslex"},{"symbol":"split","correct":"from mslex import split"},{"symbol":"quote","correct":"from mslex import quote"},{"symbol":"join","correct":"from mslex import join"}],"quickstart":{"code":"import mslex\nimport sys\n\n# Example 1: Quoting a string for Windows command line\npath_with_spaces = r'C:\\Program Files\\My App\\app.exe'\nquoted_path = mslex.quote(path_with_spaces)\nprint(f\"Original path: {path_with_spaces}\")\nprint(f\"Quoted path: {quoted_path}\")\n# Expected: C:\\\\Program\\ Files\\\\My\\ App\\\\app.exe (or similar depending on for_cmd)\n\n# Example 2: Splitting a Windows command line string\ncommand_string = 'dir \"C:\\\\Program Files\" /s'\nargs = mslex.split(command_string)\nprint(f\"Command string: {command_string}\")\nprint(f\"Split arguments: {args}\")\n# Expected: ['dir', 'C:\\\\Program Files', '/s']\n\n# Example 3: Handling complex Windows quoting nuances\n# mslex attempts to parse both UCRT and msvcrt.dll ways by default and can raise an error if they disagree.\n# You can specify ucrt=True/False or like_cmd=True/False for specific behaviors.\n# This example demonstrates a basic split without specific runtime flags.\ncomplex_command = 'program.exe arg1 \"arg 2 with spaces\" ^\"escaped arg^\"'\nif sys.platform == 'win32':\n    try:\n        parsed_complex_args = mslex.split(complex_command)\n        print(f\"Complex command (Windows): {complex_command}\")\n        print(f\"Parsed complex arguments: {parsed_complex_args}\")\n    except ValueError as e:\n        print(f\"Could not parse complex command due to ambiguity: {e}\")\nelse:\n    print(f\"mslex is primarily for Windows; skipping complex example on {sys.platform}\")","lang":"python","description":"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."},"warnings":[{"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.","message":"mslex 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Windows 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}