python / python/cpython

Argparse: add method for retrieving the parsers of actions

Đang mở
#144,932 2 bình luận 1 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:

I have a CLI interface with an argparse.ArgumentParser composed of a bunch of different actions. I was trying to validate if an argument had been set to a default value, and with just the main parser available, I was having some issues using get_default() working from the main parser, rather than the parsers for the various actions.

I ended up going with the following comparison, in order to reduce the amount of refactoring I would need to do, where args is the var holding the object returned from parse_args():

        args.my_param == parser._actions[1].choices[args.action]].get_default(args.my_param)

I could also have solved this by adding a wrapper class holding all the subparsers in an object of some kind.

But - I think it would be nice to have a public method for accessing these action parsers, mostly for enabling this kind of use case.

Has this already been discussed elsewhere?

Not that I could find

Links to previous discussion of this feature:

No response

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 với argparse.ArgumentParser, collection _actions của parser, các subparser choices và get_default(). Xác định phạm vi và hành vi dự kiến của public method dùng để truy xuất action parser, bao gồm cách method này cần hỗ trợ use case xác thực giá trị mặc định đã mô tả. Công việc được xem là hoàn tất khi API và hành vi của nó được định nghĩa rõ ràng, đồng thời feature được bao phủ bởi các test phù hợp.

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ó
4/5
Thời gian dự kiến
3-5 ngày
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
35/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.