Hướng dẫn list as argument python argparse - liệt kê dưới dạng đối số python argparse

CÂU TRẢ LỜI NGẮN

Sử dụng tùy chọn

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
05 hoặc cài đặt
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
06 của tùy chọn
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
07 (tùy thuộc vào cách bạn muốn giao diện người dùng hoạt động).

nargs

parser.add_argument('-l','--list', nargs='+', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 2345 3456 4567

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
08 mất 1 hoặc nhiều đối số,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
09 không có hoặc nhiều hơn.

nối

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567

Với

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
10, bạn cung cấp tùy chọn nhiều lần để xây dựng danh sách.

Đừng sử dụng ________ 111 ​​!!! - Có lẽ không có tình huống nào bạn muốn sử dụng

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
11 với
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
13. Bao giờ.
- There is probably no situation where you would want to use
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
11 with
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
13. Ever.


CÂU TRẢ LỜI DÀI

Chúng ta hãy xem chi tiết hơn theo một số cách khác nhau mà người ta có thể cố gắng làm điều này và kết quả cuối cùng.

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)

Đây là đầu ra bạn có thể mong đợi:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']

Takeaways:

  • Sử dụng
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    05 hoặc
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    15
    • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      05 có thể đơn giản hơn từ góc độ người dùng, nhưng có thể không trực quan nếu có các đối số vị trí vì
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      13 không thể biết những gì nên là một đối số vị trí và những gì thuộc về
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      05; Nếu bạn có các đối số vị trí thì
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      15 có thể sẽ là một lựa chọn tốt hơn.
    • Trên đây chỉ đúng nếu
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      05 được đưa ra
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      21,
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      22 hoặc
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      23. Nếu bạn cung cấp một số nguyên (chẳng hạn như
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      24) thì sẽ không có vấn đề gì trộn các tùy chọn với
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      05 và đối số vị trí vì
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      13 sẽ biết chính xác có bao nhiêu giá trị mong đợi cho tùy chọn.
  • Không sử dụng báo giá trên dòng lệnh1
  • Không sử dụng
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    11, vì nó sẽ trả lại danh sách danh sách
    • Điều này xảy ra bởi vì dưới mui xe
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      13 sử dụng giá trị của
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      29 để ép buộc mỗi cá nhân đã đưa ra đối số mà bạn đã chọn
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      29, chứ không phải tổng hợp của tất cả các đối số.
    • Bạn có thể sử dụng
      parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
      # Use like:
      # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
      
      31 (hoặc bất cứ điều gì) để có được danh sách các ints (hoặc bất cứ điều gì)

1: Tôi không có nghĩa là nói chung .. Ý tôi là sử dụng báo giá để chuyển danh sách cho

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
13 không phải là điều bạn muốn.

Mới trong phiên bản 3.2.

Mã nguồn: lib/argparse.py Lib/argparse.py


Mô-đun

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 giúp dễ dàng viết các giao diện dòng lệnh thân thiện với người dùng. Chương trình xác định những đối số mà nó yêu cầu và
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 sẽ tìm ra cách phân tích các đối số của
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
35. Mô -đun
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 cũng tự động tạo thông báo trợ giúp và sử dụng. Mô -đun cũng sẽ phát hành lỗi khi người dùng đưa ra các đối số không hợp lệ.

Chức năng cốt lõi¶

Mô-đun

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 hỗ trợ cho các giao diện dòng lệnh được xây dựng xung quanh một thể hiện là
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
38. Nó là một thùng chứa cho các thông số kỹ thuật đối số và có các tùy chọn áp dụng toàn bộ trình phân tích cú pháp:

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')

Phương thức

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
39 đính kèm các thông số kỹ thuật đối số riêng lẻ vào trình phân tích cú pháp. Nó hỗ trợ các đối số vị trí, các tùy chọn chấp nhận các giá trị và cờ bật/tắt:

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag

Phương thức

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
40 chạy trình phân tích cú pháp và đặt dữ liệu được trích xuất vào đối tượng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
41:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)

Liên kết nhanh cho add_argument ()

Tên

Sự mô tả

Giá trị

hoạt động

Chỉ định cách xử lý một đối số

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
42,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
43,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
44,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
45,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
46,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
47,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
48,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
49

sự lựa chọn

Giới hạn các giá trị đối với một tập hợp các lựa chọn cụ thể

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
50,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
51 hoặc
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
52

const

Lưu trữ một giá trị không đổi

default

Giá trị mặc định được sử dụng khi không được cung cấp đối số

Mặc định là

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
53

Dest

Chỉ định tên thuộc tính được sử dụng trong không gian tên kết quả

Cứu giúp

Trợ giúp thông điệp cho một đối số

Metavar

Tên hiển thị thay thế cho đối số như được hiển thị trong trợ giúp

nargs

Số lần đối số có thể được sử dụng

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
54,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
55,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
56,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
57 hoặc
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
58

yêu cầu

Cho biết liệu một đối số là bắt buộc hay tùy chọn

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
59 hoặc
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
60

loại hình

Tự động chuyển đổi một đối số thành loại đã cho

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
54,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
62,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
63 hoặc chức năng có thể gọi được

Thí dụ¶

Mã sau đây là một chương trình Python lấy một danh sách các số nguyên và tạo ra tổng hoặc tối đa:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))

Giả sử mã Python ở trên được lưu vào một tệp có tên

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
64, nó có thể được chạy ở dòng lệnh và nó cung cấp các thông báo trợ giúp hữu ích:

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)

Khi chạy với các đối số thích hợp, nó in tổng hoặc tối đa của số nguyên dòng lệnh:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10

Nếu các đối số không hợp lệ được truyền vào, một lỗi sẽ được hiển thị:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
0

Các phần sau đây hướng dẫn bạn qua ví dụ này.

Tạo trình phân tích cú pháp

Bước đầu tiên trong việc sử dụng

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 là tạo đối tượng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
1

Đối tượng

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 sẽ giữ tất cả thông tin cần thiết để phân tích dòng lệnh vào các loại dữ liệu Python.

Thêm đối số

Việc điền vào

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 với thông tin về các đối số chương trình được thực hiện bằng cách thực hiện các cuộc gọi đến phương thức
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69. Nói chung, các cuộc gọi này cho biết
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 làm thế nào để lấy các chuỗi trên dòng lệnh và biến chúng thành các đối tượng. Thông tin này được lưu trữ và sử dụng khi
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 được gọi. Ví dụ:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
2

Sau đó, gọi

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 sẽ trả về một đối tượng với hai thuộc tính,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
73 và
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
74. Thuộc tính
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
73 sẽ là danh sách một hoặc nhiều số nguyên và thuộc tính
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
74 sẽ là hàm
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
77, nếu
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
78 được chỉ định tại dòng lệnh hoặc hàm
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
79 nếu không.

Phân tích đối số

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 Phân tích đối số thông qua phương thức
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71. Điều này sẽ kiểm tra dòng lệnh, chuyển đổi từng đối số thành loại thích hợp và sau đó gọi hành động thích hợp. Trong hầu hết các trường hợp, điều này có nghĩa là một đối tượng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
82 đơn giản sẽ được xây dựng từ các thuộc tính được phân tích từ dòng lệnh:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
3

Trong một tập lệnh,

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 thường sẽ được gọi mà không có đối số và
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 sẽ tự động xác định các đối số dòng lệnh từ
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
35.

Đối tượng ArbutionParser

classArgParse.ArgumentParser (prog = none, use = none, description = none, epilog = none, cha mẹ = [], formatter_class = argparse.helpformatter, prefix_chars = '-', fromfile_prefix_chars , add_help = true, allow_abbrev = true, exit_on_error = true) ¶ argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True)

Tạo một đối tượng

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 mới. Tất cả các tham số nên được truyền làm đối số từ khóa. Mỗi tham số có mô tả chi tiết hơn dưới đây, nhưng tóm lại chúng là:

  • Prog - Tên của chương trình (mặc định:

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    87)

  • Cách sử dụng - Chuỗi mô tả việc sử dụng chương trình (mặc định: được tạo từ các đối số được thêm vào trình phân tích cú pháp)

  • Mô tả - Văn bản để hiển thị trước khi trợ giúp đối số (theo mặc định, không có văn bản)

  • Epilog - văn bản để hiển thị sau khi trợ giúp đối số (theo mặc định, không có văn bản)

  • Cha mẹ - Danh sách các đối tượng

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    66 có đối số cũng nên được đưa vào

  • formatter_class - một lớp để tùy chỉnh đầu ra trợ giúp

  • prefix_chars - tập hợp các ký tự có tiền tố các đối số tùy chọn (mặc định: ‘ -‘)

  • FromFile_Prefix_Chars - tập hợp các ký tự tiền tố mà từ đó các đối số bổ sung sẽ được đọc (mặc định:

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53)

  • argars_default - Giá trị mặc định toàn cầu cho các đối số (mặc định:

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53)

  • xung đột_handler - Chiến lược giải quyết các tùy chọn xung đột (thường là không cần thiết)

  • ADD_HELP - Thêm tùy chọn

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    91 vào trình phân tích cú pháp (mặc định:
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    59)

  • allow_abbrev - cho phép các tùy chọn dài được viết tắt nếu viết tắt là không rõ ràng. (Mặc định:

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    59)

  • EXIT_ON_ERROR - Xác định xem ArgumentParser có thoát ra với thông tin lỗi hay không khi xảy ra lỗi. (Mặc định:

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    59)

Đã thay đổi trong phiên bản 3.5: Tham số allow_abbrev đã được thêm vào.allow_abbrev parameter was added.

Đã thay đổi trong phiên bản 3.8: Trong các phiên bản trước, allow_abbrev cũng vô hiệu hóa nhóm các cờ ngắn như

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
95 có nghĩa là
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
96.In previous versions, allow_abbrev also disabled grouping of short flags such as
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
95 to mean
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
96.

Đã thay đổi trong phiên bản 3.9: tham số EXIT_ON_ERROR đã được thêm vào.exit_on_error parameter was added.

Các phần sau đây mô tả cách mỗi trong số này được sử dụng.

ăn xin¶

Theo mặc định, các đối tượng

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 sử dụng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
98 để xác định cách hiển thị tên của chương trình trong các thông báo trợ giúp. Mặc định này hầu như luôn luôn mong muốn vì nó sẽ làm cho các thông báo trợ giúp khớp với cách chương trình được gọi trên dòng lệnh. Ví dụ: hãy xem xét một tệp có tên
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
99 với mã sau:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
4

Sự trợ giúp cho chương trình này sẽ hiển thị

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
99 dưới dạng tên chương trình (bất kể chương trình được gọi từ đâu):

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
5

Để thay đổi hành vi mặc định này, một giá trị khác có thể được cung cấp bằng đối số

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
01 thành
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
6

Lưu ý rằng tên chương trình, cho dù được xác định từ

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
98 hoặc từ đối số
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
01, có sẵn để giúp các thông báo bằng cách sử dụng trình xác định định dạng
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
05.

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
7

cách sử dụng¶

Theo mặc định,

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 tính toán thông báo sử dụng từ các đối số mà nó chứa:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
8

Thông báo mặc định có thể được ghi đè với đối số từ khóa

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
07:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
9

Trình xác định định dạng

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
05 có sẵn để điền tên chương trình trong thông báo sử dụng của bạn.

sự mô tả¶

Hầu hết các cuộc gọi đến trình xây dựng

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 sẽ sử dụng đối số từ khóa
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
10. Lập luận này đưa ra một mô tả ngắn gọn về những gì chương trình làm và cách thức hoạt động. Trong các thông báo trợ giúp, mô tả được hiển thị giữa chuỗi sử dụng dòng lệnh và các thông báo trợ giúp cho các đối số khác nhau:

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
0

Theo mặc định, mô tả sẽ được bọc dòng để nó phù hợp với không gian đã cho. Để thay đổi hành vi này, hãy xem đối số Formatter_Class.

Epilogn

Một số chương trình muốn hiển thị mô tả bổ sung của chương trình sau khi mô tả các đối số. Văn bản như vậy có thể được chỉ định bằng đối số

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
11 thành
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66:

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
1

Như với đối số mô tả, văn bản

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
11 được bọc theo mặc định, nhưng hành vi này có thể được điều chỉnh bằng đối số formatter_ class thành
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66.

cha mẹ¶

Đôi khi, một số trình phân tích cú pháp chia sẻ một bộ đối số chung. Thay vì lặp lại các định nghĩa của các đối số này, một trình phân tích cú pháp duy nhất với tất cả các đối số được chia sẻ và có thể sử dụng đối số

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
15 cho
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66. Đối số
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
15 lấy một danh sách các đối tượng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66, thu thập tất cả các hành động vị trí và tùy chọn từ chúng và thêm các hành động này vào đối tượng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 được xây dựng:

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
2

Lưu ý rằng hầu hết các trình phân tích cú pháp cha sẽ chỉ định

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
20. Mặt khác,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 sẽ thấy hai tùy chọn
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
91 (một trong cha mẹ và một trong trẻ) và gây ra lỗi.

Ghi chú

Bạn phải khởi tạo hoàn toàn các trình phân tích cú pháp trước khi chuyển chúng qua

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
15. Nếu bạn thay đổi trình phân tích cú pháp cha mẹ sau khi trình phân tích cú pháp con, những thay đổi đó sẽ không được phản ánh ở trẻ.

formatter_ class¶¶

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 Các đối tượng cho phép định dạng trợ giúp được tùy chỉnh bằng cách chỉ định một lớp định dạng thay thế. Hiện tại, có bốn lớp như vậy:

classargparse.rawdescriphelpformatterr classargparse.rawtexthelpformatter¶ classargparse.argumentdefaultshelpformatterrargparse.RawDescriptionHelpFormatterclass argparse.RawTextHelpFormatterclass argparse.ArgumentDefaultsHelpFormatterclass argparse.MetavarTypeHelpFormatter

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
25 và
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
26 cung cấp nhiều quyền kiểm soát hơn về cách hiển thị mô tả văn bản. Theo mặc định,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 Đối tượng đã bao bọc dòng mô tả và văn bản Epilog trong các thông báo trợ giúp dòng lệnh:

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
3

Vượt qua

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
25 như
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
29 chỉ ra rằng mô tả và epilog đã được định dạng chính xác và không nên được bọc dòng:

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
4

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
26 duy trì khoảng trắng cho tất cả các loại văn bản trợ giúp, bao gồm các mô tả đối số. Tuy nhiên, nhiều dòng mới được thay thế bằng một. Nếu bạn muốn bảo quản nhiều dòng trống, hãy thêm khoảng trống giữa các dòng mới.

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
31 Tự động thêm thông tin về các giá trị mặc định vào từng thông báo trợ giúp đối số:

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
5

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
32 sử dụng tên của đối số loại cho mỗi đối số làm tên hiển thị cho các giá trị của nó (thay vì sử dụng định mệnh như định dạng thông thường):

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
6

prefix_chars¶

Hầu hết các tùy chọn dòng lệnh sẽ sử dụng

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
33 làm tiền tố, ví dụ:
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
34. Các trình phân tích cú pháp cần hỗ trợ các ký tự tiền tố khác nhau hoặc bổ sung, ví dụ: Đối với các tùy chọn như
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
35 hoặc
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
36, có thể chỉ định chúng bằng đối số
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
37 cho Trình xây dựng ArmparParser:

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
7

Đối số

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
37 mặc định là
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
39. Cung cấp một tập hợp các ký tự không bao gồm
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
33 sẽ khiến các tùy chọn
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
34 không được phép.

Fromfile_prefix_chars¶

Đôi khi, khi xử lý một danh sách đối số đặc biệt dài, có thể có ý nghĩa để giữ danh sách các đối số trong một tệp thay vì gõ nó vào dòng lệnh. Nếu đối số

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
42 được đưa ra cho hàm tạo
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66, thì các đối số bắt đầu với bất kỳ ký tự được chỉ định nào sẽ được coi là tệp và sẽ được thay thế bằng các đối số mà chúng chứa. Ví dụ:

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
8

Các đối số được đọc từ một tệp theo mặc định là một dòng trên mỗi dòng (nhưng xem thêm

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
44) và được xử lý như thể chúng ở cùng một nơi với đối số tham chiếu tệp gốc trên dòng lệnh. Vì vậy, trong ví dụ trên, biểu thức
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
45 được coi là tương đương với biểu thức
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
46.

Đối số

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
42 mặc định là
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
53, có nghĩa là các đối số sẽ không bao giờ được coi là tham chiếu tệp.

arging_default¶

Nói chung, các mặc định đối số được chỉ định bằng cách chuyển một mặc định cho

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69 hoặc bằng cách gọi các phương thức
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
50 với một tập hợp các cặp giá trị tên cụ thể. Tuy nhiên, đôi khi, có thể hữu ích khi chỉ định một mặc định toàn trình phân tích cú pháp cho các đối số. Điều này có thể được thực hiện bằng cách chuyển đối số từ khóa
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
51 cho
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66. Ví dụ: để triệt tiêu toàn cầu tạo thuộc tính trên các cuộc gọi
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71, chúng tôi cung cấp
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
54:

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
9

cho phép_abbrev¶

Thông thường, khi bạn chuyển một danh sách đối số cho phương thức

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 của
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66, nó sẽ nhận ra chữ viết tắt của các tùy chọn dài.recognizes abbreviations of long options.

Tính năng này có thể bị tắt bằng cách đặt

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
57 thành
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
60:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
0

Mới trong phiên bản 3.5.

xung đột_handler¶

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 Các đối tượng không cho phép hai hành động có cùng chuỗi tùy chọn. Theo mặc định, các đối tượng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 nêu ra một ngoại lệ nếu một nỗ lực được thực hiện để tạo đối số với một chuỗi tùy chọn đã được sử dụng:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
1

Đôi khi (ví dụ: khi sử dụng cha mẹ), có thể hữu ích khi chỉ cần ghi đè bất kỳ đối số cũ nào với cùng một chuỗi tùy chọn. Để có được hành vi này, giá trị

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
61 có thể được cung cấp cho đối số
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
62 của
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
2

Lưu ý rằng các đối tượng

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 chỉ xóa một hành động nếu tất cả các chuỗi tùy chọn của nó bị ghi đè. Vì vậy, trong ví dụ trên, hành động
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
34 cũ được giữ lại dưới dạng hành động
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
66, bởi vì chỉ chuỗi tùy chọn
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
67 đã được ghi đè.

add_help¶

Theo mặc định, các đối tượng ArgentParSer thêm một tùy chọn chỉ hiển thị thông báo trợ giúp của trình phân tích cú pháp. Ví dụ: hãy xem xét một tệp có tên

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
99 chứa mã sau:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
4

Nếu

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
69 hoặc
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
70 được cung cấp tại dòng lệnh, Trợ giúp của ArbutionParser sẽ được in:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
4

Đôi khi, nó có thể hữu ích để vô hiệu hóa việc bổ sung tùy chọn trợ giúp này. Điều này có thể đạt được bằng cách chuyển

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
60 dưới dạng đối số
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
72 cho
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
5

Tùy chọn trợ giúp thường là

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
91. Ngoại lệ cho điều này là nếu
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
37 được chỉ định và không bao gồm
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
33, trong trường hợp đó
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
69 và
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
70 không phải là các tùy chọn hợp lệ. Trong trường hợp này, ký tự đầu tiên trong
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
79 được sử dụng để tiền tố các tùy chọn trợ giúp:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
6

exit_on_error¶

Thông thường, khi bạn chuyển một danh sách đối số không hợp lệ cho phương thức

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 của
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66, nó sẽ thoát với thông tin lỗi.

Nếu người dùng muốn bắt lỗi theo cách thủ công, tính năng này có thể được bật bằng cách đặt

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
82 thành
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
60:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
7

Mới trong phiên bản 3.9.

Phương thức add_argument ()

ArcharchParser.add_argument (nameorflags ... [, hành động] [, nargs] [, const] [, mặc định] [, loại] [, lựa chọn] [, yêu cầu] [, trợ giúp]add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])

Xác định cách một đối số dòng lệnh duy nhất nên được phân tích cú pháp. Mỗi tham số có mô tả chi tiết hơn dưới đây, nhưng tóm lại chúng là:

  • Tên hoặc cờ - tên hoặc danh sách các chuỗi tùy chọn, ví dụ:

    import argparse
    
    parser = argparse.ArgumentParser()
    
    # By default it will fail with multiple arguments.
    parser.add_argument('--default')
    
    # Telling the type to be a list will also fail for multiple arguments,
    # but give incorrect results for a single argument.
    parser.add_argument('--list-type', type=list)
    
    # This will allow you to provide multiple arguments, but you will get
    # a list of lists which is not desired.
    parser.add_argument('--list-type-nargs', type=list, nargs='+')
    
    # This is the correct way to handle accepting multiple arguments.
    # '+' == 1 or more.
    # '*' == 0 or more.
    # '?' == 0 or 1.
    # An int is an explicit number of arguments to accept.
    parser.add_argument('--nargs', nargs='+')
    
    # To make the input integers
    parser.add_argument('--nargs-int-type', nargs='+', type=int)
    
    # An alternate way to accept multiple inputs, but you must
    # provide the flag once per input. Of course, you can use
    # type=int here if you want.
    parser.add_argument('--append-action', action='append')
    
    # To show the results of the given option to screen.
    for _, value in parser.parse_args()._get_kwargs():
        if value is not None:
            print(value)
    
    84 hoặc
    import argparse
    
    parser = argparse.ArgumentParser()
    
    # By default it will fail with multiple arguments.
    parser.add_argument('--default')
    
    # Telling the type to be a list will also fail for multiple arguments,
    # but give incorrect results for a single argument.
    parser.add_argument('--list-type', type=list)
    
    # This will allow you to provide multiple arguments, but you will get
    # a list of lists which is not desired.
    parser.add_argument('--list-type-nargs', type=list, nargs='+')
    
    # This is the correct way to handle accepting multiple arguments.
    # '+' == 1 or more.
    # '*' == 0 or more.
    # '?' == 0 or 1.
    # An int is an explicit number of arguments to accept.
    parser.add_argument('--nargs', nargs='+')
    
    # To make the input integers
    parser.add_argument('--nargs-int-type', nargs='+', type=int)
    
    # An alternate way to accept multiple inputs, but you must
    # provide the flag once per input. Of course, you can use
    # type=int here if you want.
    parser.add_argument('--append-action', action='append')
    
    # To show the results of the given option to screen.
    for _, value in parser.parse_args()._get_kwargs():
        if value is not None:
            print(value)
    
    85.

  • Hành động - Loại hành động cơ bản sẽ được thực hiện khi gặp đối số này tại dòng lệnh.

  • NARGS - Số lượng các đối số dòng lệnh nên được tiêu thụ.

  • Const - một giá trị không đổi được yêu cầu bởi một số lựa chọn hành động và NARGS.

  • Mặc định - giá trị được tạo ra nếu đối số không có trong dòng lệnh và nếu nó không có trong đối tượng không gian tên.

  • Loại - Loại mà đối số dòng lệnh phải được chuyển đổi.

  • Lựa chọn - Một thùng chứa của các giá trị cho phép cho đối số.

  • Yêu cầu - có hay không tùy chọn dòng lệnh có thể được bỏ qua hay không (chỉ có tùy chọn).

  • Trợ giúp - Một mô tả ngắn gọn về những gì đối số làm.

  • Metavar - Tên cho đối số trong các thông báo sử dụng.

  • DEST - Tên của thuộc tính sẽ được thêm vào đối tượng được trả về bởi

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    71.

Các phần sau đây mô tả cách mỗi trong số này được sử dụng.

Tên hoặc Cờ

Phương pháp

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69 phải biết liệu một đối số tùy chọn, như
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
66 hay
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
67 hay đối số vị trí, như một danh sách các tên tệp, được dự kiến. Do đó, các đối số đầu tiên được chuyển cho
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69 phải là một loạt các cờ hoặc một tên đối số đơn giản.

Ví dụ, một đối số tùy chọn có thể được tạo như:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
8

Trong khi một đối số vị trí có thể được tạo như:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
9

Khi

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 được gọi, các đối số tùy chọn sẽ được xác định bởi tiền tố
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
33 và các đối số còn lại sẽ được coi là vị trí:

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
0

hoạt động¶

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 Đối tượng liên kết các đối số dòng lệnh với các hành động. Những hành động này có thể thực hiện bất cứ điều gì với các đối số dòng lệnh được liên kết với chúng, mặc dù hầu hết các hành động chỉ cần thêm một thuộc tính vào đối tượng được trả về bởi
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71. Đối số từ khóa
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
95 Chỉ định cách xử lý các đối số dòng lệnh. Các hành động được cung cấp là:

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    42 - Điều này chỉ lưu trữ giá trị đối số. Đây là hành động mặc định. Ví dụ:

    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    1

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    43 - Điều này lưu trữ giá trị được chỉ định bởi đối số từ khóa Const; Lưu ý rằng đối số từ khóa Const mặc định là
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53. Hành động
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    43 được sử dụng phổ biến nhất với các đối số tùy chọn chỉ định một số loại cờ. Ví dụ:

    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    2

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    44 và
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    01 - Đây là những trường hợp đặc biệt của
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    43 được sử dụng để lưu trữ các giá trị
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    59 và
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    60. Ngoài ra, chúng tạo ra các giá trị mặc định tương ứng là
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    60 và
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    59. Ví dụ:

    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    3

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    45 - Điều này lưu trữ một danh sách và nối mỗi giá trị đối số vào danh sách. Nó rất hữu ích để cho phép một tùy chọn được chỉ định nhiều lần. Nếu giá trị mặc định là không trống, các phần tử mặc định sẽ có trong giá trị phân tích cú pháp cho tùy chọn, với bất kỳ giá trị nào từ dòng lệnh được thêm vào sau các giá trị mặc định đó. Ví dụ sử dụng:

    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    4

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    46 - Điều này lưu trữ một danh sách và nối các giá trị được chỉ định bởi đối số từ khóa Const vào danh sách; Lưu ý rằng đối số từ khóa Const mặc định là
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53. Hành động
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    46 thường hữu ích khi nhiều đối số cần lưu trữ các hằng số vào cùng một danh sách. Ví dụ:

    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    5

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    47 - Điều này đếm số lần đối số từ khóa xảy ra. Ví dụ, điều này rất hữu ích cho việc tăng mức độ dài dòng:

    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    6

    Lưu ý, mặc định sẽ là

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53 trừ khi được đặt rõ ràng thành 0.

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    48 - Điều này in một thông báo trợ giúp hoàn chỉnh cho tất cả các tùy chọn trong trình phân tích cú pháp hiện tại và sau đó thoát. Theo mặc định, một hành động trợ giúp được tự động thêm vào trình phân tích cú pháp. Xem
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    66 để biết chi tiết về cách tạo đầu ra.

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    49 - Điều này mong đợi một đối số từ khóa
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    16 trong cuộc gọi
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    69 và in thông tin phiên bản và thoát khi được gọi:

    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    7

  • $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    18 - Điều này lưu trữ một danh sách và mở rộng từng giá trị đối số cho danh sách. Ví dụ sử dụng:

    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    8

    Mới trong phiên bản 3.8.

Bạn cũng có thể chỉ định một hành động tùy ý bằng cách chuyển một lớp con hành động hoặc đối tượng khác thực hiện cùng một giao diện.

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
19 có sẵn trong
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 và thêm hỗ trợ cho các hành động Boolean như
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
67 và
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
22:

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
9

Mới trong phiên bản 3.9.

Cách được đề xuất để tạo hành động tùy chỉnh là mở rộng

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
23, ghi đè phương thức
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
24 và tùy chọn các phương thức
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
25 và
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
26.

Một ví dụ về một hành động tùy chỉnh:

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
0

Để biết thêm chi tiết, xem

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
23.

nargs¶

Các đối tượng của ArgumpParser thường liên kết một đối số dòng lệnh duy nhất với một hành động duy nhất được thực hiện. Đối số từ khóa

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
28 liên kết một số lượng đối số dòng lệnh khác nhau với một hành động duy nhất. Các giá trị được hỗ trợ là:

  • $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    29 (một số nguyên).
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    29 Đối số từ dòng lệnh sẽ được tập hợp lại với nhau thành một danh sách. Ví dụ:

    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    1

    Lưu ý rằng

    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    31 tạo ra một danh sách một mục. Điều này khác với mặc định, trong đó mặt hàng được sản xuất bởi chính nó.

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    55. Một đối số sẽ được tiêu thụ từ dòng lệnh nếu có thể và được tạo ra như một mục duy nhất. Nếu không có đối số dòng lệnh nào có mặt, giá trị từ mặc định sẽ được tạo ra. Lưu ý rằng đối với các đối số tùy chọn, có một trường hợp bổ sung - chuỗi tùy chọn có mặt nhưng không được theo sau bởi một đối số dòng lệnh. Trong trường hợp này, giá trị từ const sẽ được sản xuất. Một số ví dụ để minh họa điều này:

    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    2

    Một trong những cách sử dụng phổ biến hơn của

    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    33 là cho phép các tệp đầu vào và đầu ra tùy chọn:

    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    3

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    56. Tất cả các đối số dòng lệnh có mặt được thu thập vào một danh sách. Lưu ý rằng thường không có ý nghĩa gì khi có nhiều hơn một đối số vị trí với
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    35, nhưng có thể có nhiều đối số tùy chọn với
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    35. Ví dụ:

    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    4

  • parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    57. Giống như
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    56, tất cả các arg dòng lệnh hiện tại được thu thập vào một danh sách. Ngoài ra, một thông báo lỗi sẽ được tạo nếu có ít nhất một đối số dòng lệnh. Ví dụ:

    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    5

Nếu đối số từ khóa

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
28 không được cung cấp, số lượng đối số được tiêu thụ được xác định bởi hành động. Nói chung, điều này có nghĩa là một đối số dòng lệnh duy nhất sẽ được tiêu thụ và một mục duy nhất (không phải danh sách) sẽ được tạo ra.

hăng sô¶

Đối số

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
40 của
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69 được sử dụng để giữ các giá trị không đổi không được đọc từ dòng lệnh nhưng được yêu cầu cho các hành động
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 khác nhau. Hai cách sử dụng phổ biến nhất của nó là:

  • Khi

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    69 được gọi với
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    44 hoặc
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    45. Các hành động này thêm giá trị
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    40 vào một trong các thuộc tính của đối tượng được trả về bởi
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    71. Xem mô tả hành động cho các ví dụ. Nếu
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    40 không được cung cấp cho
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    69, nó sẽ nhận được giá trị mặc định là
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53.

  • Khi

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    69 được gọi với các chuỗi tùy chọn (như
    import argparse
    
    parser = argparse.ArgumentParser()
    
    # By default it will fail with multiple arguments.
    parser.add_argument('--default')
    
    # Telling the type to be a list will also fail for multiple arguments,
    # but give incorrect results for a single argument.
    parser.add_argument('--list-type', type=list)
    
    # This will allow you to provide multiple arguments, but you will get
    # a list of lists which is not desired.
    parser.add_argument('--list-type-nargs', type=list, nargs='+')
    
    # This is the correct way to handle accepting multiple arguments.
    # '+' == 1 or more.
    # '*' == 0 or more.
    # '?' == 0 or 1.
    # An int is an explicit number of arguments to accept.
    parser.add_argument('--nargs', nargs='+')
    
    # To make the input integers
    parser.add_argument('--nargs-int-type', nargs='+', type=int)
    
    # An alternate way to accept multiple inputs, but you must
    # provide the flag once per input. Of course, you can use
    # type=int here if you want.
    parser.add_argument('--append-action', action='append')
    
    # To show the results of the given option to screen.
    for _, value in parser.parse_args()._get_kwargs():
        if value is not None:
            print(value)
    
    66 hoặc
    import argparse
    
    parser = argparse.ArgumentParser()
    
    # By default it will fail with multiple arguments.
    parser.add_argument('--default')
    
    # Telling the type to be a list will also fail for multiple arguments,
    # but give incorrect results for a single argument.
    parser.add_argument('--list-type', type=list)
    
    # This will allow you to provide multiple arguments, but you will get
    # a list of lists which is not desired.
    parser.add_argument('--list-type-nargs', type=list, nargs='+')
    
    # This is the correct way to handle accepting multiple arguments.
    # '+' == 1 or more.
    # '*' == 0 or more.
    # '?' == 0 or 1.
    # An int is an explicit number of arguments to accept.
    parser.add_argument('--nargs', nargs='+')
    
    # To make the input integers
    parser.add_argument('--nargs-int-type', nargs='+', type=int)
    
    # An alternate way to accept multiple inputs, but you must
    # provide the flag once per input. Of course, you can use
    # type=int here if you want.
    parser.add_argument('--append-action', action='append')
    
    # To show the results of the given option to screen.
    for _, value in parser.parse_args()._get_kwargs():
        if value is not None:
            print(value)
    
    67) và
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    33. Điều này tạo ra một đối số tùy chọn có thể được theo sau bởi 0 hoặc một đối số dòng lệnh. Khi phân tích các dòng lệnh, nếu chuỗi tùy chọn gặp phải không có đối số dòng lệnh theo nó, giá trị của
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    40 sẽ được coi là
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53 thay thế. Xem mô tả NARGS cho các ví dụ.

Đã thay đổi trong phiên bản 3.11:

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
57 theo mặc định, bao gồm cả khi
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
45 hoặc
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
44.
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
57 by default, including when
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
45 or
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
44.

mặc định¶

Tất cả các đối số tùy chọn và một số đối số vị trí có thể được bỏ qua tại dòng lệnh. Đối số từ khóa

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
60 của
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69, có giá trị mặc định là
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
53, chỉ định giá trị nào nên được sử dụng nếu không có đối số dòng lệnh. Đối với các đối số tùy chọn, giá trị
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
60 được sử dụng khi chuỗi tùy chọn không có ở dòng lệnh:

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
6

Nếu không gian tên đích đã có một bộ thuộc tính, mặc định hành động sẽ không ghi được quá nhiều:

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
7

Nếu giá trị

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
60 là một chuỗi, trình phân tích cú pháp phân tích giá trị như thể nó là đối số dòng lệnh. Cụ thể, trình phân tích cú pháp áp dụng bất kỳ đối số chuyển đổi loại nào, nếu được cung cấp, trước khi đặt thuộc tính trên giá trị trả về
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
82. Mặt khác, trình phân tích cú pháp sử dụng giá trị như là:

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
8

Đối với các đối số vị trí với NARG bằng

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
66 hoặc
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
67, giá trị
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
60 được sử dụng khi không có đối số dòng lệnh nào:

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
9

Cung cấp

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
69 khiến không có thuộc tính nào được thêm vào nếu đối số dòng lệnh không có mặt:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)
0

loại hình¶

Theo mặc định, trình phân tích cú pháp đọc các đối số dòng lệnh trong các chuỗi đơn giản. Tuy nhiên, khá thường thì chuỗi dòng lệnh thay vào đó nên được hiểu là một loại khác, chẳng hạn như

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
62 hoặc
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
54. Từ khóa
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
72 cho
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69 cho phép thực hiện mọi chuyển đổi kiểm tra loại và kiểm tra loại cần thiết.

Nếu từ khóa loại được sử dụng với từ khóa mặc định, bộ chuyển đổi loại chỉ được áp dụng nếu mặc định là một chuỗi.

Đối số của

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
72 có thể là bất kỳ cuộc gọi nào có thể gọi được chấp nhận một chuỗi. Nếu hàm tăng
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
75,
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
76 hoặc
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
77, ngoại lệ sẽ bị bắt và thông báo lỗi được định dạng độc đáo được hiển thị. Không có loại ngoại lệ khác được xử lý.

Các loại và chức năng tích hợp phổ biến có thể được sử dụng làm bộ chuyển đổi loại:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)
1

Các chức năng do người dùng xác định cũng có thể được sử dụng:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)
2

Hàm

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
78 không được khuyến nghị làm bộ chuyển đổi loại. Tất cả những gì nó làm là chuyển đổi các chuỗi trống thành
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
60 và các chuỗi không trống thành
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
59. Đây thường không phải là những gì mong muốn.

Nói chung, từ khóa

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
72 là một sự tiện lợi chỉ nên được sử dụng cho các chuyển đổi đơn giản chỉ có thể tăng một trong ba ngoại lệ được hỗ trợ. Bất cứ điều gì có xử lý lỗi hoặc quản lý tài nguyên thú vị hơn nên được thực hiện ở hạ lưu sau khi các đối số được phân tích cú pháp.

Ví dụ, chuyển đổi JSON hoặc YAML có các trường hợp lỗi phức tạp yêu cầu báo cáo tốt hơn so với từ khóa

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
72. Một
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
83 sẽ không được định dạng tốt và ngoại lệ
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
84 sẽ không được xử lý.

Ngay cả

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
85 cũng có những hạn chế để sử dụng với từ khóa
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
72. Nếu một đối số sử dụng FileType và sau đó một đối số tiếp theo không thành công, một lỗi được báo cáo nhưng tệp không được tự động đóng. Trong trường hợp này, sẽ tốt hơn để đợi cho đến khi trình phân tích cú pháp đã chạy và sau đó sử dụng ________ 387-statement để quản lý các tệp.

Đối với trình kiểm tra loại chỉ cần kiểm tra đối với một tập hợp các giá trị cố định, hãy xem xét sử dụng từ khóa lựa chọn thay thế.

Lựa chọn lor

Một số đối số dòng lệnh nên được chọn từ một tập hợp các giá trị bị hạn chế. Chúng có thể được xử lý bằng cách chuyển một đối tượng container làm đối số từ khóa lựa chọn cho

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69. Khi dòng lệnh được phân tích cú pháp, các giá trị đối số sẽ được kiểm tra và thông báo lỗi sẽ được hiển thị nếu đối số không phải là một trong những giá trị chấp nhận được:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)
3

Lưu ý rằng việc đưa vào thùng chứa lựa chọn được kiểm tra sau khi bất kỳ chuyển đổi loại nào đã được thực hiện, do đó, loại đối tượng trong thùng chứa lựa chọn phải khớp với loại được chỉ định:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)
4

Bất kỳ container nào cũng có thể được truyền dưới dạng giá trị lựa chọn, do đó, các đối tượng

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
89, đối tượng
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
90 và các thùng chứa tùy chỉnh đều được hỗ trợ.

Việc sử dụng

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
91 không được khuyến nghị vì rất khó kiểm soát sự xuất hiện của nó trong việc sử dụng, trợ giúp và thông báo lỗi.

Các lựa chọn được định dạng ghi đè Metavar mặc định thường có nguồn gốc từ Dest. Đây thường là những gì bạn muốn vì người dùng không bao giờ nhìn thấy tham số Dests. Nếu màn hình này không phải là mong muốn (có lẽ vì có nhiều lựa chọn), chỉ cần chỉ định một Metavar rõ ràng.

yêu cầu¶

Nói chung, mô -đun

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 giả định rằng các cờ như
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
66 và
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
94 chỉ ra các đối số tùy chọn, luôn có thể được bỏ qua ở dòng lệnh. Để thực hiện một tùy chọn yêu cầu,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
59 có thể được chỉ định cho đối số từ khóa
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
96 thành
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)
5

Như ví dụ cho thấy, nếu một tùy chọn được đánh dấu là

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
98,
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 sẽ báo cáo lỗi nếu tùy chọn đó không có ở dòng lệnh.

Ghi chú

Các tùy chọn cần thiết thường được coi là hình thức xấu vì người dùng mong đợi các tùy chọn là tùy chọn và do đó chúng nên tránh khi có thể.

Cứu giúp¶

Giá trị

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
00 là một chuỗi chứa một mô tả ngắn gọn về đối số. Khi người dùng yêu cầu trợ giúp (thường là bằng cách sử dụng
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
69 hoặc
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
70 tại dòng lệnh), các mô tả
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
00 này sẽ được hiển thị với mỗi đối số:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)
6

Các chuỗi

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
00 có thể bao gồm các định dạng định dạng khác nhau để tránh lặp lại những thứ như tên chương trình hoặc mặc định đối số. Các nhà xác định có sẵn bao gồm tên chương trình,
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
05 và hầu hết các đối số từ khóa đến
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69, ví dụ:
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
07,
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
08, vv .:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)
7

Khi chuỗi trợ giúp hỗ trợ %-formatting, nếu bạn muốn một

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
09 theo nghĩa đen xuất hiện trong chuỗi trợ giúp, bạn phải thoát nó dưới dạng
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
10.

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 Hỗ trợ làm im lặng mục trợ giúp cho một số tùy chọn nhất định, bằng cách đặt giá trị
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
00 thành
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
13:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)
8

Metavar¶

Khi

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 tạo tin nhắn trợ giúp, nó cần một số cách để đề cập đến từng đối số dự kiến. Theo mặc định, các đối tượng ArgentParSer sử dụng giá trị định mệnh làm tên tên của mỗi đối tượng. Theo mặc định, đối với các hành động đối số vị trí, giá trị định mệnh được sử dụng trực tiếp và đối với các hành động đối số tùy chọn, giá trị định mệnh được sử dụng trên đường. Vì vậy, một đối số vị trí duy nhất với
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
15 sẽ được gọi là
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
16. Một đối số tùy chọn duy nhất
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
67 nên được theo sau bởi một đối số dòng lệnh duy nhất sẽ được gọi là
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
18. Một ví dụ:

args = parser.parse_args()
print(args.filename, args.count, args.verbose)
9

Một tên thay thế có thể được chỉ định với

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
19:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))
0

Lưu ý rằng

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
19 chỉ thay đổi tên được hiển thị - tên của thuộc tính trên đối tượng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 vẫn được xác định bởi giá trị định mệnh.

Các giá trị khác nhau của

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
28 có thể khiến metavar được sử dụng nhiều lần. Cung cấp một tuple cho
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
19 Chỉ định một màn hình khác nhau cho mỗi đối số:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))
1

định mệnh

Hầu hết các hành động

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 Thêm một số giá trị dưới dạng thuộc tính của đối tượng được trả về bởi
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71. Tên của thuộc tính này được xác định bởi đối số từ khóa
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
26 của
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69. Đối với các hành động đối số vị trí,
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
26 thường được cung cấp như là đối số đầu tiên cho
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))
2

Đối với các hành động đối số tùy chọn, giá trị của

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
26 thường được suy ra từ các chuỗi tùy chọn.
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 tạo ra giá trị của
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
26 bằng cách lấy chuỗi tùy chọn dài đầu tiên và loại bỏ chuỗi
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
33 ban đầu. Nếu không có chuỗi tùy chọn dài nào được cung cấp,
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
26 sẽ được lấy từ chuỗi tùy chọn ngắn đầu tiên bằng cách tước ký tự
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
33 ban đầu. Bất kỳ ký tự
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
33 nào sẽ được chuyển đổi thành các ký tự
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
37 để đảm bảo chuỗi là tên thuộc tính hợp lệ. Các ví dụ dưới đây minh họa hành vi này:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))
3

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
26 cho phép một tên thuộc tính tùy chỉnh được cung cấp:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))
4

Các lớp hành động

Các lớp hành động thực hiện API hành động, một cuộc gọi có thể gọi được trả về một cuộc gọi có thể gọi được xử lý các đối số từ dòng lệnh. Bất kỳ đối tượng nào theo API này có thể được truyền dưới dạng tham số

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
95 đến
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69.

classargparse.action (tùy chọn_strings, dest, nargs = none, const = none, mặc định = không, loại = không, lựa chọn = none, bắt buộc = falseargparse.Action(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)

Các đối tượng hành động được sử dụng bởi một archarchParser để thể hiện thông tin cần thiết để phân tích một đối số duy nhất từ ​​một hoặc nhiều chuỗi từ dòng lệnh. Lớp hành động phải chấp nhận hai đối số vị trí cộng với bất kỳ đối số từ khóa nào được chuyển đến

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
39 ngoại trừ chính
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
95.

Các phiên bản hành động (hoặc giá trị trả về của bất kỳ cuộc gọi nào có thể gọi được cho tham số

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
95) nên có các thuộc tính của Dest Dest, Tùy chọn_Strings Cách dễ nhất để đảm bảo các thuộc tính này được xác định là gọi
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
44.

Các trường hợp hành động phải được gọi, vì vậy các lớp con phải ghi đè phương thức

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
24, sẽ chấp nhận bốn tham số:

  • parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    46 - Đối tượng ArgentParser chứa hành động này.

  • parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    47 - Đối tượng
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    82 sẽ được trả về bởi
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    71. Hầu hết các hành động thêm một thuộc tính cho đối tượng này bằng cách sử dụng
    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    50.

  • parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    51 - Các đối số dòng lệnh liên quan, với bất kỳ chuyển đổi loại nào được áp dụng. Chuyển đổi loại được chỉ định với đối số từ khóa loại thành
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    69.

  • parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    53 - Chuỗi tùy chọn được sử dụng để gọi hành động này. Đối số
    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    53 là tùy chọn và sẽ vắng mặt nếu hành động được liên kết với một đối số vị trí.

Phương thức

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
24 có thể thực hiện các hành động tùy ý, nhưng thường sẽ đặt các thuộc tính trên
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
47 dựa trên
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
26 và
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
51.

Các lớp con hành động có thể xác định phương thức

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
26 không có đối số và trả về một chuỗi sẽ được sử dụng khi in cách sử dụng chương trình. Nếu phương thức đó không được cung cấp, một mặc định hợp lý sẽ được sử dụng.

Phương thức parse_args ()

ArcharchParser.parse_args (args = none, không gian tên = none) ¶parse_args(args=None, namespace=None)

Chuyển đổi chuỗi đối số thành các đối tượng và gán chúng là thuộc tính của không gian tên. Trả lại không gian tên đông dân.

Các cuộc gọi trước đây đến

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69 Xác định chính xác những đối tượng nào được tạo và cách chúng được gán. Xem tài liệu cho
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69 để biết chi tiết.

  • Args - Danh sách các chuỗi để phân tích cú pháp. Mặc định được lấy từ

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    35.

  • Không gian tên - một đối tượng để lấy các thuộc tính. Mặc định là một đối tượng

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    82 trống mới.

Giá trị tùy chọn Cú pháp

Phương thức

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 hỗ trợ một số cách chỉ định giá trị của một tùy chọn (nếu nó có một). Trong trường hợp đơn giản nhất, tùy chọn và giá trị của nó được truyền dưới dạng hai đối số riêng biệt:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))
5

Đối với các tùy chọn dài (các tùy chọn có tên dài hơn một ký tự), tùy chọn và giá trị cũng có thể được truyền dưới dạng đối số dòng lệnh duy nhất, sử dụng

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
65 để tách chúng ra:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))
6

Đối với các tùy chọn ngắn (các tùy chọn chỉ dài một ký tự), tùy chọn và giá trị của nó có thể được nối:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))
7

Một số tùy chọn ngắn có thể được kết hợp với nhau, chỉ sử dụng một tiền tố

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
33 duy nhất, miễn là chỉ có tùy chọn cuối cùng (hoặc không ai trong số chúng) yêu cầu một giá trị:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))
8

Đối số không hợp lệ¶

Trong khi phân tích các dòng lệnh,

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 kiểm tra nhiều lỗi khác nhau, bao gồm các tùy chọn mơ hồ, các loại không hợp lệ, các tùy chọn không hợp lệ, số lượng đối số vị trí sai, v.v. :

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))
9

Đối số chứa ________ 233¶

Phương pháp

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 cố gắng đưa ra lỗi bất cứ khi nào người dùng rõ ràng đã phạm sai lầm, nhưng một số tình huống vốn đã mơ hồ. Ví dụ: đối số dòng lệnh
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
70 có thể là một nỗ lực để chỉ định một tùy chọn hoặc cố gắng cung cấp một đối số vị trí. Phương pháp
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 rất thận trọng ở đây: Các đối số vị trí chỉ có thể bắt đầu bằng
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
33 nếu chúng trông giống như số âm và không có tùy chọn nào trong trình phân tích cú pháp trông giống như số âm:

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)
0

Nếu bạn có các đối số vị trí phải bắt đầu bằng

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
33 và don lồng trông giống như các số âm, bạn có thể chèn các đối số giả
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
74 cho biết
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 rằng mọi thứ sau đó là một đối số vị trí:

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)
1

Các chữ viết tắt đối số (khớp tiền tố) ¶

Phương pháp

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 theo mặc định cho phép các tùy chọn dài được viết tắt là tiền tố, nếu viết tắt không rõ ràng (tiền tố phù hợp với tùy chọn duy nhất):by default allows long options to be abbreviated to a prefix, if the abbreviation is unambiguous (the prefix matches a unique option):

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)
2

Một lỗi được tạo ra cho các đối số có thể tạo ra nhiều tùy chọn. Tính năng này có thể được vô hiệu hóa bằng cách cài đặt allow_abbrev thành

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
60.allow_abbrev to
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
60.

Ngoài ________ 135¶

Đôi khi có thể hữu ích khi có một đối số phân tích phân tích khác với các đối số của

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
35. Điều này có thể được thực hiện bằng cách chuyển một danh sách các chuỗi cho
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71. Điều này rất hữu ích để kiểm tra tại dấu nhắc tương tác:

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)
3

Đối tượng không gian tên

classargparse.namespace¶argparse.Namespace

Lớp đơn giản được sử dụng theo mặc định bởi

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 để tạo các thuộc tính giữ đối tượng và trả về nó.

Lớp này có chủ ý đơn giản, chỉ là một lớp con

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
82 với một biểu diễn chuỗi có thể đọc được. Nếu bạn thích có chế độ xem giống như các thuộc tính, bạn có thể sử dụng thành ngữ Python tiêu chuẩn,
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
83:

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)
4

Cũng có thể hữu ích khi có một gán

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 gán cho một đối tượng đã có, thay vì một đối tượng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
82 mới. Điều này có thể đạt được bằng cách chỉ định đối số từ khóa
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
86:

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)
5

Các tiện ích khác

Chỉ huy phụ công

ArcharchParser.add_subparsers ([Tiêu đề] [, Mô tả] [, Prog] [, Parser_Class] [, Action] [, Tùy chọn_Strings] [, Dest] [, Yêu cầu] [, Help] [, Metavar]) ¶add_subparsers([title][, description][, prog][, parser_class][, action][, option_strings][, dest][, required][, help][, metavar])

Nhiều chương trình chia chức năng của chúng thành một số lệnh phụ, ví dụ, chương trình

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
87 có thể gọi các lệnh phụ như
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
88,
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
89 và
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
90. Chia tách chức năng theo cách này có thể là một ý tưởng đặc biệt tốt khi một chương trình thực hiện một số chức năng khác nhau yêu cầu các loại đối số dòng lệnh khác nhau.
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 hỗ trợ việc tạo ra các lệnh phụ như vậy bằng phương pháp
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
92. Phương thức
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
92 thường được gọi là không có đối số và trả về một đối tượng hành động đặc biệt. Đối tượng này có một phương thức duy nhất,
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
94, lấy tên lệnh và bất kỳ đối số trình xây dựng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 nào và trả về một đối tượng
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 có thể được sửa đổi như bình thường.

Mô tả các tham số:

  • Tiêu đề - Tiêu đề cho nhóm phân tích phụ trong đầu ra trợ giúp; Theo mặc định, các tiểu ban, "nếu mô tả được cung cấp, nếu không thì sử dụng tiêu đề cho các đối số vị trí

  • Mô tả - Mô tả cho nhóm phân tích phụ trong đầu ra trợ giúp, theo mặc định

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53

  • Prog - Thông tin sử dụng sẽ được hiển thị với Trợ giúp phụ, theo mặc định, tên của chương trình và bất kỳ đối số vị trí nào trước đối số phụ

  • Parser_Class - Lớp sẽ được sử dụng để tạo các phiên bản phân tích phụ, theo mặc định, lớp của trình phân tích cú pháp hiện tại (ví dụ: ArgumentParSer)

  • Hành động - Loại hành động cơ bản sẽ được thực hiện khi gặp đối số này ở dòng lệnh

  • Dest - Tên của thuộc tính theo đó tên phụ sẽ được lưu trữ; theo mặc định

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53 và không có giá trị nào được lưu trữ

  • Yêu cầu - Có hay không một tiểu ban phải được cung cấp, theo mặc định

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    60 (được thêm vào trong 3.7)

  • Trợ giúp - Trợ giúp cho nhóm phân tích phụ trong đầu ra trợ giúp, theo mặc định

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53

  • Metavar - Chuỗi trình bày các lệnh phụ có sẵn trong trợ giúp; Theo mặc định, đó là

    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    53 và trình bày các lệnh phụ ở dạng {cmd1, cmd2, ..}

Một số ví dụ sử dụng:

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)
6

Lưu ý rằng đối tượng được trả về bởi

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 sẽ chỉ chứa các thuộc tính cho trình phân tích cú pháp chính và trình con được chọn bởi dòng lệnh (chứ không phải bất kỳ trình nào khác). Vì vậy, trong ví dụ trên, khi lệnh
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
03 được chỉ định, chỉ có các thuộc tính
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
84 và
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
16 và khi lệnh
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
06 được chỉ định, chỉ có các thuộc tính
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
84 và
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
08.

Tương tự, khi một tin nhắn trợ giúp được yêu cầu từ trình con, chỉ có sự trợ giúp cho trình phân tích cú pháp cụ thể đó mới được in. Thông điệp trợ giúp sẽ không bao gồm trình phân tích cú pháp phụ huynh hoặc trình phân tích cú pháp anh chị em. .

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)
7

Phương pháp

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
92 cũng hỗ trợ các đối số từ khóa
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
12 và
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
13. Khi có mặt, các lệnh Subparser sẽ xuất hiện trong nhóm của riêng họ trong đầu ra trợ giúp. Ví dụ:

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)
8

Hơn nữa,

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
14 hỗ trợ một đối số
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
15 bổ sung, cho phép nhiều chuỗi tham khảo cùng một trình tự phụ. Ví dụ này, như
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
87, bí danh
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
17 như một tốc ký cho
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
18:

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

options:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)
9

Một cách đặc biệt hiệu quả để xử lý các lệnh phụ là kết hợp việc sử dụng phương thức

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
92 với các cuộc gọi đến
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
50 để mỗi người con biết chức năng Python nào nên thực hiện. Ví dụ:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
0

Bằng cách này, bạn có thể để

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 thực hiện công việc gọi chức năng thích hợp sau khi phân tích đối số hoàn tất. Liên kết các chức năng với các hành động như thế này thường là cách dễ nhất để xử lý các hành động khác nhau cho mỗi người phụ của bạn. Tuy nhiên, nếu cần phải kiểm tra tên của trình con được gọi, đối số từ khóa
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
26 cho cuộc gọi
parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
92 sẽ hoạt động:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
1

Thay đổi trong phiên bản 3.7: Đối số từ khóa bắt buộc mới.New required keyword argument.

Đối tượng Filetype

classargparse.filetype (mode = 'r', bufsize = -1, expoding = none, error = none) ¶ argparse.FileType(mode='r', bufsize=- 1, encoding=None, errors=None)

Nhà máy

$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
85 tạo ra các đối tượng có thể được chuyển đến đối số loại
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
39. Các đối số có các đối tượng
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
85 là loại của chúng sẽ mở các đối số dòng lệnh dưới dạng các tệp với các chế độ được yêu cầu, kích thước bộ đệm, mã hóa và xử lý lỗi (xem hàm
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
27 để biết thêm chi tiết):

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
2

Các đối tượng FileType hiểu đối số giả

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
39 và tự động chuyển đổi nó thành
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
29 cho các đối tượng
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
85 có thể đọc được và
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
31 cho các đối tượng
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567

$ # Quotes won't help here... 
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
85 có thể ghi:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
3

Mới trong phiên bản 3.4: Các đối số từ khóa mã hóa và lỗi.The encodings and errors keyword arguments.

Nhóm tranh luận

ArcharchParser.add_argument_group (title = none, description = none) ¶add_argument_group(title=None, description=None)

Theo mặc định,

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 Các đối số dòng lệnh của các nhóm thành các đối số vị trí của Cameron và các đối số tùy chọn, khi hiển thị các thông báo trợ giúp. Khi có một nhóm đối số khái niệm tốt hơn so với mặc định này, các nhóm thích hợp có thể được tạo bằng phương pháp
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
34:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
4

Phương thức

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
34 trả về một đối tượng nhóm đối số có phương thức
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69 giống như
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 thông thường. Khi một đối số được thêm vào nhóm, trình phân tích cú pháp đối xử với nó giống như một đối số bình thường, nhưng hiển thị đối số trong một nhóm riêng cho các tin nhắn trợ giúp. Phương thức
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
34 chấp nhận các đối số tiêu đề và mô tả có thể được sử dụng để tùy chỉnh màn hình này:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
5

Lưu ý rằng bất kỳ đối số nào không có trong các nhóm do người dùng định nghĩa sẽ kết thúc trong các phần đối số vị trí thông thường của các phần và các đối số tùy chọn.

Đã thay đổi trong phiên bản 3.11: Gọi

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
34 trên một nhóm đối số bị phản đối. Tính năng này không bao giờ được hỗ trợ và không phải lúc nào cũng hoạt động chính xác. Chức năng tồn tại trên API do tai nạn thông qua kế thừa và sẽ bị xóa trong tương lai.Calling
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
34 on an argument group is deprecated. This feature was never supported and does not always work correctly. The function exists on the API by accident through inheritance and will be removed in the future.

Loại trừ lẫn nhau¶

ArcharchParser.add_mutally_exclusive_group (bắt buộc = false) ¶add_mutually_exclusive_group(required=False)

Tạo một nhóm loại trừ lẫn nhau.

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 sẽ đảm bảo rằng chỉ có một trong các đối số trong nhóm loại trừ lẫn nhau có mặt trên dòng lệnh:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
6

Phương pháp

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
41 cũng chấp nhận một đối số cần thiết, để chỉ ra rằng ít nhất một trong các đối số loại trừ lẫn nhau là bắt buộc:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
7

Lưu ý rằng các nhóm đối số loại trừ hiện tại không hỗ trợ tiêu đề và các đối số mô tả của

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
34.

Đã thay đổi trong phiên bản 3.11: Gọi

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
34 hoặc
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
41 trên một nhóm loại trừ lẫn nhau không được chấp nhận. Những tính năng này không bao giờ được hỗ trợ và không phải lúc nào cũng hoạt động chính xác. Các chức năng tồn tại trên API do tai nạn thông qua kế thừa và sẽ bị xóa trong tương lai.Calling
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
34 or
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
41 on a mutually exclusive group is deprecated. These features were never supported and do not always work correctly. The functions exist on the API by accident through inheritance and will be removed in the future.

Người phân tích cú pháp Defaults¶

ArcharchParser.set_defaults (** kwargs) ¶set_defaults(**kwargs)

Hầu hết thời gian, các thuộc tính của đối tượng được trả về bởi

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 sẽ được xác định đầy đủ bằng cách kiểm tra các đối số dòng lệnh và các hành động đối số.
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
50 cho phép một số thuộc tính bổ sung được xác định mà không cần thêm bất kỳ sự kiểm tra nào của dòng lệnh:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
8

Lưu ý rằng các mặc định cấp độ phân tích cú pháp luôn ghi đè mặc định cấp đối số:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
9

Mặc định cấp độ phân tích cú pháp có thể đặc biệt hữu ích khi làm việc với nhiều trình phân tích cú pháp. Xem phương thức

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')
92 để biết ví dụ về loại này.

Archarchparparser.get_default (Dest) ¶get_default(dest)

Nhận giá trị mặc định cho thuộc tính không gian tên, như được đặt bởi

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
69 hoặc bằng
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
50:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
00

In ấn trợ giúp

Trong hầu hết các ứng dụng điển hình,

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 sẽ chăm sóc định dạng và in bất kỳ thông báo sử dụng hoặc lỗi nào. Tuy nhiên, một số phương thức định dạng có sẵn:

ArcharchParser.print_usage (file = none) ¶print_usage(file=None)

In một mô tả ngắn gọn về cách

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 nên được gọi trên dòng lệnh. Nếu tệp là
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
53,
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
31 được giả định.

ArcharchParser.print_help (file = none) ¶print_help(file=None)

In tin nhắn trợ giúp, bao gồm sử dụng chương trình và thông tin về các đối số được đăng ký với

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66. Nếu tệp là
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
53,
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
31 được giả định.

Ngoài ra còn có các biến thể của các phương thức này chỉ cần trả về một chuỗi thay vì in nó:

ArcharchParser.format_usage () ¶format_usage()

Trả về một chuỗi chứa một mô tả ngắn gọn về cách

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66 nên được gọi trên dòng lệnh.

ArcharchParser.format_help () ¶format_help()

Trả về một chuỗi chứa thông báo trợ giúp, bao gồm việc sử dụng chương trình và thông tin về các đối số được đăng ký với

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66.

Phân tích cú pháp một phần

ArcharchParser.parse_known_args (args = none, không gian tên = none) ¶parse_known_args(args=None, namespace=None)

Đôi khi một tập lệnh chỉ có thể phân tích một vài đối số dòng lệnh, chuyển các đối số còn lại cho tập lệnh hoặc chương trình khác. Trong những trường hợp này, phương pháp

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
59 có thể hữu ích. Nó hoạt động giống như
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
71 ngoại trừ việc nó không tạo ra lỗi khi có các đối số bổ sung. Thay vào đó, nó trả về một tuple hai mục chứa không gian tên dân cư và danh sách các chuỗi đối số còn lại.

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
01

Cảnh báo

Các quy tắc khớp tiền tố áp dụng cho

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
59. Trình phân tích cú pháp có thể tiêu thụ một tùy chọn ngay cả khi nó chỉ là tiền tố của một trong những tùy chọn đã biết của nó, thay vì để nó trong danh sách đối số còn lại. rules apply to
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
59. The parser may consume an option even if it’s just a prefix of one of its known options, instead of leaving it in the remaining arguments list.

Tùy chỉnh tệp phân tích cú pháp tệp

ArmentParser.convert_arg_line_to_args (arg_line) ¶convert_arg_line_to_args(arg_line)

Các đối số được đọc từ một tệp (xem đối số từ khóa từfile_prefix_chars đến hàm tạo

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
66) được đọc một đối số trên mỗi dòng.
import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
44 có thể được ghi đè cho việc đọc fancier.

Phương thức này lấy một đối số arg_line là một chuỗi được đọc từ tệp đối số. Nó trả về một danh sách các đối số được phân tích cú pháp từ chuỗi này. Phương thức được gọi là một lần trên mỗi dòng đọc từ tệp đối số, theo thứ tự.

Một sự ghi đè hữu ích của phương pháp này là một loại coi từng từ được phân tách không gian như một đối số. Ví dụ sau đây cho thấy cách làm điều này:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
02

Phương pháp thoát

ArcharchParser.exit (status = 0, message = none) ¶exit(status=0, message=None)

Phương thức này chấm dứt chương trình, thoát với trạng thái được chỉ định và, nếu được đưa ra, nó sẽ in một thông báo trước đó. Người dùng có thể ghi đè phương thức này để xử lý các bước này khác nhau:

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
03

ArcharchParser.Error (tin nhắn) ¶error(message)

Phương pháp này in một thông báo sử dụng bao gồm thông báo đến lỗi tiêu chuẩn và chấm dứt chương trình bằng mã trạng thái là 2.

Phân tích cú pháp xen kẽ

ArcharchParser.parse_intermixed_args (args = none, không gian tên = không)parse_intermixed_args(args=None, namespace=None)ArgumentParser.parse_known_intermixed_args(args=None, namespace=None)

Một số lệnh UNIX cho phép người dùng xen kẽ các đối số tùy chọn với các đối số vị trí. Các phương pháp

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
64 và
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
65 hỗ trợ phong cách phân tích cú pháp này.

Các trình phân tích cú pháp này không hỗ trợ tất cả các tính năng của Argparse và sẽ tăng các ngoại lệ nếu các tính năng không được hỗ trợ được sử dụng. Cụ thể, các nhóm phụ,

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
58 và các nhóm loại trừ lẫn nhau bao gồm cả hai tùy chọn và vị trí không được hỗ trợ.

Ví dụ sau đây cho thấy sự khác biệt giữa

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
59 và
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
64: cái trước trả về
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
69 là các đối số không được phân biệt, trong khi cái sau thu thập tất cả các vị trí vào
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
70.

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
04

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
65 trả về một bộ hai mục chứa không gian tên dân cư và danh sách các chuỗi đối số còn lại.
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
64 gây ra lỗi nếu có bất kỳ chuỗi đối số không được phân bổ còn lại.

Mới trong phiên bản 3.7.

Nâng cấp mã OPTPARE

Ban đầu, mô -đun

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 đã cố gắng duy trì khả năng tương thích với
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
74. Tuy nhiên,
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
74 rất khó để mở rộng một cách minh bạch, đặc biệt là với những thay đổi cần thiết để hỗ trợ các nhà xác định
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
76 mới và thông báo sử dụng tốt hơn. Khi hầu hết mọi thứ trong
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
74 đã được sao chép hoặc được gắn khỉ, nó dường như không còn thực tế khi cố gắng duy trì khả năng tương thích ngược.

Mô -đun

parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33 được cải thiện trên mô -đun thư viện tiêu chuẩn
parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
74 theo một số cách bao gồm:

  • Xử lý đối số vị trí.

  • Hỗ trợ các lệnh phụ.

  • Cho phép các tiền tố tùy chọn thay thế như

    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    80 và
    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    81.

  • Xử lý các đối số theo phong cách không hoặc hoặc một hoặc nhiều hơn.

  • Sản xuất nhiều thông điệp sử dụng nhiều thông tin hơn.

  • Cung cấp một giao diện đơn giản hơn nhiều cho tùy chỉnh

    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    72 và
    import argparse
    
    parser = argparse.ArgumentParser()
    
    # By default it will fail with multiple arguments.
    parser.add_argument('--default')
    
    # Telling the type to be a list will also fail for multiple arguments,
    # but give incorrect results for a single argument.
    parser.add_argument('--list-type', type=list)
    
    # This will allow you to provide multiple arguments, but you will get
    # a list of lists which is not desired.
    parser.add_argument('--list-type-nargs', type=list, nargs='+')
    
    # This is the correct way to handle accepting multiple arguments.
    # '+' == 1 or more.
    # '*' == 0 or more.
    # '?' == 0 or 1.
    # An int is an explicit number of arguments to accept.
    parser.add_argument('--nargs', nargs='+')
    
    # To make the input integers
    parser.add_argument('--nargs-int-type', nargs='+', type=int)
    
    # An alternate way to accept multiple inputs, but you must
    # provide the flag once per input. Of course, you can use
    # type=int here if you want.
    parser.add_argument('--append-action', action='append')
    
    # To show the results of the given option to screen.
    for _, value in parser.parse_args()._get_kwargs():
        if value is not None:
            print(value)
    
    95.

Đường dẫn nâng cấp một phần từ

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag
74 lên
parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
33:

  • Thay thế tất cả các cuộc gọi

    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    86 bằng các cuộc gọi
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    39.

  • Thay thế

    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    88 bằng
    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    89 và thêm các cuộc gọi
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    39 bổ sung cho các đối số vị trí. Hãy nhớ rằng những gì trước đây được gọi là
    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    91, bây giờ trong bối cảnh
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    33 được gọi là
    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    93.

  • Thay thế

    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    94 bằng cách sử dụng
    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    64 thay vì
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    71.

  • Thay thế các hành động gọi lại và đối số từ khóa

    parser.add_argument('filename')           # positional argument
    parser.add_argument('-c', '--count')      # option that takes a value
    parser.add_argument('-v', '--verbose',
                        action='store_true')  # on/off flag
    
    97 bằng các đối số
    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    72 hoặc
    import argparse
    
    parser = argparse.ArgumentParser()
    
    # By default it will fail with multiple arguments.
    parser.add_argument('--default')
    
    # Telling the type to be a list will also fail for multiple arguments,
    # but give incorrect results for a single argument.
    parser.add_argument('--list-type', type=list)
    
    # This will allow you to provide multiple arguments, but you will get
    # a list of lists which is not desired.
    parser.add_argument('--list-type-nargs', type=list, nargs='+')
    
    # This is the correct way to handle accepting multiple arguments.
    # '+' == 1 or more.
    # '*' == 0 or more.
    # '?' == 0 or 1.
    # An int is an explicit number of arguments to accept.
    parser.add_argument('--nargs', nargs='+')
    
    # To make the input integers
    parser.add_argument('--nargs-int-type', nargs='+', type=int)
    
    # An alternate way to accept multiple inputs, but you must
    # provide the flag once per input. Of course, you can use
    # type=int here if you want.
    parser.add_argument('--append-action', action='append')
    
    # To show the results of the given option to screen.
    for _, value in parser.parse_args()._get_kwargs():
        if value is not None:
            print(value)
    
    95.

  • Thay thế tên chuỗi cho các đối số từ khóa

    $ python arg.py --default 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ python arg.py --list-type 1234 2345 3456 4567
    ...
    arg.py: error: unrecognized arguments: 2345 3456 4567
    
    $ # Quotes won't help here... 
    $ python arg.py --list-type "1234 2345 3456 4567"
    ['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
    
    $ python arg.py --list-type-nargs 1234 2345 3456 4567
    [['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
    
    $ python arg.py --nargs 1234 2345 3456 4567
    ['1234', '2345', '3456', '4567']
    
    $ python arg.py --nargs-int-type 1234 2345 3456 4567
    [1234, 2345, 3456, 4567]
    
    $ # Negative numbers are handled perfectly fine out of the box.
    $ python arg.py --nargs-int-type -1234 2345 -3456 4567
    [-1234, 2345, -3456, 4567]
    
    $ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
    ['1234', '2345', '3456', '4567']
    
    72 bằng các đối tượng loại tương ứng (ví dụ: int, float, phức tạp, v.v.).

  • Thay thế

    args = parser.parse_args()
    print(args.filename, args.count, args.verbose)
    
    01 bằng
    parser.add_argument('-l','--list', action='append', help=' Set flag', required=True)
    # Use like:
    # python arg.py -l 1234 -l 2345 -l 3456 -l 4567
    
    82 và
    args = parser.parse_args()
    print(args.filename, args.count, args.verbose)
    
    03 và
    args = parser.parse_args()
    print(args.filename, args.count, args.verbose)
    
    04 bằng
    args = parser.parse_args()
    print(args.filename, args.count, args.verbose)
    
    05.

  • Thay thế các chuỗi bằng các đối số ngầm, chẳng hạn như

    args = parser.parse_args()
    print(args.filename, args.count, args.verbose)
    
    06 hoặc
    args = parser.parse_args()
    print(args.filename, args.count, args.verbose)
    
    07 bằng cú pháp Python tiêu chuẩn để sử dụng từ điển cho các chuỗi định dạng, nghĩa là,
    parser = argparse.ArgumentParser(
                        prog = 'ProgramName',
                        description = 'What the program does',
                        epilog = 'Text at the bottom of help')
    
    07 và
    import argparse
    
    parser = argparse.ArgumentParser()
    
    # By default it will fail with multiple arguments.
    parser.add_argument('--default')
    
    # Telling the type to be a list will also fail for multiple arguments,
    # but give incorrect results for a single argument.
    parser.add_argument('--list-type', type=list)
    
    # This will allow you to provide multiple arguments, but you will get
    # a list of lists which is not desired.
    parser.add_argument('--list-type-nargs', type=list, nargs='+')
    
    # This is the correct way to handle accepting multiple arguments.
    # '+' == 1 or more.
    # '*' == 0 or more.
    # '?' == 0 or 1.
    # An int is an explicit number of arguments to accept.
    parser.add_argument('--nargs', nargs='+')
    
    # To make the input integers
    parser.add_argument('--nargs-int-type', nargs='+', type=int)
    
    # An alternate way to accept multiple inputs, but you must
    # provide the flag once per input. Of course, you can use
    # type=int here if you want.
    parser.add_argument('--append-action', action='append')
    
    # To show the results of the given option to screen.
    for _, value in parser.parse_args()._get_kwargs():
        if value is not None:
            print(value)
    
    05.

  • Thay thế đối số của Trình xây dựng OptionParser

    args = parser.parse_args()
    print(args.filename, args.count, args.verbose)
    
    10 bằng một cuộc gọi đến
    args = parser.parse_args()
    print(args.filename, args.count, args.verbose)
    
    11.

Làm thế nào để Argparse vượt qua các đối số dòng lệnh trong Python?

Sử dụng thư viện Python Argparse có bốn bước:..
Nhập thư viện Python Argparse ..
Tạo trình phân tích cú pháp ..
Thêm đối số tùy chọn và vị trí cho trình phân tích cú pháp ..
Hành hình . parse_args ().

Store_True trong Python là gì?

Tùy chọn Store_True tự động tạo giá trị mặc định là sai.Tương tự như vậy, Store_False sẽ mặc định là đúng khi đối số dòng lệnh không có mặt.Nguồn cho hành vi này là cô đọng và rõ ràng: http://hg.python.org/cpython/file/2.7/lib/argparse.py#l861.automatically creates a default value of False. Likewise, store_false will default to True when the command-line argument is not present. The source for this behavior is succinct and clear: http://hg.python.org/cpython/file/2.7/Lib/argparse.py#l861.

Metavar là gì?

Metavar là tên cho đối số trong các thông báo sử dụng.Dest là tên của thuộc tính sẽ được thêm vào đối tượng.Đối tượng này được trả về bởi parse_args.the name for the argument in usage messages. dest is the name of the attribute to be added to the object. This object is returned by parse_args .

Parse_args trở lại là gì?

parse_args () Trả về hai giá trị: Tùy chọn, một đối tượng chứa các giá trị cho tất cả các tùy chọn của bạn, ví dụ:Nếu "--file" lấy một đối số chuỗi duy nhất, thì các tùy chọn.Tệp sẽ là tên tệp do người dùng cung cấp hoặc không có nếu người dùng không cung cấp tùy chọn đó.options, an object containing values for all of your options— e.g. if "--file" takes a single string argument, then options. file will be the filename supplied by the user, or None if the user did not supply that option.