Hướng dẫn python argparse string argument - đối số chuỗi python argparse

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

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


Mô-đun

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8 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 = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8 sẽ tìm ra cách phân tích các đối số của
$ 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)
00. Mô -đun
>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8 cũng tự động tạo thông báo trợ giúp và sử dụng và các lỗi khi người dùng đưa ra các đối số không hợp lệ.

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

$ 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)
02, nó có thể được chạy ở dòng lệnh và 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, nó sẽ đưa ra một lỗi:

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'

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 = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8 là tạo đối tượ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)
04:

>>> parser = argparse.ArgumentParser(description='Process some integers.')

Đối tượ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)
04 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

$ 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)
04 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
$ 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)
07. Nói chung, các cuộc gọi này cho biết
$ 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)
04 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
$ 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)
09 được gọi. Ví dụ:

>>> 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)')

Sau đó, gọi

$ 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)
09 sẽ trả về một đối tượng với hai thuộc tính,
$ 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)
11 và
$ 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)
12. Thuộc tính
$ 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)
11 sẽ là danh sách một hoặc nhiều INT và thuộc tính
$ 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)
12 sẽ là hà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)
15, nếu
$ 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)
16 được chỉ định ở dòng lệnh hoặc hà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)
17 nếu không.

Phân tích đối số

$ 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)
04 Đối số phân tích cú pháp thông qua phương thứ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)
09. Đ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
$ 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)
20 đơ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.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])

Trong một tập lệnh,

$ 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)
09 thường sẽ được gọi không có đối số và
$ 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)
04 sẽ tự động xác định các đối số dòng lệnh từ
$ 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)
00.

Đối tượng ArbutionParser

Lớp ________ 124 ________ 125 (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) ¶(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

$ 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)
04 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:

    $ 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)
    
    27)

  • 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ố (mặc định: Không có)

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

  • Cha mẹ - Danh sách các đối tượ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)
    
    04 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:

    $ 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)
    
    29)

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

    $ 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)
    
    29)

  • 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

    $ 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)
    
    31 vào trình phân tích cú pháp (mặc định:
    $ 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)
    
    32)

  • 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:

    $ 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)
    
    32)

  • 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:

    $ 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)
    
    32)

Đã 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ư

$ 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)
35 có nghĩa là
$ 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)
36.In previous versions, allow_abbrev also disabled grouping of short flags such as
$ 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)
35 to mean
$ 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)
36.

Đã 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

$ 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)
04 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)
38 để 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
$ 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)
39 với mã sau:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()

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

$ 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)
39 dưới dạng tên chương trình (bất kể chương trình được gọi từ đâu):

$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help

Để 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ố

$ 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)
41 thành
$ 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)
04:

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit

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

$ 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)
38 hoặc từ đối số
$ 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)
41, có sẵn để trợ giúp tin nhắn bằng trình xác định định 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)
45.

$ 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

cách sử dụng¶

Theo mặc định,

$ 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)
04 Tính toán thông báo sử dụng từ các đối số mà nó chứa:

$ 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

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

$ 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)
47:

$ 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

Bộ xác định định 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)
45 có sẵn để điền tên chương trình trong các 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

$ 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)
04 sẽ sử dụng đối số từ khóa
$ 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)
50. 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:

$ 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

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ố

$ 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)
51 thành
$ 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)
04:

$ 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

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

$ 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)
51 đượ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
$ 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)
04.

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ố

$ 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)
55 cho
$ 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)
04. Đối số
$ 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)
55 lấy một danh sách các đối tượng ____104, 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
$ 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)
04 được xây 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)
5

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

$ 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)
60. Mặt khá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)
04 sẽ thấy hai tùy chọn
$ 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)
31 (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

$ 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)
55. 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¶¶

$ 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)
04 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:

Lớp ________ 124 ________ 166¶ Lớp ________ 124 ________ 168¶ Lớp ________ 124 ________ 170¶ Lớp ________ 124 ________ 172¶

$ 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)
73 và
$ 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)
74 Cung cấp nhiều quyền kiểm soát hơn về cách mô tả văn bản được hiển thị. Theo mặc định,
$ 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)
04 Đối tượng Kết xuất dòng mô tả và văn bản Epilog trong các thông báo trợ giúp dòng lệnh:

$ 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

Vượt qua

$ 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)
73 như
$ 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)
77 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:

$ 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

