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. added
    type-bug
    on Sep 16, 2023
  2. 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

  3. added
    pending
    on Sep 16, 2023
  4. 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)

  5. 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.

  6. removed
    pending
    on Sep 18, 2023
  7. added
    stdlib
    on Nov 26, 2023
  8. added a commit that references this issue on Jan 31, 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 a commit that references this issue on Feb 5, 2024
  12. added
    3.11
    3.12
    3.13
    on Feb 5, 2024
  13. added 2 commits that reference this issue on Feb 5, 2024
  14. 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.113.123.13stdlibtype-bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions