argparse option argument with `nargs=argparse.REMAINDER` is misplaced in usage line

Offen
#92,625 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
45/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
cli

Rechercherichtung

Führe das minimale argparse-Skript aus dem Bericht aus und untersuche, wie ArgumentParser seine Usage-Zeile für add_argument-Aufrufe mit nargs=argparse.REMAINDER formatiert. Bestätige die aktuelle Reihenfolge und ermittle die relevanten Tests oder den Einstiegspunkt für die Usage-Formatierung von argparse. Erledigt ist die Aufgabe, wenn die Remainder-Option in der Usage-Ausgabe nach den Positionsargumenten erscheint, ohne die Reihenfolge anderer Optionen zu verändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

stdlib type-feature

Bug report

When using an optional argument with nargs=argparse.REMAINDER, the argument appears within the list of optional arguments and before any positional arguments. I expected it to be placed last in usage line, which would better match the behavior since it consumes any following parameters. Not sure it is really bug...

The following scripts shows the issue:

#!/usr/bin/env python

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('foo')
parser.add_argument('--extra', nargs=argparse.REMAINDER)
parser.add_argument('--option')

args = parser.parse_args()

[--extra ...] is not at all last in the usage line:

$ ./test.py -h
usage: test.py [-h] [--extra ...] [--option OPTION] foo

positional arguments:
  foo

options:
  -h, --help       show this help message and exit
  --extra ...
  --option OPTION

Your environment

  • archlinux/amd64
  • Tested on python 3.10.4 and also on 3.7.7 and 3.8.7.
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
558

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/cpython

Alle Issues in python/cpython

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.