Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse fails to get value of argument if the value is -- #109475

Closed
kushal-kumaran opened this issue Sep 16, 2023 · 3 comments
Closed

argparse fails to get value of argument if the value is -- #109475

kushal-kumaran opened this issue Sep 16, 2023 · 3 comments
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@kushal-kumaran
Copy link

kushal-kumaran commented Sep 16, 2023

Bug report

Bug description:

This code:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--broken")
parser.add_argument("--working")

args = parser.parse_args(["--broken=--", "--working=asdf"])
print(args)

prints:

$ podman run -v $(pwd):/src python:3.11.5-bookworm python /src/delim.py 
Namespace(broken=[], working='asdf')

The value of the --broken argument is not parsed, presumably because it is the standard end-of-argument-list value. But this does not seem expected when the value is introduced with =. Besides, in this case, the parsing does continue for subsequent args, so it was not actually treated as end of arguments.

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

Linux

Linked PRs

@kushal-kumaran kushal-kumaran added the type-bug An unexpected behavior, bug, or error label Sep 16, 2023
@sobolevn
Copy link
Member

-- means a very specific thing in argument parsing, I don't think that it should be a value of any other argument. If you need this value somewhere in your program, you can use some alias: like --flag=doubledash

@sobolevn sobolevn added the pending The issue will be closed if no feedback is provided label Sep 16, 2023
@SouSingh
Copy link

Hello @kushal-kumaran I try to run it on my device I get some output I hope that will help
Screenshot (106)

@ericvsmith
Copy link
Member

-- means a very specific thing in argument parsing, I don't think that it should be a value of any other argument. If you need this value somewhere in your program, you can use some alias: like --flag=doubledash

I completely disagree with this. Imagine if you were reading the documentation of a command line utility, and it said "If you want the output file to be named '--', then use 'doubledash' instead, and this will be translated to '--' internally. It is not possible to specify an output name of 'doubledash'."

I think this is a completely valid bug report. I'm not sure it's fixable, given all of the issues with argparse and how it scans arguments. I am surprised that using =-- doesn't work. See #53580 for a case where using = was suggested as a partial workaround.

@ericvsmith ericvsmith removed the pending The issue will be closed if no feedback is provided label Sep 18, 2023
@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 26, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jan 31, 2024
@serhiy-storchaka serhiy-storchaka self-assigned this Jan 31, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 5, 2024
pythonGH-114814)

For example "--option=--".
(cherry picked from commit 4aa4f09)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Feb 5, 2024
…argparse (pythonGH-114814)

For example "--option=--".
(cherry picked from commit 4aa4f09)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@serhiy-storchaka serhiy-storchaka added 3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes labels Feb 5, 2024
serhiy-storchaka added a commit that referenced this issue Feb 5, 2024
…se (GH-114814) (GH-115036)

For example "--option=--".
(cherry picked from commit 4aa4f09)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Feb 5, 2024
…se (GH-114814) (GH-115037)

For example "--option=--".
(cherry picked from commit 4aa4f09)
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this issue Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants