python / python/cpython

`argparse._SubParsersAction.add_parser` should default to `description` matching `help`

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

Chưa có ai nhận issue này.

stdlib type-feature
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ả

Feature or enhancement

Proposal:
import argparse

# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--foo', action='store_true', help='foo help')
subparsers = parser.add_subparsers(help='subcommand help')

# create the parser for the "a" command
parser_a = subparsers.add_parser('a', help='a help')
parser_a.add_argument('bar', type=int, help='bar help')

# create the parser for the "b" command
parser_b = subparsers.add_parser('b', help='b help', description='b help')
parser_b.add_argument('--baz', choices=('X', 'Y', 'Z'), help='baz help')

# parse the args
print(parser.parse_args())

This currently produces:

gsnedders@gsnedders-margot tmp.IKpGQE2mE3 % python3.13 test.py --help  
usage: PROG [-h] [--foo] {a,b} ...

positional arguments:
  {a,b}       subcommand help
    a         a help
    b         b help

options:
  -h, --help  show this help message and exit
  --foo       foo help
gsnedders@gsnedders-margot tmp.IKpGQE2mE3 % python3.13 test.py a --help
usage: PROG a [-h] bar

positional arguments:
  bar         bar help

options:
  -h, --help  show this help message and exit
gsnedders@gsnedders-margot tmp.IKpGQE2mE3 % python3.13 test.py b --help
usage: PROG b [-h] [--baz {X,Y,Z}]

b help

options:
  -h, --help     show this help message and exit
  --baz {X,Y,Z}  baz help

It would be nicer, however, if add_parser's description parameter defaulted to the value of the help argument.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

https://github.com/python/cpython/issues/89438 considered not outputting the value of help here to be a bug, but was closed because the description parameter also exists.

Linked PRs
  • gh-135236

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.

Hướng nghiên cứu

Bắt đầu từ argparse._SubParsersAction.add_parser và xem xét PR được liên kết gh-135236 để hiểu hành vi được đề xuất. Công việc được hoàn thành khi một subparser sử dụng văn bản trợ giúp của nó làm mô tả mặc định, trong khi mô tả được cung cấp rõ ràng vẫn có hiệu lực, và hành vi argparse liên quan được bao phủ bằng các bài kiểm thử.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
cli
Loại issue
Tính năng
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

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.