Skip to content

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

Closed

Description

@kushal-kumaran

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

Activity

  1. sobolevn commented on Sep 16, 2023

    @sobolevn
    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

  2. added
    pendingThe issue will be closed if no feedback is provided
    on Sep 16, 2023
  3. SouSingh commented on Sep 16, 2023

    @SouSingh

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

  4. ericvsmith commented on Sep 18, 2023

    @ericvsmith
    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.

  5. removed
    pendingThe issue will be closed if no feedback is provided
    on Sep 18, 2023
  6. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 26, 2023
  7. added a commit that references this issue on Jan 31, 2024
  8. added a commit that references this issue on Feb 5, 2024
  9. added a commit that references this issue on Feb 5, 2024
  10. added a commit that references this issue on Feb 5, 2024
  11. added
    3.11only security fixes
    3.12only security fixes
    3.13only security fixes
    on Feb 5, 2024
  12. added 2 commits that reference this issue on Feb 5, 2024
  13. added a commit that references this issue on Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.11only security fixes3.12only security fixes3.13only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions