python / python/cpython

argparse: fix inconsistency in `add_argument()` API when using positional argument with `dest=` parameter

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

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

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

Mô tả

Feature or enhancement

Proposal:

Current behaviour is ugly and inconsistent between options and positional arguments:

parser.add_argument("--option-foo",    dest="my_foo_option_var")
parser.add_argument(metavar="BAR_ARG", dest="my_bar_arg_var")
# or even worse
parser.add_argument("my_bar_arg_var",  metavar="BAR_ARG")

Proposed change streamlines the library API

parser.add_argument("--option-foo", dest="my_foo_option_var")
parser.add_argument("BAR_ARG",      dest="my_bar_arg_var")

I think a small patch could fix this without breaking any existing code.

--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1423,7 +1423,7 @@
     # =======================
     def add_argument(self, *args, **kwargs):
         """
-        add_argument(dest, ..., name=value, ...)
+        add_argument(arg, ..., name=value, ...)
         add_argument(option_string, option_string, ..., name=value, ...)
         """
 
@@ -1433,7 +1433,11 @@
         chars = self.prefix_chars
         if not args or len(args) == 1 and args[0][0] not in chars:
             if args and 'dest' in kwargs:
-                raise ValueError('dest supplied twice for positional argument')
+                if 'metavar' not in kwargs:
+                    kwargs['metavar'] = args[0]
+                else:
+                    raise ValueError('`arg` supplied with both `dest` and `metavar` for positional argument')
+                args = (kwargs.pop('dest'), )
             kwargs = self._get_positional_kwargs(*args, **kwargs)
 
         # otherwise, we're adding an optional 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:

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 trong Lib/argparse.py tại điểm vào add_argument và xem xét cách xử lý hiện tại đối với các đối số vị trí theo đề xuất. Kiểm tra các bài kiểm thử argparse hiện có và xác nhận rằng hành vi mới của dest và metavar cho đối số vị trí hoạt động, trong khi các dạng hiện có vẫn tương thích.

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ó
3/5
Thời gian dự kiến
1-2 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.