$ 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)
74 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.

$ 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)
79 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ố:

$ 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

$ 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)
80 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):

$ 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

prefix_chars¶

Hầu hết các tùy chọn dòng lệnh sẽ 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)
81 làm tiền tố, 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)
82. 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ư
$ 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)
83 hoặ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)
84, có thể chỉ định chúng bằng đối số
$ 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)
85 cho Trình xây dựng ArmparParser:

$ python prog.py 1 2 3 4
4

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

Đối số

$ 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)
85 mặc định là
$ 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)
87. Cung cấp một tập hợp các ký tự không bao gồ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)
81 sẽ khiến các tùy chọn
$ 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)
82 không được phép.

Fromfile_prefix_chars¶

Đôi khi, ví dụ, 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ố

$ 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)
90 được đưa ra cho hàm tạo
$ 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)
04, 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ụ:

$ python prog.py 1 2 3 4
4

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

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

$ 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)
92) 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
$ 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)
93 được coi là tương đương với biểu thứ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)
94.

Đối số

$ 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)
90 mặc định là
$ 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)
29, 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

$ 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)
07 hoặc bằng cách gọi các phương thứ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)
98 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
$ 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)
99 cho
$ 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)
04. Ví dụ: để tạo ra toàn cầu tạo thuộc tính trên các cuộc gọi ____10109, chúng tôi cung cấp
$ python prog.py 1 2 3 4
4

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

$ python prog.py 1 2 3 4
4

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

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

$ 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)
09 của
$ 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)
04, 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

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
05 thành
$ python prog.py 1 2 3 4
4

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

$ 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.5.

xung đột_handler¶

$ 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)
04 Đối tượng không cho phép hai hành động có cùng một chuỗi tùy chọn. Theo mặc định, các đối tượ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)
04 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 prog.py 1 2 3 4
4

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

Đô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ị

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
09 có thể được cung cấp cho đối số
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
10 của
$ 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)
04:

$ python prog.py 1 2 3 4
4

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

Lưu ý rằng các đối tượ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)
04 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
$ 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)
82 cũ được giữ lại dưới dạng hành động
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
14, bởi vì chỉ chuỗi tùy chọn
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
15 đã đượ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

$ 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)
39 chứa mã sau:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()

Nếu

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
17 hoặc
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
18 được cung cấp tại dòng lệnh, Trợ giúp của ArbutionParser sẽ được in:

$ python prog.py 1 2 3 4
4

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

Đô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

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
06 dưới dạng đối số
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
20 cho
$ 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)
04:

$ python prog.py 1 2 3 4
4

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

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

$ 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)
31. Ngoại lệ cho điều này là nếu
$ 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)
85 được chỉ định và không bao gồ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)
81, trong trường hợp đó
$ python prog.py 1 2 3 4
4

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

$ python prog.py 1 2 3 4 --sum
10
18 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
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
27 được sử dụng để tiền tố các tùy chọn trợ giúp:

$ python prog.py 1 2 3 4
4

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

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

$ 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)
09 của
$ 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)
04, 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

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
30 thành
$ python prog.py 1 2 3 4
4

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

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
0

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

Phương thức add_argument ()

________ 232 ________ 233 (tên hoặc cờ ... [, hành động] [, nargs] [, const] [, mặc định] [, loại] [, lựa chọn] [, yêu cầu] [, trợ giúp](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ụ:

    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    34 hoặc
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    35.

  • 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.

  • 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

    $ 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)
    
    09.

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

$ 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)
07 phải biết liệu một đối số tùy chọn, như
$ python prog.py 1 2 3 4
4

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

$ python prog.py 1 2 3 4 --sum
10
15 hay đối số vị trí, như một danh sách các tên tệp, được mong đợi. Do đó, các đối số đầu tiên được chuyển cho
$ 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)
07 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 prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
1

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

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
2

Khi

$ 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)
09 được gọi, các đối số tùy chọn sẽ được xác định bởi tiền tố
$ 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)
81 và các đối số còn lại sẽ được coi là vị trí:

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
3

