python / python/cpython

argparse eats initial '=' from short option argument

Đang mở
#124,305 9 bình luận 0 reaction 1 người được giao Xem trên GitHub

@serhiy-storchaka đang làm issue này rồi.

Từ ngày 23/9/2024.

stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

When configured with a short option that requires an argument, and an input commandline has the option argument at the same argv item, argparse deletes initial '=' from it. This behavior is inconsistent with other implementations of the same parsing (at least: getopt, getopt_long from glibc or FreeBSD libc; getopt and optparse from Python standard library).

Actual result: initial '=' is deleted under the described conditions.

Expected result: initial '=' is kept in the collected option value.

Example code:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-Q', type=str)
args = parser.parse_args()
print(getattr(args, 'Q', None))

Running it:

$ ./t_eq_argparse.py -Q22
22
$ ./t_eq_argparse.py -Q=22
22

With a separate argument, truncating has no place:

$ ./t_eq_argparse.py -Q =22
=22

Compare to getopt module:

$ ./t_eq_getopt.py -Q22
22
$ ./t_eq_getopt.py -Q=22
=22

The same result with libc getopt (Ubuntu 22.04) and optparse module. Test code for getopt and optparse is obvious.

The issue is present with today's Lib/argparse.py from github trunk.

CPython versions tested on:

3.10, CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-124332

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.