python / python/cpython

argparse: document exceptions, subclass from SystemExit and ArgparseError

Đang mở
#93,873 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.

docs
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

There are many problems with exit status people encounter using argparse (and some of them are open, e.g. https://github.com/python/cpython/issues/85427).

It is reasonable and consistent with other code that it raises SystemExit (in case of unrecognized arguments).
However, this is not flexible and requires pretty much work from users if they want to catch this error, and not exit (there are many alternatives (which in itself contradicts the Zen of Python), the default proposes to subclass the ArgumentParser).

Why can't argparse just subclass SystemExit? This is usually a good guideline for a module to create its own exception classes.

Also, all three argparse exceptions could derive from one argparse exception class (to make their interception easier) (in the title I suggested ArgparseError, but a better name might be found). However, this is not so crucial as this SystemExit: I run a big function in one place of which there is parse_args; I don't want to attribute all SystemExit-s to argparse when I catch exceptions from that function (for now I had to create my own subclass of SystemExit raised from the line with parse_args).

And (should it be a separate feature request?) could there be a subsection dedicated to exceptions in the argparse module documentation? I had to read Python help to understand the initialisation of ArgumentError.
If argparse could guarantee the types of exceptions that it can raise, that would be ideal.

Originally posted by @ericvsmith in https://github.com/python/cpython/issues/83514#issuecomment-1093850932

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ừ các điểm vào của argparse được đề cập trong issue: ArgumentParser, parse_args và ArgumentError, sau đó xem lại phần về các phương thức thoát trong tài liệu argparse và đầu ra trợ giúp của Python. Xác định hệ thống phân cấp ngoại lệ dự kiến, các bảo đảm về kiểu ngoại lệ được phát sinh và phạm vi của một tiểu mục riêng về ngoại lệ trước khi đề xuất các thay đổi và cập nhật tài liệu.

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, documentation
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá 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.