hoạt độ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)
04 Đố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ỉ đơn giản là thêm một thuộc tính cho đối tượng được trả về bởi
$ 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)
09. Đối số từ khóa
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
45 Chỉ định cách xử lý các đối số dòng lệnh. Các hành động được cung cấp là:

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    46 - Điều này chỉ lưu trữ giá trị đối số. Đây là hành động mặc định. Ví dụ:

    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    4

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    47 - Điều này lưu trữ giá trị được chỉ định bởi đối số từ khóa Const. Hành động
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    47 đượ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ụ:

    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    5

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    49 và
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    50 - Đây là những trường hợp đặc biệt của
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    47 được sử dụng để lưu trữ các giá 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)
    
    32 và
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    06 tương ứng. Ngoài ra, chúng tạo ra các giá trị mặc định tương ứng là
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    06 và
    $ 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)
    
    32. Ví dụ:

    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    6

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    56 - Đ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. Điều này rất hữu ích để cho phép một tùy chọn được chỉ định nhiều lần. Ví dụ sử dụng:

    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    7

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    57 - Đ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. . Ví dụ:

    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    8

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    60 - Đ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:

    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    9

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

    $ 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)
    
    29 trừ khi được đặt rõ ràng thành 0.

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    62 - Đ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
    $ 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)
    
    04 để biết chi tiết về cách tạo đầu ra.

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    64 - Điều này mong đợi một đối số từ khóa
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    65 trong cuộc gọi
    $ 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)
    
    07 và in thông tin phiên bản và thoát khi được gọi:

    >>> parser = argparse.ArgumentParser(description='Process some integers.')
    
    0

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    67 - Đ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(description='Process some integers.')
    
    1

    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 prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
68 có sẵn trong
>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8 và thêm hỗ trợ cho các hành động Boolean như
$ python prog.py 1 2 3 4
4

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

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

>>> parser = argparse.ArgumentParser(description='Process some integers.')
2

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 prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
72, ghi đè phương thức
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
73 và tùy chọn các phương thức
$ python prog.py 1 2 3 4
4

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

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

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

>>> parser = argparse.ArgumentParser(description='Process some integers.')
3

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

$ python prog.py 1 2 3 4
4

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

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 prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
77 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 prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    78 (một số nguyên).
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    78 Các đố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 = argparse.ArgumentParser(description='Process some integers.')
    
    4

    Lưu ý rằng

    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    80 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ó.

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    81. 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 = argparse.ArgumentParser(description='Process some integers.')
    
    5

    Một trong những cách sử dụng phổ biến hơn của

    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    82 là cho phép các tệp đầu vào và đầu ra tùy chọn:

    >>> parser = argparse.ArgumentParser(description='Process some integers.')
    
    6

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    83. 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 prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    84, nhưng có thể có nhiều đối số tùy chọn với
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    84. Ví dụ:

    >>> parser = argparse.ArgumentParser(description='Process some integers.')
    
    7

  • $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    86. Giống như
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    83, 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 = argparse.ArgumentParser(description='Process some integers.')
    
    8

Nếu đối số từ khóa

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
77 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 prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
89 của
$ 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)
07 đượ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
$ 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)
04 khác nhau. Hai cách sử dụng phổ biến nhất của nó là:

  • Khi

    $ 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)
    
    07 được gọi với
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    93 hoặc
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    94. Các hành động này thêm giá trị
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    89 vào một trong các thuộc tính của đối tượng được trả về bởi
    $ 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)
    
    09. Xem mô tả hành động cho các ví dụ.

  • Khi

    $ 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)
    
    07 được gọi với các chuỗi tùy chọn (như
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    14 hoặc
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    15) và
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    82. Đ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 prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    89 sẽ được giả sử thay thế. Xem mô tả NARGS cho các ví dụ.

Với các hành động

$ python prog.py 1 2 3 4
4

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

$ python prog.py 1 2 3 4 --sum
10
57, đối số từ khóa
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
89 phải được đưa ra. Đối với các hành động khác, nó mặc định là
$ 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)
29.

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 prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
06 của
$ 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)
07, có giá trị mặc định là
$ 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)
29, 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 prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
06 được sử dụng khi chuỗi tùy chọn không có ở dòng lệnh:

>>> parser = argparse.ArgumentParser(description='Process some integers.')
9

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('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)')
0

Nếu giá trị

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
06 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ề
$ 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)
20. Mặt khác, trình phân tích cú pháp sử dụng giá trị như là:

>>> 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)')
1

Đối với các đối số vị trí với NARG bằng

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
12 hoặc
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
13, giá trị
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
06 được sử dụng khi không có đối số dòng lệnh nào có mặt:

>>> 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)')
2

Cung cấp

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
15 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:

>>> 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)')
3

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ư

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
16 hoặc
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
17. Từ khóa
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
18 cho
$ 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)
07 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 prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
18 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 prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
21,
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
22 hoặc
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
23, 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:

>>> 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)')
4

Các chức năng do người dùng xác định cũng có thể được sử dụng:

>>> 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)')
5

Hàm

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
24 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
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
06 và các chuỗi không trống thành
$ 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)
32. Đây thường không phải là những gì mong muốn.

Nói chung, từ khóa

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
18 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 prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
18. Một
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
29 sẽ không được định dạng tốt và ngoại lệ
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
30 sẽ không được xử lý.

Ngay cả

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
31 cũng có những hạn chế để sử dụng với từ khóa
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
18. 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 ________ 333-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

$ 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)
07. 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:

>>> 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)')
6

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:

>>> 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)')
7

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 prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
35, đối tượng
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
36 và các thùng chứa tùy chỉnh đều được hỗ trợ.

Việc sử dụng

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
37 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ừ định mệnh. Đâ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 = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8 giả định rằng các cờ như
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
14 và
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
40 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,
$ 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)
32 có thể được chỉ định cho đối số từ khóa
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
42 thành
$ 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)
07:

>>> 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)')
8

Như ví dụ cho thấy, nếu một tùy chọn được đánh dấu là

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
44,
$ 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)
09 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ị

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
46 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
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
17 hoặc
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
18 tại dòng lệnh), các mô tả
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
46 này sẽ được hiển thị với mỗi đối số:

>>> 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)')
9

Các chuỗi

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
46 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,
$ 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)
45 và hầu hết các đối số từ khóa đến
$ 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)
07, ví dụ:
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
53,
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
54, vv .:

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
0

Khi chuỗi trợ giúp hỗ trợ %-formatting, nếu bạn muốn một

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
55 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
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
56.

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8 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ị
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
46 thành
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
59:

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
1

Metavar¶

Khi

$ 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)
04 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
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
61 sẽ được gọi là
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
62. Một đối số tùy chọn duy nhất
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
15 nên được theo sau bởi một đối số dòng lệnh duy nhất sẽ được gọi là
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
64. Một ví dụ:

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
2

Một tên thay thế có thể được chỉ định với

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
65:

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
3

Lưu ý rằng

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
65 chỉ thay đổi tên được hiển thị - tên của thuộc tính trên đối tượ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)
09 vẫn được xác định bởi giá trị định mệnh.

Các giá trị khác nhau của

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
77 có thể khiến Metavar được sử dụng nhiều lần. Cung cấp một tuple cho
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
65 Chỉ định một màn hình khác nhau cho từng đối số:

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
4

định mệnh

Hầu hết các hành độ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)
04 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
$ 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)
09. Tên của thuộc tính này được xác định bởi đối số từ khóa
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
72 của
$ 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)
07. Đối với các hành động đối số vị trí,
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
72 thường được cung cấp làm đối số đầu tiên cho
$ 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)
07:

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
5

Đối với các hành động đối số tùy chọn, giá trị của

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
72 thường được suy ra từ các chuỗi tùy chọn.
$ 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)
04 tạo ra giá trị của
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
72 bằng cách lấy chuỗi tùy chọn dài đầu tiên và loại bỏ chuỗi
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
79 ban đầu. Nếu không có chuỗi tùy chọn dài nào được cung cấp,
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
72 sẽ được lấy từ chuỗi tùy chọn ngắn đầu tiên bằng cách tước ký tự
$ 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)
81 ban đầu. Bất kỳ ký tự
$ 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)
81 nào sẽ được chuyển đổi thành các ký tự
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
83 để đả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:

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
6

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
72 cho phép một tên thuộc tính tùy chỉnh được cung cấp:

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
7

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ố

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
45 đến
$ 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)
07.

Lớp ________ 124 ________ 388 (Tùy chọn_Strings, Dest, nargs = none, const = none, default = none, type = none(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

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
89 ngoại trừ chính
$ python prog.py 1 2 3 4
4

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

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ố

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
45) 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
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
92.

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 prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
73, cần chấp nhận bốn tham số:

  • $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    94 - Đối tượng ArgentParSer có chứa hành động này.

  • $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    95 - Đối tượ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)
    
    20 sẽ được trả về bởi
    $ 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)
    
    09. 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
    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    98.

  • $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    99 - 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
    $ 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)
    
    07.

  • >>> parser = argparse.ArgumentParser(description='Process some integers.')
    
    01 - Chuỗi tùy chọn được sử dụng để gọi hành động này. Đối số
    >>> parser = argparse.ArgumentParser(description='Process some integers.')
    
    01 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 prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
73 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
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
95 dựa trên
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
72 và
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
99.

Các lớp con hành động có thể xác định phương thức

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
75 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 ()

________ 232 ________ 409 (args = none, không gian tên = không) ¶(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

$ 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)
07 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
$ 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)
07 để 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ừ

    $ 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)
    
    00.

  • 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

    $ 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)
    
    20 trống mới.

Giá trị tùy chọn Cú pháp

Phương thứ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)
09 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:

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
8

Đố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(description='Process some integers.')
15 để tách chúng ra:

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
9

Đố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()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
0

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ố

$ 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)
81, 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()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
1

Đối số không hợp lệ¶

Trong khi phân tích dòng lệnh,

$ 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)
09 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ệ, tùy chọn không hợp lệ, số lượng đối số vị trí sai, v.v. :

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
2

Đối số chứa ________ 181¶

Phương pháp

$ 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)
09 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(description='Process some integers.')
20 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
$ 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)
09 rất thận trọng ở đây: Các đối số vị trí chỉ có thể bắt đầu bằ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)
81 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:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
3

Nếu bạn có các đối số vị trí phải bắt đầu với

$ 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)
81 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(description='Process some integers.')
24 cho biết
$ 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)
09 rằng mọi thứ sau đó là một đối số vị trí:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
4

Các chữ viết tắt đối số (khớp tiền tố) ¶

Phương pháp

$ 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)
09 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):

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
5

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

$ python prog.py 1 2 3 4
4

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

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

Ngoài ________ 100¶

Đô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

$ 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)
00. Đ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
$ 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)
09. Điều này rất hữu ích để kiểm tra tại dấu nhắc tương tác:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
6

Đối tượng không gian tên

Lớp ________ 124 ________ 432¶

Lớp đơn giản được sử dụng theo mặc định bởi

$ 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)
09 để 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(description='Process some integers.')
34 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(description='Process some integers.')
35:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
7

Cũng có thể hữu ích khi có một gán

$ 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)
04 cho một đối tượng đã có, thay vì một đối tượ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)
20 mới. Điều này có thể đạt được bằng cách chỉ định đối số từ khóa
>>> parser = argparse.ArgumentParser(description='Process some integers.')
38:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
8

Các tiện ích khác

Chỉ huy phụ công

A([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(description='Process some integers.')
41 có thể gọi các lệnh phụ như
>>> parser = argparse.ArgumentParser(description='Process some integers.')
42,
>>> parser = argparse.ArgumentParser(description='Process some integers.')
43 và
>>> parser = argparse.ArgumentParser(description='Process some integers.')
44. 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.
$ 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)
04 hỗ trợ việc tạo ra các lệnh phụ như vậy bằng phương pháp
>>> parser = argparse.ArgumentParser(description='Process some integers.')
46. Phương thức
>>> parser = argparse.ArgumentParser(description='Process some integers.')
46 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(description='Process some integers.')
48, lấy tên lệnh và bất kỳ đối số trình xây 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)
04 nào và trả về một đối tượ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)
04 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

    $ 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)
    
    29

  • 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

    $ 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)
    
    29 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

    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    06 (đượ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

    $ 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)
    
    29

  • Metavar - Chuỗi trình bày các lệnh phụ có sẵn trong trợ giúp; Theo mặc định, đó là

    $ 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)
    
    29 và trình bày các lệnh phụ ở dạng {cmd1, cmd2, ..}

Một số ví dụ sử dụng:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
9

Lưu ý rằng đối tượng được trả về bởi

$ 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)
09 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 (và 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 = argparse.ArgumentParser(description='Process some integers.')
57 được chỉ định, chỉ có các thuộc tính
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
34 và
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
62 và khi lệnh
>>> parser = argparse.ArgumentParser(description='Process some integers.')
60 được chỉ định, chỉ có các thuộc tính
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
34 và
>>> parser = argparse.ArgumentParser(description='Process some integers.')
62.

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 myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
0

Phương pháp

>>> parser = argparse.ArgumentParser(description='Process some integers.')
46 cũng hỗ trợ các đối số từ khóa
>>> parser = argparse.ArgumentParser(description='Process some integers.')
66 và
>>> parser = argparse.ArgumentParser(description='Process some integers.')
67. 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 myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
1

Hơn nữa,

>>> parser = argparse.ArgumentParser(description='Process some integers.')
68 hỗ trợ một đối số
>>> parser = argparse.ArgumentParser(description='Process some integers.')
69 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(description='Process some integers.')
41, bí danh
>>> parser = argparse.ArgumentParser(description='Process some integers.')
71 như một tốc ký cho
>>> parser = argparse.ArgumentParser(description='Process some integers.')
72:

$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
2

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(description='Process some integers.')
46 với các cuộc gọi đến
$ 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)
98 để mỗi người con biết chức năng Python nào nên thực hiện. Ví dụ:

$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
3

Bằng cách này, bạn có thể để

$ 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)
09 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
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
72 cho cuộc gọi
>>> parser = argparse.ArgumentParser(description='Process some integers.')
46 sẽ hoạt động:

$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
4

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

Lớp ________ 124 ________ 479 (mode = 'r', bufsize =- 1, mã hóa = none, error = none) ¶(mode='r', bufsize=- 1, encoding=None, errors=None)

Nhà máy

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
31 tạo ra các đối tượng có thể được chuyển đến đối số loại
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
89. Các đối số có các đối tượng
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
31 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 có chế độ yêu cầu, kích thước bộ đệm, mã hóa và xử lý lỗi (xem hàm
>>> parser = argparse.ArgumentParser(description='Process some integers.')
83 để biết thêm chi tiết):

$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
5

Các đối tượng FileType hiểu đối số giả

$ 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)
87 và tự động chuyển đổi nó thành
>>> parser = argparse.ArgumentParser(description='Process some integers.')
85 cho các đối tượng
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
31 có thể đọc được và
>>> parser = argparse.ArgumentParser(description='Process some integers.')
87 cho các đối tượng
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
31 có thể ghi:

$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
6

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

________ 232 ________ 490 (Tiêu đề = Không, Mô tả = Không) ¶(title=None, description=None)

Theo mặc định,

$ 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)
04 Các đối số dòng lệnh của các nhóm thành các đối số vị trí của các đối số và các đối số tùy chọn của Cameron 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 = argparse.ArgumentParser(description='Process some integers.')
92:

$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
7

Phương thức

>>> parser = argparse.ArgumentParser(description='Process some integers.')
92 trả về một đối tượng nhóm đối số có phương thứ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)
07 giống như
$ 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)
04 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 = argparse.ArgumentParser(description='Process some integers.')
92 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 myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
8

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.

Loại trừ lẫn nhau¶

________ 232 ________ 498 (Yêu cầu = Sai) ¶(required=False)

Tạo một nhóm loại trừ lẫn nhau.

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8 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 myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

options:
 -h, --help  show this help message and exit
 --foo FOO   foo help
9

Phương pháp

>>> 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)')
00 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:

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
0

Lưu ý rằng các nhóm đối số loại trừ hiện tại không hỗ trợ tiêu đề và mô tả đối số của

>>> parser = argparse.ArgumentParser(description='Process some integers.')
92.

Người phân tích cú pháp Defaults¶

________ 232 ________ 503 (** kwargs) ¶(**kwargs)

Hầu hết thời gian, các thuộc tính của đối tượng được trả về bởi

$ 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)
09 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ố.
$ 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)
98 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ỳ kiểm tra nào của dòng lệnh:

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
1

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ố:

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
2

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(description='Process some integers.')
46 để biết ví dụ về loại này.

________ 232 ________ 508 (Dest) ¶(dest)

Nhận giá trị mặc định cho thuộc tính không gian tên, như được đặt bởi

$ 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)
07 hoặc bằ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)
98:

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
3

In ấn trợ giúp

Trong hầu hết các ứng dụng điển hình,

$ 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)
09 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:

________ 232 ________ 513 (Tệp = Không) ¶(file=None)

In một mô tả ngắn gọn về cá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)
04 nên được gọi trên dòng lệnh. Nếu tệp là
$ 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)
29,
>>> parser = argparse.ArgumentParser(description='Process some integers.')
87 được giả định.

________ 232 ________ 518 (File = none) ¶(file=None)

In một 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

$ 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)
04. Nếu tệp là
$ 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)
29,
>>> parser = argparse.ArgumentParser(description='Process some integers.')
87 đượ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ó:

________ 232 ________ 523 ()()

Trả về một chuỗi chứa một mô tả ngắn gọn về cá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)
04 nên được gọi trên dòng lệnh.

________ 232 ________ 526 ()()

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

$ 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)
04.

Phân tích cú pháp một phần

________ 232 ________ 529 (args = none, không gian tên = không) ¶(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('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)')
30 có thể hữu ích. Nó hoạt động giống như
$ 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)
09 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 = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
4

Cảnh báo

Các quy tắc phù hợp với tiền tố áp dụng cho

>>> 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)')
30. 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('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)')
30. 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

________ 232 ________ 534 (arg_line) ¶(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

$ 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)
04) được đọc một đối số trên mỗi 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)
92 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 = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
5

Phương pháp thoát

________ 232 ________ 538 (status = 0, message = none) ¶(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 = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
6

________ 232 ________ 540 (tin nhắn) ¶(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ẽ

________ 232 ________ 542 (args = none, không gian tên = không)(args=None, namespace=None)
$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10
32
>>> 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)')
44(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('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)')
45 và
>>> 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)')
46 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('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)')
47 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('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)')
30 và
>>> 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)')
45: cái trước trả về
>>> 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)')
50 là các đối số không được phân chia, trong khi cái sau thu thập tất cả các vị trí thành
>>> 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)')
51.

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
7

>>> 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)')
46 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('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)')
45 gây ra lỗi nếu có bất kỳ chuỗi đối số không được nhìn nhận nào còn lại.

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

Nâng cấp mã OPTPARE

Ban đầu, mô -đun

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8 đã cố gắng duy trì khả năng tương thích với
>>> 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)')
55. Tuy nhiên,
>>> 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)')
55 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('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)')
57 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('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)')
55 đã đượ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 = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8 được cải thiện trên mô -đun thư viện tiêu chuẩn
>>> 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)')
55 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('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)')
    
    61 và
    >>> 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)')
    
    62.

  • 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 prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    18 và
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    45.

Đường dẫn nâng cấp một phần từ

>>> 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)')
55 lên
>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

options:
 -h, --help  show this help message and exit
8:

  • Thay thế tất cả các cuộc gọi

    >>> 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)')
    
    67 bằng các cuộc gọi
    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    89.

  • Thay thế

    >>> 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)')
    
    69 bằng
    >>> 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)')
    
    70 và thêm các cuộc gọi
    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    89 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('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)')
    
    72, bây giờ trong bối cảnh
    >>> parser = argparse.ArgumentParser(prog='myprogram')
    >>> parser.print_help()
    usage: myprogram [-h]
    
    options:
     -h, --help  show this help message and exit
    
    8 được gọi là
    >>> 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)')
    
    74.

  • Thay thế

    >>> 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)')
    
    75 bằng cách sử dụng
    >>> 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)')
    
    45 thay vì
    $ 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)
    
    09.

  • Thay thế các hành động gọi lại và đối số từ khóa

    >>> 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)')
    
    78 bằng các đối số
    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    18 hoặc
    $ python prog.py 1 2 3 4
    4
    
    $ python prog.py 1 2 3 4 --sum
    10
    
    45.

  • Thay thế tên chuỗi cho các đối số từ khóa

    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    18 bằng các đối tượng loại tương ứng (ví dụ: int, float, phức tạp, v.v.).

  • Thay thế

    >>> 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)')
    
    82 bằ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)
    
    20 và
    >>> 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)')
    
    84 và
    >>> 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)')
    
    85 bằng
    >>> 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)')
    
    86.

  • Thay thế các chuỗi bằng các đối số ngầm như

    >>> 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)')
    
    87 hoặc
    >>> 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)')
    
    88 bằng cú pháp Python tiêu chuẩn để sử dụng từ điển để định dạng chuỗi, nghĩa là,
    $ python prog.py a b c
    usage: prog.py [-h] [--sum] N [N ...]
    prog.py: error: argument N: invalid int value: 'a'
    
    53 và
    $ 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)
    
    45.

  • Thay thế đối số của Trình xây dựng OptionParser

    >>> 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)')
    
    91 bằng một cuộc gọi đến
    >>> 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)')
    
    92.