| CARVIEW |
Select Language
HTTP/2 200
server: nginx
date: Fri, 26 Dec 2025 20:11:18 GMT
content-type: text/html; charset=utf-8
expires: Fri, 26 Dec 2025 20:11:17 GMT
cache-control: no-cache
x-frame-options: sameorigin
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-permitted-cross-domain-policies: none
content-encoding: gzip
x-clacks-overhead: GNU Terry Pratchett
strict-transport-security: max-age=315360000; includeSubDomains; preload
Issue 22909: [argparse] Using parse_known_args, unknown arg with space in value is interpreted as first positional arg - Python tracker
- Python Home
- About
- News
- Documentation
- Downloads
- Community
- Foundation
- Developer's Guide
- Issue Tracker
- Issues
- Summaries
- User
- Administration
- Help
Issue22909
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2014-11-20 21:28 by TabAtkins, last changed 2022-04-11 14:58 by admin. This issue is now closed.
| Messages (3) | |||
|---|---|---|---|
| msg231448 - (view) | Author: Tab Atkins Jr. (TabAtkins) | Date: 2014-11-20 21:28 | |
If using parse_known_args() to get argument pass-through, and one of the "unknown" arguments has a value with a space in it (even if quoted!), the entire unknown argument (key=value) will be interpreted as the value of the first positional argument instead.
Example:
```
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("pos", nargs="?", default=None)
parser.parse_known_args(["--foo='bar'"])
# (Namespace(pos=None), ['--foo=bar'])
# As expected.
parse.parse_known_args(["--foo='bar baz'"])
# (Namespace(pos="--foo='bar baz'"), [])
# What?!?
```
Since *known* arguments with spaces in them are parsed fine, this looks to be regression in a lesser-used feature.
|
|||
| msg231877 - (view) | Author: paul j3 (paul.j3) * ![]() |
Date: 2014-11-30 01:45 | |
This an issue for parse_args as well. parse_args just calls parse_known_args, and raises an error if extras is not empty.
Early on in parsing, it tries to classify argument strings as either optionals (--flags) or positionals (arguments). And there's an explicit test for spaces:
def _parse_optional(self, arg_string):
...
# if it contains a space, it was meant to be a positional
if ' ' in arg_string:
return None
Basically, if it can't match the string with a define optional, and it contains a space (anywhere) it is classed as positional. That's what your example shows.
It sounds familiar, so I suspect it was raised in an earlier issue. I'll have to look it up.
|
|||
| msg231879 - (view) | Author: paul j3 (paul.j3) * ![]() |
Date: 2014-11-30 02:44 | |
Duplicate of https://bugs.python.org/issue22433 |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:58:10 | admin | set | github: 67098 |
| 2014-11-30 02:48:24 | berker.peksag | set | status: open -> closed superseder: Argparse considers unknown optional arguments with spaces as a known positional argument stage: resolved |
| 2014-11-30 02:44:38 | paul.j3 | set | resolution: duplicate messages: + msg231879 |
| 2014-11-30 01:45:35 | paul.j3 | set | nosy:
+ paul.j3 messages: + msg231877 |
| 2014-11-20 21:28:58 | TabAtkins | create | |

