Trước bất cứ điều gì khác: Không bao giờ sử dụng
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
05 nếu bạn không cần, vì về nguyên tắc không an toàn để làm như vậy. Đối với loại cấu trúc đơn giản này [không có thẻ], bạn nên sử dụng with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
06 [và with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
07 tương ứng sẽ phàn nàn nếu dữ liệu của bạn không thể được an toàn sau khi bán phá giá].never use with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
05 if you don't have to, as it is in principle unsafe to do so. For this kind of simple structure [without tags] you should use with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
06 [and the corresponding with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
07 that will complain if your data cannot be safe_loaded after dumping].with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
08 có chữ ký sau:def dump[documents, stream=None, Dumper=Dumper,
default_style=None, default_flow_style=None,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding='utf-8', explicit_start=None, explicit_end=None,
version=None, tags=None]
Chỉ trong số này chỉ cần đưa ra cái đầu tiên, đó phải là biến
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
09 của bạn. Nếu bạn không chỉ định một luồng, thì with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
10 ghi cấu trúc dữ liệu vào một đối tượng tệp bộ nhớ [như Stringio] và sau khi viết sẽ trả về giá trị dưới dạng chuỗi.Vì vậy, mặc dù bạn có thể làm:
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
Điều này là không hiệu quả và cho thấy rất ít sự hiểu biết về cách thức hoạt động của
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
11.Nếu bạn muốn mở tệp để đọc và viết, bạn phải đảm bảo rằng cả hai bạn đều đặt lại chỉ mục trong tệp và cắt ngắn nội dung của nó. Điều này thường không đáng để nỗ lực nên sẽ an toàn hơn khi mở lại tệp để viết:and truncate its content. This is usually not worth the effort so it is safer to re-open the file for writing:
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
.
Nếu bạn không chỉ định
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
12, đầu ra của bạn sẽ trông giống như:{state: deleted}
Đầu ra sẽ không bao gồm các trích dẫn thừa xung quanh
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
13 trong đầu vào của bạn. Bạn cũng có thể chỉ định with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
14, nhưng điều này cũng sẽ đặt báo giá vào khoảng with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
15. Nếu mất các trích dẫn là một vấn đề và bạn thực sự muốn đầu ra trông giống như đầu vào, bạn nên sử dụng with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
16 [Tuyên bố miễn trừ trách nhiệm tôi là tác giả của gói đó], có thể bảo tồn các trích dẫn trên các chuỗi riêng lẻ, xử lý YAML 1.2 [thay vì YAML 1.1] và cũng bảo tồn các bình luận trong tệp của bạn.If losing the quotes is a problem and you really want the output to look like the input, you should be using
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
16
[disclaimer I am the author of that package], which can preserve the quotes on individual strings, handles YAML 1.2 [instead of YAML 1.1] and also preserves comments in your file. Mới trong phiên bản 3.2.
Mã nguồn: lib/argparse.py Lib/argparse.py
Mô-đun
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 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à with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 sẽ tìm ra cách phân tích các đối số của with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
19. Mô -đun with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 cũng tự động tạo thông báo trợ giúp và sử dụng. Mô -đun cũng sẽ phát hành lỗi khi người dùng đưa ra các đối số không hợp lệ.Chức năng cốt lõi¶
Mô-đun
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 hỗ trợ cho các giao diện dòng lệnh được xây dựng xung quanh một thể hiện là with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
22. Nó là một thùng chứa cho các thông số kỹ thuật đối số và có các tùy chọn áp dụng toàn bộ trình phân tích cú pháp:parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
Phương pháp
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
23 đính kèm các thông số kỹ thuật đối số riêng lẻ vào trình phân tích cú pháp. Nó hỗ trợ các đối số vị trí, các tùy chọn chấp nhận các giá trị và cờ bật/tắt:parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
Phương thức
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
24 chạy trình phân tích cú pháp và đặt dữ liệu được trích xuất vào đối tượng with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
25:args = parser.parse_args[] print[args.filename, args.count, args.verbose]
Liên kết nhanh cho add_argument []
hoạt động | Chỉ định cách xử lý một đối số | 26, 27, 28, 29, 30, 31, 32, 33
|
sự lựa chọn | Giới hạn các giá trị đối với một tập hợp các lựa chọn cụ thể | 34, 35 hoặc 36
|
const | Lưu trữ một giá trị không đổi | |
default | Giá trị mặc định được sử dụng khi không được cung cấp đối số | Mặc định là 37
|
Dest | Chỉ định tên thuộc tính được sử dụng trong không gian tên kết quả | |
Cứu giúp | Trợ giúp thông điệp cho một đối số | |
Metavar | Tên hiển thị thay thế cho đối số như được hiển thị trong trợ giúp | |
nargs | Số lần đối số có thể được sử dụng | 38, 39, 40, 41 hoặc 42
|
yêu cầu | Cho biết liệu một đối số là bắt buộc hay tùy chọn | 43 hoặc 44
|
loại hình | Tự động chuyển đổi một đối số thành loại đã cho | 38, 46, 47 hoặc chức năng có thể gọi được
|
Thí dụ¶
Mã sau đây là một chương trình Python lấy một danh sách các số nguyên và tạo ra tổng hoặc tối đa:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
Giả sử mã Python ở trên được lưu vào một tệp có tên
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
48, nó có thể được chạy ở dòng lệnh và nó cung cấp các thông báo trợ giúp hữu ích:$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]
Khi chạy với các đối số thích hợp, nó in tổng hoặc tối đa của số nguyên dòng lệnh:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 10
Nếu các đối số không hợp lệ được truyền vào, một lỗi sẽ được hiển thị:
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
0Cá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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 là tạo đối tượng with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50:with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
1Đối tượng
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 sẽ chứa 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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53. Nói chung, các cuộc gọi này cho biết with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 được gọi. Ví dụ:with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
2Sau đó, gọi
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 sẽ trả về một đối tượng với hai thuộc tính, with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
57 và with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
58. Thuộc tính with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
57 sẽ là danh sách một hoặc nhiều số nguyên và thuộc tính with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
58 sẽ là hàm with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
61, nếu with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
62 được chỉ định tại dòng lệnh hoặc hàm with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
63 nếu không.Phân tích đối số
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 đối số phân tích cú pháp thông qua phương pháp with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55. Đ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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
66 đơ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:with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
3Trong một tập lệnh,
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 thường sẽ được gọi mà không có đối số và with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 sẽ tự động xác định các đối số dòng lệnh từ with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
19.Đối tượng ArbutionParser
classArgParse.ArgumentParser [prog = none, use = none, description = none, epilog = none, cha mẹ = [], formatter_class = argparse.helpformatter, prefix_chars = '-', fromfile_prefix_chars , add_help = true, allow_abbrev = true, exit_on_error = true] ¶ argparse.ArgumentParser[prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True]¶Tạo một đối tượng
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 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:
71]with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Cách sử dụng - Chuỗi mô tả việc sử dụng chương trình [mặc định: được tạo từ các đối số được thêm vào trình phân tích cú pháp]
Mô tả - Văn bản để hiển thị trước khi trợ giúp đối số [theo mặc định, không có văn bản]
Epilog - văn bản để hiển thị sau khi trợ giúp đối số [theo mặc định, không có văn bản]
Cha mẹ - Danh sách các đối tượng
50 có đối số cũng nên được đưa vàowith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
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:
37]with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
argars_default - Giá trị mặc định toàn cầu cho các đối số [mặc định:
37]with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
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
75 vào trình phân tích cú pháp [mặc định:with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
43]with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
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:
43]with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
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:
43]with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Đã 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ư
79 có nghĩa là with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
80.In previous versions, allow_abbrev also disabled grouping of short flags such as with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
79 to mean with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
80.Đã 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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 sử dụng with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
82 để 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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
83 với mã sau:with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
4Sự trợ giúp cho chương trình này sẽ hiển thị
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
83 làm tên chương trình [bất kể chương trình được gọi từ đâu]:with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
5Để thay đổi hành vi mặc định này, một giá trị khác có thể được cung cấp bằng đối số
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
85 thành with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50:with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
6Lưu ý rằng tên chương trình, cho dù được xác định từ
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
82 hoặc từ đối số with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
85, có sẵn để trợ giúp tin nhắn bằng trình xác định định dạng with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
89.with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
7cách sử dụng¶
Theo mặc định,
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 tính toán thông báo sử dụng từ các đối số mà nó chứa:with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
8Thông báo mặc định có thể được ghi đè với đối số từ khóa
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
91:with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
9Bộ xác định định dạng
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
89 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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 sẽ sử dụng đối số từ khóa with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
94. 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:def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
0Theo 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ố
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
95 thành with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50:def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
1Như với đối số mô tả, văn bản
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
95 được bao bọc theo dòng 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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50.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ố
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
99 cho with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50. Đối số with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
99 lấy một danh sách các đối tượng with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50, 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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 được xây dựng:def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
2Lưu ý rằng hầu hết các trình phân tích cú pháp cha sẽ chỉ định
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
04. Mặt khác, with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 sẽ thấy hai tùy chọn with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
75 [một trong cha mẹ và một trong con] 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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
99. 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¶¶
Các đối tượng
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 cho phép định dạng trợ giúp được tùy chỉnh bằng cách chỉ định một lớp định dạng thay thế. Hiện tại, có bốn lớp như vậy:
classargparse.rawdescriphelpformatter¶ classargparse.rawtexthelpformatter¶ classargparse.argumentdefaultshelpformatterrargparse.RawDescriptionHelpFormatter¶ class
argparse.RawTextHelpFormatter¶ class
argparse.ArgumentDefaultsHelpFormatter¶ class
argparse.MetavarTypeHelpFormatter¶def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
09 và def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
10 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, with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 đối tượng bao bọc dòng mô tả và văn bản epilog trong các thông báo trợ giúp dòng lệnh:def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
3Vượt qua
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
09 như def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
13 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:def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
4def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
10 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.def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
15 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ố:def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
5def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
16 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]:def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
6prefix_chars¶
Hầu hết các tùy chọn dòng lệnh sẽ sử dụng
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
17 làm tiền tố, ví dụ: def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
18. 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ư def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
19 hoặc def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
20, có thể chỉ định chúng bằng đối số def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
21 cho Trình xây dựng ArmparParser:
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
7Đối số
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
21 mặc định là def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
23. Việc cung cấp một tập hợp các ký tự không bao gồm def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
17 sẽ khiến các tùy chọn def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
18 không được phép.Fromfile_prefix_chars¶
Đôi khi, khi xử lý một danh sách đối số đặc biệt dài, có thể có ý nghĩa để giữ danh sách các đối số trong một tệp thay vì gõ nó vào dòng lệnh. Nếu đối số
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
26 được đưa ra cho hàm tạo with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50, 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ụ:def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
8Cá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
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
28] 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 def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
29 được coi là tương đương với biểu thức def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
30.Đối số
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
26 mặc định là with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
37, 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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53 hoặc bằng cách gọi các phương thức def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
34 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 def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
35 cho with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50. Ví dụ: để triệt tiêu toàn cầu tạo thuộc tính trên các cuộc gọi with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55, chúng tôi cung cấp def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
38:def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
9cho phép_abbrev¶
Thông thường, khi bạn chuyển một danh sách đối số cho phương thức
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 của with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50, 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
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
41 thành with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
44:{state: deleted}
0Mới trong phiên bản 3.5.
xung đột_handler¶
Các đối tượng
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 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:{state: deleted}
1Đôi khi [ví dụ: khi sử dụng cha mẹ], có thể hữu ích khi chỉ cần ghi đè bất kỳ đối số cũ nào với cùng một chuỗi tùy chọn. Để có được hành vi này, giá trị
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
45 có thể được cung cấp cho đối số def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
46 của with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50:{state: deleted}
2Lưu ý rằng các đối tượng
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 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 def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
18 cũ được giữ lại dưới dạng hành động def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
50, bởi vì chỉ chuỗi tùy chọn def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
51 đã đượ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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
83 chứa mã sau:with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
4Nếu
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
53 hoặc def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
54 được cung cấp tại dòng lệnh, Trợ giúp của ArbutionParser sẽ được in:{state: deleted}
4Đôi khi, nó có thể hữu ích để vô hiệu hóa việc bổ sung tùy chọn trợ giúp này. Điều này có thể đạt được bằng cách chuyển
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
44 dưới dạng đối số def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
56 cho with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50:{state: deleted}
5Tùy chọn trợ giúp thường là
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
75. Ngoại lệ cho điều này là nếu def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
21 được chỉ định và không bao gồm def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
17, trong trường hợp đó def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
53 và def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
54 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 def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
63 được sử dụng để tiền tố các tùy chọn trợ giúp:{state: deleted}
6exit_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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 của with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50, 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
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
66 thành with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
44:{state: deleted}
7Mới trong phiên bản 3.9.
Phương thức add_argument []
ArcharchParser.add_argument [nameorflags ... [, hành động] [, nargs] [, const] [, mặc định] [, loại] [, lựa chọn] [, yêu cầu] [, trợ giúp]add_argument[name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest]]¶Xác định cách một đối số dòng lệnh duy nhất nên được phân tích cú pháp. Mỗi tham số có mô tả chi tiết hơn dưới đây, nhưng tóm lại chúng là:
Tên hoặc cờ - tên hoặc danh sách các chuỗi tùy chọn, ví dụ:
68 hoặcdef set_state[state]: file_name = "file_to_edit.yaml" with open[file_name] as f: doc = yaml.safe_load[f] doc['state'] = state with open[file_name, 'w'] as f: yaml.safe_dump[doc, f, default_flow_style=False]
69.def set_state[state]: file_name = "file_to_edit.yaml" with open[file_name] as f: doc = yaml.safe_load[f] doc['state'] = state with open[file_name, 'w'] as f: yaml.safe_dump[doc, f, default_flow_style=False]
Hành động - Loại hành động cơ bản sẽ được thực hiện khi gặp đối số này tại dòng lệnh.
NARGS - Số lượng các đối số dòng lệnh nên được tiêu thụ.
Const - một giá trị không đổi được yêu cầu bởi một số lựa chọn hành động và NARGS.
Mặc định - giá trị được tạo ra nếu đối số không có trong dòng lệnh và nếu nó không có trong đối tượng không gian tên.
Loại - Loại mà đối số dòng lệnh phải được chuyển đổi.
Lựa chọn - Một thùng chứa của các giá trị cho phép cho đối số.
Yêu cầu - có hay không tùy chọn dòng lệnh có thể được bỏ qua hay không [chỉ có tùy chọn].
Trợ giúp - Một mô tả ngắn gọn về những gì đối số làm.
Metavar - Tên cho đối số trong các thông báo sử dụng.
DEST - Tên của thuộc tính sẽ được thêm vào đối tượng được trả về bởi
55.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53 phải biết liệu một đối số tùy chọn, như def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
50 hay def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
51 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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53 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ư:
{state: deleted}
8Trong khi một đối số vị trí có thể được tạo như:
{state: deleted}
9Khi
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 được gọi, các đối số tùy chọn sẽ được xác định bởi tiền tố def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
17 và các đối số còn lại sẽ được coi là vị trí:parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']0
hoạt động¶
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 Đối tượng liên kết các đối số dòng lệnh với các hành động. Những hành động này có thể thực hiện bất cứ điều gì với các đối số dòng lệnh được liên kết với chúng, mặc dù hầu hết các hành động chỉ cần thêm một thuộc tính vào đối tượng được trả về bởi with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55. Đối số từ khóa def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
79 Chỉ định cách xử lý các đối số dòng lệnh. Các hành động được cung cấp là:
26 - Điều này chỉ lưu trữ giá trị đối số. Đây là hành động mặc định. Ví dụ:with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
1
27 - Điều này lưu trữ giá trị được chỉ định bởi đối số từ khóa Const; Lưu ý rằng đối số từ khóa Const mặc định làwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
37. Hành độngwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
27 đượ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ụ:with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
2
28 vàwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
85 - Đây là những trường hợp đặc biệt củadef set_state[state]: file_name = "file_to_edit.yaml" with open[file_name] as f: doc = yaml.safe_load[f] doc['state'] = state with open[file_name, 'w'] as f: yaml.safe_dump[doc, f, default_flow_style=False]
27 được sử dụng để lưu trữ các giá trịwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
43 vàwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
44 tương ứng. Ngoài ra, chúng tạo ra các giá trị mặc định tương ứng làwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
44 vàwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
43. Ví dụ:with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
3
29 - Điều này lưu trữ một danh sách và nối mỗi giá trị đối số vào danh sách. Nó rất hữu ích để cho phép một tùy chọn được chỉ định nhiều lần. Nếu giá trị mặc định là không trống, các phần tử mặc định sẽ có trong giá trị phân tích cú pháp cho tùy chọn, với bất kỳ giá trị nào từ dòng lệnh được thêm vào sau các giá trị mặc định đó. Ví dụ sử dụng:with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
4
30 - Điều này lưu trữ một danh sách và nối các giá trị được chỉ định bởi đối số từ khóa Const vào danh sách; Lưu ý rằng đối số từ khóa Const mặc định làwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
37. Hành độngwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
30 thường hữu ích khi nhiều đối số cần lưu trữ các hằng số vào cùng một danh sách. Ví dụ:with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
5
31 - Đ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:with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
6Lưu ý, mặc định sẽ là
37 trừ khi được đặt rõ ràng thành 0.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
32 - Đ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. Xemwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
50 để biết chi tiết về cách tạo ra đầu ra.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
33 - Điều này mong đợi một đối số từ khóawith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
00 trong cuộc gọi{state: deleted}
53 và in thông tin phiên bản và thoát khi được gọi:with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
7
02 - Đ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:{state: deleted}
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
8Mớ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.
{state: deleted}
03 có sẵn trong with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 và thêm hỗ trợ cho các hành động Boolean như def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
51 và {state: deleted}
06:parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']9
Mới trong phiên bản 3.9.
Cách được đề xuất để tạo hành động tùy chỉnh là mở rộng
{state: deleted}
07, ghi đè phương thức {state: deleted}
08 và tùy chọn các phương thức {state: deleted}
09 và {state: deleted}
10.Một ví dụ về một hành động tùy chỉnh:
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag0
Để biết thêm chi tiết, xem
{state: deleted}
07.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
{state: deleted}
12 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à:
13 [một số nguyên].{state: deleted}
13 Đố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ụ:{state: deleted}
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
1Lưu ý rằng
15 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ó.{state: deleted}
39. 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:with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
2Một trong những cách sử dụng phổ biến hơn của
17 là cho phép các tệp đầu vào và đầu ra tùy chọn:{state: deleted}
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
3
40. 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ớiwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
19, nhưng có thể có nhiều đối số tùy chọn với{state: deleted}
19. Ví dụ:{state: deleted}
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
4
41. Giống nhưwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
40, 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ụ:with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
5
Nếu đối số từ khóa
{state: deleted}
12 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ố
{state: deleted}
24 của with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53 đượ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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 khác nhau. Hai cách sử dụng phổ biến nhất của nó là:Khi
53 được gọi vớiwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
28 hoặc{state: deleted}
29. Các hành động này thêm giá trị{state: deleted}
24 vào một trong các thuộc tính của đối tượng được trả về bởi{state: deleted}
55. Xem mô tả hành động cho các ví dụ. Nếuwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
24 không được cung cấp cho{state: deleted}
53, nó sẽ nhận được giá trị mặc định làwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
37.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Khi
53 được gọi với các chuỗi tùy chọn [nhưwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
50 hoặcdef set_state[state]: file_name = "file_to_edit.yaml" with open[file_name] as f: doc = yaml.safe_load[f] doc['state'] = state with open[file_name, 'w'] as f: yaml.safe_dump[doc, f, default_flow_style=False]
51] vàdef set_state[state]: file_name = "file_to_edit.yaml" with open[file_name] as f: doc = yaml.safe_load[f] doc['state'] = state with open[file_name, 'w'] as f: yaml.safe_dump[doc, f, default_flow_style=False]
17. Đ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{state: deleted}
24 sẽ được coi là{state: deleted}
37 thay thế. Xem mô tả NARGS cho các ví dụ.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Đã thay đổi trong phiên bản 3.11:
41 theo mặc định, bao gồm cả khi {state: deleted}
29 hoặc {state: deleted}
28.{state: deleted}
{state: deleted}
41 by default, including when {state: deleted}
29 or {state: deleted}
28.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
{state: deleted}
44 của with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53, có giá trị mặc định là with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
37, 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ị {state: deleted}
44 được sử dụng khi chuỗi tùy chọn không có ở dòng lệnh:parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag6
Nếu không gian tên đích đã có một bộ thuộc tính, mặc định hành động sẽ không ghi được quá nhiều:
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag7
Nếu giá trị
{state: deleted}
44 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ề with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
66. Mặt khác, trình phân tích cú pháp sử dụng giá trị như là:parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag8
Đối với các đối số vị trí với NARG bằng
{state: deleted}
50 hoặc {state: deleted}
51, giá trị {state: deleted}
44 được sử dụng khi không có đối số dòng lệnh nào có mặt:parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag9
Cung cấp
{state: deleted}
53 khiến không có thuộc tính nào được thêm vào nếu đối số dòng lệnh không có mặt:args = parser.parse_args[] print[args.filename, args.count, args.verbose]0
loại hình¶
Theo mặc định, trình phân tích cú pháp đọc các đối số dòng lệnh trong các chuỗi đơn giản. Tuy nhiên, khá thường xuyên, 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ư
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
46 hoặc with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
38. Từ khóa {state: deleted}
56 cho with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53 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
{state: deleted}
56 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 {state: deleted}
59, {state: deleted}
60 hoặc {state: deleted}
61, ngoại lệ sẽ bị bắt và thông báo lỗi được định dạng độc đáo được hiển thị. Không có loại ngoại lệ khác được xử lý.Các loại và chức năng tích hợp phổ biến có thể được sử dụng làm bộ chuyển đổi loại:
args = parser.parse_args[] print[args.filename, args.count, args.verbose]1
Các chức năng do người dùng xác định cũng có thể được sử dụng:
args = parser.parse_args[] print[args.filename, args.count, args.verbose]2
Hàm
{state: deleted}
62 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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
44 và các chuỗi không trống thành with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
43. Đây thường không phải là những gì mong muốn.Nói chung, từ khóa
{state: deleted}
56 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
{state: deleted}
56. Một {state: deleted}
67 sẽ không được định dạng tốt và ngoại lệ {state: deleted}
68 sẽ không được xử lý.Ngay cả
{state: deleted}
69 cũng có những hạn chế để sử dụng với từ khóa {state: deleted}
56. 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 ________ 371-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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53. Khi dòng lệnh được phân tích cú pháp, các giá trị đối số sẽ được kiểm tra và thông báo lỗi sẽ được hiển thị nếu đối số không phải là một trong những giá trị chấp nhận được:args = parser.parse_args[] print[args.filename, args.count, args.verbose]3
Lưu ý rằng việc đưa vào thùng chứa lựa chọn được kiểm tra sau khi bất kỳ chuyển đổi loại nào đã được thực hiện, do đó, loại đối tượng trong thùng chứa lựa chọn phải khớp với loại được chỉ định:
args = parser.parse_args[] print[args.filename, args.count, args.verbose]4
Bất kỳ container nào cũng có thể được truyền dưới dạng giá trị lựa chọn, do đó, các đối tượng
{state: deleted}
73, đối tượng {state: deleted}
74 và các thùng chứa tùy chỉnh đều được hỗ trợ.Việc sử dụng
{state: deleted}
75 không được khuyến nghị vì rất khó kiểm soát sự xuất hiện của nó trong việc sử dụng, trợ giúp và thông báo lỗi.Các lựa chọn được định dạng ghi đè Metavar mặc định thường có nguồn gốc từ Dest. Đây thường là những gì bạn muốn vì người dùng không bao giờ nhìn thấy tham số Dests. Nếu màn hình này không phải là mong muốn [có lẽ vì có nhiều lựa chọn], chỉ cần chỉ định một Metavar rõ ràng.
yêu cầu¶
Nói chung, mô -đun
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 giả định rằng các cờ như def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
50 và {state: deleted}
78 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, with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
43 có thể được chỉ định cho đối số từ khóa {state: deleted}
80 thành with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53:args = parser.parse_args[] print[args.filename, args.count, args.verbose]5
Như ví dụ cho thấy, nếu một tùy chọn được đánh dấu là
{state: deleted}
82, with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 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ị
{state: deleted}
84 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 def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
53 hoặc def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
54 tại dòng lệnh], các mô tả {state: deleted}
84 này sẽ được hiển thị với mỗi đối số:args = parser.parse_args[] print[args.filename, args.count, args.verbose]6
Các chuỗi
{state: deleted}
84 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, with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
89 và hầu hết các đối số từ khóa đến with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53, ví dụ: {state: deleted}
91, {state: deleted}
92, vv .:args = parser.parse_args[] print[args.filename, args.count, args.verbose]7
Khi chuỗi trợ giúp hỗ trợ %-formatting, nếu bạn muốn một
{state: deleted}
93 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 {state: deleted}
94.with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 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ị {state: deleted}
84 thành {state: deleted}
97:args = parser.parse_args[] print[args.filename, args.count, args.verbose]8
Metavar¶
Khi
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 tạo thông báo 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 {state: deleted}
99 sẽ được gọi là parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']00. Một đối số tùy chọn duy nhất
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
51 nên được theo sau bởi một đối số dòng lệnh duy nhất sẽ được gọi là parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']02. Một ví dụ:
args = parser.parse_args[] print[args.filename, args.count, args.verbose]9
Một tên thay thế có thể được chỉ định với
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']03:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]0
Lưu ý rằng
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']03 chỉ thay đổi tên được hiển thị - tên của thuộc tính trên đối tượng
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 vẫn được xác định bởi giá trị định mệnh.Các giá trị khác nhau của
{state: deleted}
12 có thể khiến metavar được sử dụng nhiều lần. Cung cấp một tuple cho parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']03 Chỉ định một màn hình khác nhau cho mỗi đối số:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]1
định mệnh
Hầu hết các hành động
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55. Tên của thuộc tính này được xác định bởi đối số từ khóa parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 của
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53. Đối với các hành động đối số vị trí, parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 thường được cung cấp là đối số đầu tiên cho
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53:import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]2
Đối với các hành động đối số tùy chọn, giá trị của
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 thường được suy ra từ các chuỗi tùy chọn.
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 tạo ra giá trị của parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 bằng cách lấy chuỗi tùy chọn dài đầu tiên và loại bỏ chuỗi
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']17 ban đầu. Nếu không có chuỗi tùy chọn dài nào được cung cấp,
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 sẽ được lấy từ chuỗi tùy chọn ngắn đầu tiên bằng cách tước ký tự
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
17 ban đầu. Bất kỳ ký tự def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
17 nào sẽ được chuyển đổi thành các ký tự parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']21 để đảm bảo chuỗi là tên thuộc tính hợp lệ. Các ví dụ dưới đây minh họa hành vi này:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]3
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 cho phép một tên thuộc tính tùy chỉnh được cung cấp:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]4
Các lớp hành động
Các lớp hành động thực hiện API hành động, một cuộc gọi có thể gọi được trả về một cuộc gọi có thể gọi được xử lý các đối số từ dòng lệnh. Bất kỳ đối tượng nào theo API này có thể được truyền dưới dạng tham số
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
79 đến with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53. classargparse.action [tùy chọn_strings, dest, nargs = none, const = none, mặc định = không, loại = không, lựa chọn = none, bắt buộc = falseargparse.Action[option_strings, dest,
nargs=None, const=None, default=None, type=None, choices=None,
required=False, help=None, metavar=None]¶Các đối tượng hành động được sử dụng bởi một archarchParser để thể hiện thông tin cần thiết để phân tích một đối số duy nhất từ một hoặc nhiều chuỗi từ dòng lệnh. Lớp hành động phải chấp nhận hai đối số vị trí cộng với bất kỳ đối số từ khóa nào được chuyển đến
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
23 ngoại trừ chính def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
79.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ố
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
79] nên có các thuộc tính của Dest Dest, Tùy chọn_Strings Cách dễ nhất để đảm bảo các thuộc tính này được xác định là gọi parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']28.
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
{state: deleted}
08, cần chấp nhận bốn tham số:parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
30 - Đối tượng ArgentParSer có chứa hành động này.parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
31 - Đối tượng
66 sẽ được trả về bởiwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
55. 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ụngwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
34.parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
35 - 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
53.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
37 - Chuỗi tùy chọn được sử dụng để gọi hành động này. Đối sốparser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
37 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
{state: deleted}
08 có thể thực hiện các hành động tùy ý, nhưng thường sẽ đặt các thuộc tính trên parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']31 dựa trên
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 và
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']35.
Các lớp con hành động có thể xác định phương thức
{state: deleted}
10 không có đối số và trả về một chuỗi sẽ được sử dụng khi in cách sử dụng của chương trình. Nếu phương thức đó không được cung cấp, một mặc định hợp lý sẽ được sử dụng.Phương thức parse_args []
ArcharchParser.parse_args [args = none, không gian tên = none] ¶parse_args[args=None, namespace=None]¶Chuyển đổi chuỗi đối số thành các đối tượng và gán chúng là thuộc tính của không gian tên. Trả lại không gian tên đông dân.
Các cuộc gọi trước đây đến
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53 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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53 để 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ừ
19.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
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
66 trống mới.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Giá trị tùy chọn Cú pháp
Phương thức
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 hỗ trợ một số cách chỉ định giá trị của một tùy chọn [nếu nó có một]. Trong trường hợp đơn giản nhất, tùy chọn và giá trị của nó được truyền dưới dạng hai đối số riêng biệt:import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]5
Đối với các tùy chọn dài [các tùy chọn có tên dài hơn một ký tự], tùy chọn và giá trị cũng có thể được truyền dưới dạng đối số dòng lệnh duy nhất, sử dụng
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']49 để tách chúng ra:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]6
Đối với các tùy chọn ngắn [các tùy chọn chỉ dài một ký tự], tùy chọn và giá trị của nó có thể được nối:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]7
Một số tùy chọn ngắn có thể được kết hợp với nhau, chỉ sử dụng một tiền tố
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
17, miễn là chỉ có tùy chọn cuối cùng [hoặc không ai trong số chúng] yêu cầu một giá trị:import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]8
Đối số không hợp lệ¶
Trong khi phân tích dòng lệnh,
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 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[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]9
Đối số chứa ________ 217¶
Phương pháp
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 cố gắng đưa ra lỗi bất cứ khi nào người dùng rõ ràng đã phạm sai lầm, nhưng một số tình huống vốn đã mơ hồ. Ví dụ: đối số dòng lệnh parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']54 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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 rất thận trọng ở đây: Các đối số vị trí chỉ có thể bắt đầu bằng def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
17 nếu chúng trông giống như số âm và không có tùy chọn nào trong trình phân tích cú pháp trông giống như số âm:$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]0
Nếu bạn có các đối số vị trí phải bắt đầu với
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
17 và don lồng trông giống như các số âm, bạn có thể chèn các đối số giả parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']58 cho
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 rằng mọi thứ sau đó là một đối số vị trí:$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]1
Các chữ viết tắt đối số [khớp tiền tố] ¶
Phương pháp
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 theo mặc định cho phép các tùy chọn dài được viết tắt là tiền tố, nếu viết tắt không rõ ràng [tiền tố phù hợp với tùy chọn duy nhất]:by default allows long options to be abbreviated to a prefix, if the abbreviation is unambiguous [the prefix matches a unique option]:$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]2
Một lỗi được tạo ra cho các đối số có thể tạo ra nhiều tùy chọn. Tính năng này có thể được vô hiệu hóa bằng cách đặt allow_abbrev thành
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
44.allow_abbrev to with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
44.Ngoài ________ 119¶
Đô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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
19. Đ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 with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55. Điều này rất hữu ích để kiểm tra tại dấu nhắc tương tác:$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]3
Đối tượng không gian tên
classargparse.namespace¶argparse.Namespace¶Lớp đơn giản được sử dụng theo mặc định bởi
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 để tạo các thuộc tính giữ đối tượng và trả về nó.Lớp này có chủ ý đơn giản, chỉ là một lớp con
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']66 với một biểu diễn chuỗi có thể đọc được. Nếu bạn thích có chế độ xem giống như các thuộc tính, bạn có thể sử dụng thành ngữ Python tiêu chuẩn,
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']67:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]4
Cũng có thể hữu ích khi có một gán
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 thuộc tính cho một đối tượng đã có, thay vì một đối tượng with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
66 mới. Điều này có thể đạt được bằng cách chỉ định đối số từ khóa parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']70:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]5
Các tiện ích khác
Chỉ huy phụ công
ArcharchParser.add_subparsers [[Tiêu đề] [, Mô tả] [, Prog] [, Parser_Class] [, Action] [, Tùy chọn_Strings] [, Dest] [, Yêu cầu] [, Help] [, Metavar]] ¶add_subparsers[[title][, description][, prog][, parser_class][, action][, option_strings][, dest][, required][, help][, metavar]]¶Nhiều chương trình chia chức năng của chúng thành một số lệnh phụ, ví dụ, chương trình
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']71 có thể gọi các lệnh phụ như
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']72,
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']73 và
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']74. 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.
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 hỗ trợ việc tạo ra các lệnh phụ như vậy bằng phương pháp parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']76. Phương thức
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']76 thường được gọi là không có đối số và trả về một đối tượng hành động đặc biệt. Đối tượng này có một phương thức duy nhất,
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']78, lấy tên lệnh và bất kỳ đối số trình xây dựng
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 nào và trả về một đối tượng with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 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 con trong đầu ra trợ giúp, theo mặc định
37with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
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
37 và không có giá trị nào được lưu trữwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Yêu cầu - Có hay không một tiểu ban phải được cung cấp, theo mặc định
44 [được thêm vào trong 3.7]with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Trợ giúp - Trợ giúp cho nhóm phân tích phụ trong đầu ra trợ giúp, theo mặc định
37with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Metavar - Chuỗi trình bày các lệnh phụ có sẵn trong trợ giúp; Theo mặc định, đó là
37 và trình bày các lệnh phụ ở dạng {cmd1, cmd2, ..}with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Một số ví dụ sử dụng:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]6
Lưu ý rằng đối tượng được trả về bởi
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 sẽ chỉ chứa các thuộc tính cho trình phân tích cú pháp chính và trình con được chọn bởi dòng lệnh [chứ không phải bất kỳ trình nào khác]. Vì vậy, trong ví dụ trên, khi lệnh parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']87 được chỉ định, chỉ có các thuộc tính
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
68 và parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']00 và khi lệnh
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']90 được chỉ định, chỉ có các thuộc tính
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
68 và parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']92.
Tương tự, khi một tin nhắn trợ giúp được yêu cầu từ trình con, chỉ có sự trợ giúp cho trình phân tích cú pháp cụ thể đó mới được in. Thông điệp trợ giúp sẽ không bao gồm trình phân tích cú pháp phụ huynh hoặc trình phân tích cú pháp anh chị em. .
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]7
Phương pháp
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']76 cũng hỗ trợ các đối số từ khóa
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']96 và
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']97. Khi có mặt, các lệnh Subparser sẽ xuất hiện trong nhóm của riêng họ trong đầu ra trợ giúp. Ví dụ:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]8
Hơn nữa,
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']98 hỗ trợ một đối số
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']99 bổ sung, cho phép nhiều chuỗi tham khảo cùng một trình con. Ví dụ này, như
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']71, bí danh
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag01 như một tốc ký cho
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag02:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]9
Một cách đặc biệt hiệu quả để xử lý các lệnh phụ là kết hợp việc sử dụng phương thức
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']76 với các cuộc gọi đến
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
34 để mỗi người con biết chức năng Python nào nên thực hiện. Ví dụ:$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 100
Bằng cách này, bạn có thể để
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 thực hiện công việc gọi chức năng thích hợp sau khi phân tích đối số hoàn tất. Liên kết các chức năng với các hành động như thế này thường là cách dễ nhất để xử lý các hành động khác nhau cho mỗi người phụ của bạn. Tuy nhiên, nếu cần phải kiểm tra tên của trình con được gọi, đối số từ khóa parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 cho cuộc gọi
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']76 sẽ hoạt động:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 101
Thay đổi trong phiên bản 3.7: Đối số từ khóa bắt buộc mới.New required keyword argument.
Đối tượng Filetype
classargparse.filetype [mode = 'r', bufsize = -1, expoding = none, error = none] ¶ argparse.FileType[mode='r', bufsize=- 1, encoding=None, errors=None]¶Nhà máy
{state: deleted}
69 tạo ra các đối tượng có thể được chuyển đến đối số loại with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
23. Các đối số có các đối tượng {state: deleted}
69 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.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag11 để biết thêm chi tiết]:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 102
Các đối tượng FileType hiểu đối số giả
def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
23 và tự động chuyển đổi nó thành parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag13 cho các đối tượng
{state: deleted}
69 có thể đọc được và parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag15 cho các đối tượng
{state: deleted}
69 có thể ghi:$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 103
Mới trong phiên bản 3.4: Các đối số từ khóa mã hóa và lỗi.The encodings and errors keyword arguments.
Nhóm tranh luận
ArcharchParser.add_argument_group [title = none, description = none] ¶add_argument_group[title=None, description=None]¶Theo mặc định,
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 Các đối số dòng lệnh của các nhóm vào các đối số vị trí của Cameron 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.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag18:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 104
Phương thức
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag18 trả về một đối tượng nhóm đối số có phương thức
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53 giống như with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 thông thường. Khi một đối số được thêm vào nhóm, trình phân tích cú pháp đối xử với nó giống như một đối số bình thường, nhưng hiển thị đối số trong một nhóm riêng cho các tin nhắn trợ giúp. Phương thức parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag18 chấp nhận các đối số tiêu đề và mô tả có thể được sử dụng để tùy chỉnh màn hình này:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 105
Lưu ý rằng bất kỳ đối số nào không có trong các nhóm do người dùng định nghĩa sẽ kết thúc trong các phần đối số vị trí thông thường của các phần và các đối số tùy chọn.
Đã thay đổi trong phiên bản 3.11: Gọi parser.add_argument['filename'] # positional argument
parser.add_argument['-c', '--count'] # option that takes a value
parser.add_argument['-v', '--verbose',
action='store_true'] # on/off flag
18 trên một nhóm đối số bị phản đối. Tính năng này không bao giờ được hỗ trợ và không phải lúc nào cũng hoạt động chính xác. Chức năng tồn tại trên API do tai nạn thông qua kế thừa và sẽ bị xóa trong tương lai.Calling
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag18 on an argument group is deprecated. This feature was never supported and does not always work correctly. The function exists on the API by accident through inheritance and will be removed in the future.
Loại trừ lẫn nhau¶
ArcharchParser.add_mutally_exclusive_group [bắt buộc = false] ¶add_mutually_exclusive_group[required=False]¶Tạo một nhóm loại trừ lẫn nhau.
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 sẽ đảm bảo rằng chỉ có một trong các đối số trong nhóm loại trừ lẫn nhau có mặt trên dòng lệnh:$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 106
Phương pháp
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag25 cũng chấp nhận một đối số cần thiết, để chỉ ra rằng ít nhất một trong các đối số loại trừ lẫn nhau là bắt buộc:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 107
Lưu ý rằng các nhóm đối số loại trừ hiện tại không hỗ trợ tiêu đề và các đối số mô tả của
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag18.
Đã thay đổi trong phiên bản 3.11: Gọi parser.add_argument['filename'] # positional argument
parser.add_argument['-c', '--count'] # option that takes a value
parser.add_argument['-v', '--verbose',
action='store_true'] # on/off flag
18 hoặc parser.add_argument['filename'] # positional argument
parser.add_argument['-c', '--count'] # option that takes a value
parser.add_argument['-v', '--verbose',
action='store_true'] # on/off flag
25 trên một nhóm loại trừ lẫn nhau không được chấp nhận. Những tính năng này không bao giờ được hỗ trợ và không phải lúc nào cũng hoạt động chính xác. Các chức năng tồn tại trên API do tai nạn thông qua kế thừa và sẽ bị xóa trong tương lai.Calling
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag18 or
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag25 on a mutually exclusive group is deprecated. These features were never supported and do not always work correctly. The functions exist on the API by accident through inheritance and will be removed in the future.
Người phân tích cú pháp Defaults¶
ArcharchParser.set_defaults [** kwargs] ¶set_defaults[**kwargs]¶Hầu hết thời gian, các thuộc tính của đối tượng được trả về bởi
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 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ố. def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
34 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:$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 108
Lưu ý rằng các mặc định cấp độ phân tích cú pháp luôn ghi đè mặc định cấp đối số:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 109
Mặc định cấp độ phân tích cú pháp có thể đặc biệt hữu ích khi làm việc với nhiều trình phân tích cú pháp. Xem phương thức
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']76 để biết ví dụ về loại này. Archarchparparser.get_default [Dest] ¶get_default[dest]¶
Nhận giá trị mặc định cho thuộc tính không gian tên, như được đặt bởi
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
53 hoặc bằng def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
34:with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
00In ấn trợ giúp
Trong hầu hết các ứng dụng điển hình,
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 sẽ chăm sóc định dạng và in bất kỳ thông báo sử dụng hoặc lỗi nào. Tuy nhiên, một số phương thức định dạng có sẵn: ArcharchParser.print_usage [file = none] ¶print_usage[file=None]¶In một mô tả ngắn gọn về cách
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 nên được gọi trên dòng lệnh. Nếu tệp là with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
37, parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag15 được giả định. ArcharchParser.print_help [file = none] ¶print_help[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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50. Nếu tệp là with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
37, parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag15 được giả định.
Ngoài ra còn có các biến thể của các phương thức này chỉ cần trả về một chuỗi thay vì in nó:
ArcharchParser.format_usage [] ¶format_usage[]¶Trả về một chuỗi chứa một mô tả ngắn gọn về cách
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50 nên được gọi trên dòng lệnh. ArcharchParser.format_help [] ¶format_help[]¶Trả về một chuỗi chứa thông báo trợ giúp, bao gồm việc sử dụng chương trình và thông tin về các đối số được đăng ký với
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50.Phân tích cú pháp một phần
ArcharchParser.parse_known_args [args = none, không gian tên = none] ¶parse_known_args[args=None, namespace=None]¶Đôi khi một tập lệnh chỉ có thể phân tích một vài đối số dòng lệnh, chuyển các đối số còn lại cho tập lệnh hoặc chương trình khác. Trong những trường hợp này, phương pháp
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag43 có thể hữu ích. Nó hoạt động giống như
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
55 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.with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
01Cảnh báo
Các quy tắc khớp tiền tố áp dụng cho parser.add_argument['filename'] # positional argument
parser.add_argument['-c', '--count'] # option that takes a value
parser.add_argument['-v', '--verbose',
action='store_true'] # on/off flag
43. Trình phân tích cú pháp có thể tiêu thụ một tùy chọn ngay cả khi nó chỉ là tiền tố của một trong những tùy chọn đã biết của nó, thay vì để nó trong danh sách đối số còn lại. rules apply to
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag43. The parser may consume an option even if it’s just a prefix of one of its known options, instead of leaving it in the remaining arguments list.
Tùy chỉnh tệp phân tích cú pháp tệp
ArmentParser.convert_arg_line_to_args [arg_line] ¶convert_arg_line_to_args[arg_line]¶Các đối số được đọc từ một tệp [xem đối số từ khóa từfile_prefix_chars đến hàm tạo
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
50] được đọc một đối số trên mỗi dòng. def set_state[state]:
file_name = "file_to_edit.yaml"
with open[file_name] as f:
doc = yaml.safe_load[f]
doc['state'] = state
with open[file_name, 'w'] as f:
yaml.safe_dump[doc, f, default_flow_style=False]
28 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:
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
02Phương pháp thoát
ArcharchParser.exit [status = 0, message = none] ¶exit[status=0, message=None]¶Phương thức này chấm dứt chương trình, thoát với trạng thái được chỉ định và, nếu được đưa ra, nó sẽ in một thông báo trước đó. Người dùng có thể ghi đè phương thức này để xử lý các bước này khác nhau:
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
03
ArcharchParser.Error [tin nhắn] ¶error[message]¶Phương pháp này in một thông báo sử dụng bao gồm thông báo đến lỗi tiêu chuẩn và chấm dứt chương trình bằng mã trạng thái là 2.
Phân tích cú pháp xen kẽ
ArcharchParser.parse_intermixed_args [args = none, không gian tên = không]parse_intermixed_args[args=None, namespace=None]¶ ArgumentParser.parse_known_intermixed_args[args=None, namespace=None]¶Một số lệnh UNIX cho phép người dùng xen kẽ các đối số tùy chọn với các đối số vị trí. Các phương pháp
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag48 và
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag49 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ụ,
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
42 và các nhóm loại trừ lẫn nhau bao gồm cả hai tùy chọn và vị trí không được hỗ trợ.Ví dụ sau đây cho thấy sự khác biệt giữa
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag43 và
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag48: cái trước trả về
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag53 là các đối số không được phân biệt, trong khi cái sau thu thập tất cả các vị trí vào
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag54.
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
04parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag49 Trả về một bộ hai mục chứa không gian tên dân cư và danh sách các chuỗi đối số còn lại.
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag48 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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 đã cố gắng duy trì khả năng tương thích với parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag58. Tuy nhiên,
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag58 rất khó để mở rộng một cách minh bạch, đặc biệt là với những thay đổi cần thiết để hỗ trợ các nhà xác định
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag60 mới và thông báo sử dụng tốt hơn. Khi hầu hết mọi thứ trong
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag58 đã đượ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
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17 được cải thiện trên mô -đun thư viện tiêu chuẩn parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag58 theo một số cách bao gồm:
Xử lý đối số vị trí.
Hỗ trợ các lệnh phụ.
Cho phép các tiền tố tùy chọn thay thế như
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
64 vàparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
65.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
56 và{state: deleted}
79.def set_state[state]: file_name = "file_to_edit.yaml" with open[file_name] as f: doc = yaml.safe_load[f] doc['state'] = state with open[file_name, 'w'] as f: yaml.safe_dump[doc, f, default_flow_style=False]
Đường dẫn nâng cấp một phần từ
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag58 lên
with open["file_to_edit.yaml", 'w'] as f:
f.write[yaml.safe_dump[doc]]
17:Thay thế tất cả các cuộc gọi
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
70 bằng các cuộc gọi
23.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Thay thế
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
72 bằngparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
73 và thêm các cuộc gọi
23 bổ sung cho các đối số vị trí. Hãy nhớ rằng những gì trước đây được gọi làwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
75, bây giờ trong bối cảnh
17 được gọi làwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
77.Thay thế
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
78 bằng cách sử dụngparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
48 thay vì
55.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Thay thế các hành động gọi lại và đối số từ khóa
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
81 bằng các đối số
56 hoặc{state: deleted}
79.def set_state[state]: file_name = "file_to_edit.yaml" with open[file_name] as f: doc = yaml.safe_load[f] doc['state'] = state with open[file_name, 'w'] as f: yaml.safe_dump[doc, f, default_flow_style=False]
Thay thế tên chuỗi cho các đối số từ khóa
56 bằng các đối tượng loại tương ứng [ví dụ: int, float, phức tạp, v.v.].{state: deleted}
Thay thế
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
85 bằng
66 vàwith open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
87 vàparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
88 bằngparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
89.Thay thế các chuỗi bằng các đối số ngầm như
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
90 hoặcparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
91 bằng cú pháp Python tiêu chuẩn để sử dụng từ điển cho các chuỗi định dạng, nghĩa là,
91 và{state: deleted}
89.with open["file_to_edit.yaml", 'w'] as f: f.write[yaml.safe_dump[doc]]
Thay thế đối số của Trình xây dựng OptionParser
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
94 bằng một cuộc gọi đếnparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
95.