Mô-đun
args = parser.parse_args[]3 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. Nó phân tích các đối số được xác định từ
args = parser.parse_args[]4
Mô-đun
args = parser.parse_args[]3 cũng tự động tạo các thông báo trợ giúp và sử dụng, đồng thời đưa ra các lỗi khi người dùng đưa ra các đối số không hợp lệ cho chương trình
args = parser.parse_args[]3 là một mô-đun tiêu chuẩn;
Trình phân tích cú pháp được tạo bằng
args = parser.parse_args[]7 và một tham số mới được thêm vào bằng
args = parser.parse_args[]8. Các đối số có thể là tùy chọn, bắt buộc hoặc vị trí
Đối số tùy chọn argparse của Python
Ví dụ sau tạo một trình phân tích cú pháp đối số đơn giản
#!/usr/bin/python import argparse # help flag provides flag help # store_true actions stores argument as True parser = argparse.ArgumentParser[] parser.add_argument['-o', '--output', action='store_true', help="shows output"] args = parser.parse_args[] if args.output: print["This is some output"]
Ví dụ thêm một đối số có hai tùy chọn. một
args = parser.parse_args[]9 ngắn và một
if args.output: print["This is some output"]0 dài. Đây là những đối số tùy chọn
import argparse
Mô-đun được nhập
parser.add_argument['-o', '--output', action='store_true', help="shows output"]
Một đối số được thêm vào với
args = parser.parse_args[]8.
if args.output: print["This is some output"]2 được đặt thành
if args.output: print["This is some output"]3 sẽ lưu trữ đối số dưới dạng
if args.output: print["This is some output"]4, nếu có. Tùy chọn trợ giúp cung cấp trợ giúp đối số
args = parser.parse_args[]
Các đối số được phân tích cú pháp với
if args.output: print["This is some output"]5. Các đối số được phân tích cú pháp hiện diện dưới dạng thuộc tính đối tượng. Trong trường hợp của chúng ta, sẽ có thuộc tính
if args.output: print["This is some output"]6________số 8_______
Nếu có đối số, chúng tôi hiển thị một số đầu ra
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output
Chúng tôi chạy chương trình với
args = parser.parse_args[]9 và
if args.output: print["This is some output"]8
$ optional_arg.py --help usage: optional_arg.py [-h] [-o] optional arguments: -h, --help show this help message and exit -o, --output shows output
Chúng tôi có thể hiển thị trợ giúp chương trình
Một đối số được yêu cầu với tùy chọn
if args.output: print["This is some output"]9
#!/usr/bin/python import argparse # required arg parser = argparse.ArgumentParser[] parser.add_argument['--name', required=True] args = parser.parse_args[] print[f'Hello {args.name}']
Ví dụ phải có tùy chọn
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output0 được chỉ định;
$ required_arg.py --name Peter Hello Peter $ required_arg.py usage: required_arg.py [-h] --name NAME required_arg.py: error: the following arguments are required: --name
Các đối số vị trí argparse của Python
Ví dụ sau hoạt động với các đối số vị trí. Chúng được tạo ra với
args = parser.parse_args[]8
#!/usr/bin/python import argparse # positional args parser = argparse.ArgumentParser[] parser.add_argument['name'] parser.add_argument['age'] args = parser.parse_args[] print[f'{args.name} is {args.age} years old']
Ví dụ mong đợi hai đối số vị trí. tên và tuổi
import argparse0
Đối số vị trí được tạo mà không có ký tự tiền tố dấu gạch ngang
import argparse1
Tùy chọn
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output2 của
args = parser.parse_args[]8 đặt tên cho đối số. Nếu không được đưa ra, nó được suy ra từ tùy chọn
import argparse2
Chương trình đặt tên
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output4 cho đối số
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output5
import argparse3
Kiểu argparse của Python
Đối số
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output6 xác định loại đối số
import argparse4
Chương trình hiển thị n số nguyên ngẫu nhiên từ -100 đến 100
import argparse5
Tùy chọn
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output5 mong đợi giá trị số nguyên và nó được yêu cầuQuảng cáo
import argparse6
Tùy chọn
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output8 chỉ định giá trị mặc định, nếu giá trị không được cung cấp
import argparse7
Ví dụ tính lũy thừa. Giá trị số mũ là không bắt buộc;
import argparse8
Python argparse metavar
Tùy chọn
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output9 đặt tên cho giá trị dự kiến bị lỗi và kết quả trợ giúp
import argparse9
Ví dụ đặt tên cho giá trị mong đợi
$ optional_arg.py --help usage: optional_arg.py [-h] [-o] optional arguments: -h, --help show this help message and exit -o, --output shows output0. Tên mặc định là
$ optional_arg.py --help usage: optional_arg.py [-h] [-o] optional arguments: -h, --help show this help message and exit -o, --output shows output1
parser.add_argument['-o', '--output', action='store_true', help="shows output"]0
Tên đã đặt được hiển thị trong đầu ra trợ giúp
Hành động
$ optional_arg.py --help usage: optional_arg.py [-h] [-o] optional arguments: -h, --help show this help message and exit -o, --output shows output2 cho phép nhóm các tùy chọn lặp lại
parser.add_argument['-o', '--output', action='store_true', help="shows output"]1
Ví dụ tạo tin nhắn chào mừng tới tất cả các tên được chỉ định với các tùy chọn
$ optional_arg.py --help usage: optional_arg.py [-h] [-o] optional arguments: -h, --help show this help message and exit -o, --output shows output3 hoặc
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output0;
parser.add_argument['-o', '--output', action='store_true', help="shows output"]2
$ optional_arg.py --help usage: optional_arg.py [-h] [-o] optional arguments: -h, --help show this help message and exit -o, --output shows output5 chỉ định số lượng đối số dòng lệnh sẽ được sử dụng
parser.add_argument['-o', '--output', action='store_true', help="shows output"]3
Ví dụ hiển thị một dãy ký tự từ ký tự một đến ký tự hai. Nó mong đợi hai đối số
parser.add_argument['-o', '--output', action='store_true', help="shows output"]4
Với
$ optional_arg.py --help usage: optional_arg.py [-h] [-o] optional arguments: -h, --help show this help message and exit -o, --output shows output6, chúng tôi xác định rằng chúng tôi mong đợi hai đối số
parser.add_argument['-o', '--output', action='store_true', help="shows output"]5
Chương trình hiện ra dãy ký tự từ e đến k
Có thể đặt số lượng đối số thay đổi bằng ký tự
$ optional_arg.py --help usage: optional_arg.py [-h] [-o] optional arguments: -h, --help show this help message and exit -o, --output shows output7
parser.add_argument['-o', '--output', action='store_true', help="shows output"]6
Ví dụ tính tổng các giá trị;
parser.add_argument['-o', '--output', action='store_true', help="shows output"]7
Các lựa chọn argparse của Python
Tùy chọn
$ optional_arg.py --help usage: optional_arg.py [-h] [-o] optional arguments: -h, --help show this help message and exit -o, --output shows output8 giới hạn các đối số trong danh sách đã cho
parser.add_argument['-o', '--output', action='store_true', help="shows output"]8
Trong ví dụ, tùy chọn
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output4 có thể chấp nhận các giá trị sau.
#!/usr/bin/python import argparse # required arg parser = argparse.ArgumentParser[] parser.add_argument['--name', required=True] args = parser.parse_args[] print[f'Hello {args.name}']0,
#!/usr/bin/python import argparse # required arg parser = argparse.ArgumentParser[] parser.add_argument['--name', required=True] args = parser.parse_args[] print[f'Hello {args.name}']1,
#!/usr/bin/python import argparse # required arg parser = argparse.ArgumentParser[] parser.add_argument['--name', required=True] args = parser.parse_args[] print[f'Hello {args.name}']2 hoặc
#!/usr/bin/python import argparse # required arg parser = argparse.ArgumentParser[] parser.add_argument['--name', required=True] args = parser.parse_args[] print[f'Hello {args.name}']3
parser.add_argument['-o', '--output', action='store_true', help="shows output"]9
Ví dụ sau mô phỏng lệnh head của Linux. Nó hiển thị n dòng văn bản từ đầu tệp
args = parser.parse_args[]0
Ví dụ: chúng tôi có tệp thử nghiệm nhỏ này
args = parser.parse_args[]1
Ví dụ có hai tùy chọn.
#!/usr/bin/python import argparse # required arg parser = argparse.ArgumentParser[] parser.add_argument['--name', required=True] args = parser.parse_args[] print[f'Hello {args.name}']4 cho tên tệp và
$ optional_arg.py -o This is some output $ optional_arg.py --output This is some output5 cho số dòng hiển thị