python / python/cpython

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

未關閉
#92,625 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-feature
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

執行報告中的最小 argparse 腳本,並檢查 ArgumentParser 如何為使用 nargs=argparse.REMAINDER 的 add_argument 呼叫格式化 usage 行。確認目前的順序,並找出相關的 argparse usage 格式化測試或進入點。完成條件是:remainder 選項出現在 usage 輸出中的位置引數之後,同時不影響其他選項的順序。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
cli
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。