`HelpFormatter.write_usage` breaks options at a hyphen

Đang mở Phù hợp với người mới
#3,362 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@kdeldycke đang làm issue này rồi.

Từ ngày 2/11/2026.

  • #3385 của @cyphercodes — đã đóng, không merge
  • #3388 của @charlieleith — đã đóng, không merge
  • #3399 của @MackDing — đã đóng, không merge
  • #3411 của @lihan3238 — đã đóng, không merge
  • #3414 của @kimjune01 — đã đóng, không merge
  • #3425 của @RajvardhanPatil07 — đã đóng, không merge
  • #3437 của @c-tonneslan — đã đóng, không merge
  • #3441 của @goutamadwant — đã đóng, không merge
  • #3518 của @thakoreh — đã đóng, không merge
  • #3902 của @kdeldycke — đang mở

Đánh giá

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

Hướng nghiên cứu

Bắt đầu tại click.HelpFormatter.write_usage và đường dẫn wrap_text của nó, sau đó chạy bản tái hiện Python được cung cấp với width 65. Xác minh đầu ra đối chiếu với văn bản usage dự kiến và bổ sung coverage cho thấy các tùy chọn có dấu gạch nối vẫn nguyên vẹn ở ranh giới dòng, trong khi việc ngắt dòng vẫn tuân theo độ rộng đã cấu hình.

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

Mô tả

help output

If an option containing a hyphen in its name is printed at the line break limit, it’s broken at the hyphen.

Reproduction
import click

options = [
    "--enable-verbose-logging",
    "--output-file-path",
    "--max-retry-count",
    "--disable-cache-mode",
    "--config-file-location",
    "--user-auth-token",
    "--auto-update-interval",
    "--force-overwrite-existing",
    "--network-timeout-seconds",
    "--debug-trace-enabled",
]

f = click.HelpFormatter(width=65)
f.write_usage("program", " ".join(options))
print(f.getvalue())
Expected output
Usage: program --enable-verbose-logging --output-file-path
               --max-retry-count --disable-cache-mode
               --config-file-location --user-auth-token
               --auto-update-interval --force-overwrite-existing
               --network-timeout-seconds --debug-trace-enabled
Actual output
Usage: program --enable-verbose-logging --output-file-path --max-
               retry-count --disable-cache-mode --config-file-
               location --user-auth-token --auto-update-interval
               --force-overwrite-existing --network-timeout-
               seconds --debug-trace-enabled
Environment
  • Python version: 3.12.3
  • Click version: 8.3.1
Additional information
width option

This also happens without specifying width. The width option is altered in the example to make the issue more frequent.

Relevance to other use cases

This may likely only affect:

  • direct users of click.HelpFormatter,
  • users of click.argument with custom metavar containing hyphens.

On the other hand, click prints [OPTIONS] instead of listing individual options on the usage line and uses underscores instead of hyphens in argument names (unless specifying metavar).

Cause

I looked into the code and found that textwrap.TextWrapper is used under the hood, which has the break_on_hyphens option. However, there is no option for the click user to influence this, apart from reimplementing HelpFormatter.write_usage (in a subclass) and wrap_text.

Ngôn ngữ chính
Python
Star
17.8k
Fork
3.3k
Merge trung bình
3 ngày 3 giờ
Pull request đã merge (30 ngày)
13

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

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.

Issue khác của pallets/click

Tất cả issue của pallets/click

Issue tương tự

Thêm issue về Python

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.