Hướng dẫn python main command line arguments - đối số dòng lệnh chính của python

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: Giao diện dòng lệnh trong Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Command Line Interfaces in Python

Việc thêm khả năng xử lý các đối số dòng lệnh Python cung cấp giao diện thân thiện với người dùng cho chương trình dòng lệnh dựa trên văn bản của bạn. Nó tương tự như một giao diện người dùng đồ họa dành cho một ứng dụng trực quan mà thao túng bởi các yếu tố hoặc tiện ích đồ họa.Python command line arguments provides a user-friendly interface to your text-based command line program. It’s similar to what a graphical user interface is for a visual application that’s manipulated by graphical elements or widgets.

Python phơi bày một cơ chế để nắm bắt và trích xuất các đối số dòng lệnh Python của bạn. Các giá trị này có thể được sử dụng để sửa đổi hành vi của một chương trình. Ví dụ: nếu chương trình của bạn xử lý dữ liệu được đọc từ một tệp, thì bạn có thể chuyển tên của tệp cho chương trình của mình, thay vì mã hóa giá trị cứng trong mã nguồn của bạn.

Đến cuối hướng dẫn này, bạn sẽ biết:

  • Nguồn gốc của các đối số dòng lệnh Python of Python command line arguments
  • Hỗ trợ cơ bản cho các đối số dòng lệnh Python for Python command line arguments
  • Các tiêu chuẩn hướng dẫn thiết kế giao diện dòng lệnh guiding the design of a command line interface
  • Những điều cơ bản để tùy chỉnh thủ công và xử lý các đối số dòng lệnh Python to manually customize and handle Python command line arguments
  • Các thư viện có sẵn trong Python để giảm bớt sự phát triển của giao diện dòng lệnh phức tạp available in Python to ease the development of a complex command line interface

Nếu bạn muốn một cách thân thiện với người dùng để cung cấp các đối số dòng lệnh Python cho chương trình của bạn mà không cần nhập thư viện chuyên dụng hoặc nếu bạn muốn hiểu rõ hơn về cơ sở chung cho các thư viện hiện có dành riêng để xây dựng giao diện dòng lệnh Python, thì hãy giữ Về đọc!

Giao diện dòng lệnh

Giao diện dòng lệnh [CLI] cung cấp một cách để người dùng tương tác với một chương trình chạy trong trình thông dịch shell dựa trên văn bản. Một số ví dụ về phiên dịch shell là bash trên linux hoặc dấu nhắc lệnh trên windows. Giao diện dòng lệnh được kích hoạt bởi trình thông dịch shell để lộ dấu nhắc lệnh. Nó có thể được đặc trưng bởi các yếu tố sau:command line interface [CLI] provides a way for a user to interact with a program running in a text-based shell interpreter. Some examples of shell interpreters are Bash on Linux or Command Prompt on Windows. A command line interface is enabled by the shell interpreter that exposes a command prompt. It can be characterized by the following elements:

  • Một lệnh hoặc chương trìnhcommand or program
  • Không hoặc nhiều đối số dòng lệnharguments
  • Đầu ra đại diện cho kết quả của lệnhoutput representing the result of the command
  • Tài liệu văn bản được gọi là sử dụng hoặc trợ giúpusage or help

Không phải mọi giao diện dòng lệnh có thể cung cấp tất cả các yếu tố này, nhưng danh sách này cũng không phải là toàn bộ. Sự phức tạp của dòng lệnh dao động từ khả năng truyền một đối số duy nhất, đến nhiều đối số và tùy chọn, giống như một ngôn ngữ cụ thể của miền. Ví dụ: một số chương trình có thể khởi chạy tài liệu web từ dòng lệnh hoặc bắt đầu một trình thông dịch shell tương tác như Python.

Hai ví dụ sau với lệnh Python minh họa mô tả giao diện dòng lệnh:

$ python -c "print['Real Python']"
Real Python

Trong ví dụ đầu tiên này, trình thông dịch Python có tùy chọn

$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
7 cho lệnh, trong đó nói rằng sẽ thực thi các đối số dòng lệnh Python theo tùy chọn
$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
7 dưới dạng chương trình Python.command, which says to execute the Python command line arguments following the option
$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
7 as a Python program.

Một ví dụ khác cho thấy cách gọi Python với

$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
9 để hiển thị trợ giúp:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]

Hãy thử điều này trong thiết bị đầu cuối của bạn để xem tài liệu trợ giúp hoàn chỉnh.

Di sản c

Các đối số dòng lệnh Python trực tiếp kế thừa từ ngôn ngữ lập trình C. Như Guido Van Rossum đã viết trong phần giới thiệu về Python cho các lập trình viên UNIX/C vào năm 1993, C có ảnh hưởng mạnh mẽ đến Python. Guido đề cập đến các định nghĩa về nghĩa đen, định danh, toán tử và các câu như

$ sha1sum main.c
125a0f900ff6f164752600550879cbfabb098bc3  main.c
0,
$ sha1sum main.c
125a0f900ff6f164752600550879cbfabb098bc3  main.c
1 hoặc
$ sha1sum main.c
125a0f900ff6f164752600550879cbfabb098bc3  main.c
2. Việc sử dụng các đối số dòng lệnh Python cũng bị ảnh hưởng mạnh mẽ bởi ngôn ngữ C.

Để minh họa những điểm tương đồng, hãy xem xét chương trình C sau:

 1// main.c
 2#include 
 3
 4int main[int argc, char *argv[]] {
 5    printf["Arguments count: %d\n", argc];
 6    for [int i = 0; i main
Arguments count: 1
Argument      0: main

Bạn có thể thực hiện một chương trình Python,

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
09, tương đương với chương trình C,
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
04, bạn đã thấy ở trên:

# main.py
import sys

if __name__ == "__main__":
    print[f"Arguments count: {len[sys.argv]}"]
    for i, arg in enumerate[sys.argv]:
        print[f"Argument {i:>6}: {arg}"]

Bạn không thấy một biến

$ sha1sum main.c
125a0f900ff6f164752600550879cbfabb098bc3  main.c
4 như trong ví dụ mã C. Nó không tồn tại trong Python vì
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 là đủ. Bạn có thể phân tích các đối số dòng lệnh Python trong
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 mà không cần phải biết độ dài của danh sách và bạn có thể gọi
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
14 tích hợp nếu cần số lượng đối số của chương trình.

Ngoài ra, lưu ý rằng

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
15, khi được áp dụng cho một ITable, trả về một đối tượng
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
16 có thể phát ra các cặp liên kết chỉ số của một phần tử trong
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
17 với giá trị tương ứng của nó. Điều này cho phép lặp qua nội dung của
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 mà không phải duy trì bộ đếm cho chỉ mục trong danh sách.

Thực thi

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
09 như sau:

$ python main.py Python Command Line Arguments
Arguments count: 5
Argument      0: main.py
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 chứa thông tin giống như trong chương trình C:

  • Tên của chương trình
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    09 là mục đầu tiên của danh sách.
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    09 is the first item of the list.
  • Các đối số
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    22,
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    23,
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    24 và
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    25 là các yếu tố còn lại trong danh sách.
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    22,
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    23,
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    24, and
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    25 are the remaining elements in the list.

Với phần giới thiệu ngắn gọn này về một vài khía cạnh phức tạp của ngôn ngữ C, giờ đây bạn đã được trang bị một số kiến ​​thức có giá trị để nắm bắt thêm các đối số dòng lệnh Python.

Hai tiện ích từ thế giới Unix

Để sử dụng các đối số dòng lệnh Python trong hướng dẫn này, bạn sẽ thực hiện một số tính năng một phần của hai tiện ích từ hệ sinh thái UNIX:

  1. sha1sum
  2. SEQ

Bạn sẽ có được một số quen thuộc với các công cụ UNIX này trong các phần sau.

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26 Tính toán các băm SHA-1 và nó thường được sử dụng để xác minh tính toàn vẹn của các tệp. Đối với một đầu vào đã cho, một hàm băm luôn trả về cùng một giá trị. Bất kỳ thay đổi nhỏ trong đầu vào sẽ dẫn đến một giá trị băm khác nhau. Trước khi bạn sử dụng tiện ích với các tham số cụ thể, bạn có thể cố gắng hiển thị trợ giúp:hash function always returns the same value. Any minor changes in the input will result in a different hash value. Before you use the utility with concrete parameters, you may try to display the help:

$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]

Hiển thị trợ giúp của chương trình dòng lệnh là một tính năng phổ biến được phơi bày trong giao diện dòng lệnh.

Để tính toán giá trị băm SHA-1 của nội dung của tệp, bạn tiến hành như sau:

$ sha1sum main.c
125a0f900ff6f164752600550879cbfabb098bc3  main.c

Kết quả cho thấy giá trị băm SHA-1 là trường đầu tiên và tên của tệp là trường thứ hai. Lệnh có thể lấy nhiều hơn một tệp làm đối số:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
0

Nhờ tính năng mở rộng Wildcards của thiết bị đầu cuối UNIX, nó cũng có thể cung cấp các đối số dòng lệnh Python với các ký tự ký tự đại diện. Một nhân vật như vậy là dấu hoa thị hoặc sao [

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
28]:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
1

Shell chuyển đổi

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
29 thành
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
04 và
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
09, là hai tệp phù hợp với mẫu
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
29 trong thư mục hiện tại và chuyển chúng đến
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26. Chương trình tính toán băm SHA1 của từng tệp trong danh sách đối số. Bạn có thể thấy rằng, trên Windows, hành vi là khác nhau. Windows không có sự mở rộng ký tự đại diện, vì vậy chương trình có thể phải phù hợp với điều đó. Việc thực hiện của bạn có thể cần phải mở rộng ký tự đại diện trong nội bộ.SHA1 hash of each of the files in the argument list. You’ll see that, on Windows, the behavior is different. Windows has no wildcard expansion, so the program may have to accommodate for that. Your implementation may need to expand wildcards internally.

Không có bất kỳ đối số nào,

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26 đọc từ đầu vào tiêu chuẩn. Bạn có thể cung cấp dữ liệu cho chương trình bằng cách nhập các ký tự trên bàn phím. Đầu vào có thể kết hợp bất kỳ ký tự nào, bao gồm cả vận chuyển trở lại Enter. Để chấm dứt đầu vào, bạn phải báo hiệu phần cuối của tệp với Enter, theo sau là chuỗi Ctrl+D:Enter. To terminate the input, you must signal the end of file with Enter, followed by the sequence Ctrl+D:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
2

Trước tiên, bạn nhập tên của chương trình,

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26, tiếp theo là Enter, và sau đó
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
36 và
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
22, mỗi người cũng theo sau là Enter. Để đóng luồng đầu vào, bạn nhập ctrl+d. Kết quả là giá trị của băm SHA1 được tạo cho văn bản
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
38. Tên của tệp là
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
39. Đây là một quy ước để chỉ ra đầu vào tiêu chuẩn. Giá trị băm giống nhau khi bạn thực hiện các lệnh sau:Enter, and then
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
36 and
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
22, each also followed by Enter. To close the input stream, you type Ctrl+D. The result is the value of the SHA1 hash generated for the text
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
38. The name of the file is
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
39. This is a convention to indicate the standard input. The hash value is the same when you execute the following commands:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
3

Tiếp theo, bạn sẽ đọc một mô tả ngắn về

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40.

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40 tạo ra một chuỗi các số. Ở dạng cơ bản nhất của nó, như tạo ra chuỗi từ 1 đến 5, bạn có thể thực hiện các mục sau:sequence of numbers. In its most basic form, like generating the sequence from 1 to 5, you can execute the following:

Để có được cái nhìn tổng quan về các khả năng được phơi bày bởi

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40, bạn có thể hiển thị trợ giúp tại dòng lệnh:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
4

Đối với hướng dẫn này, bạn sẽ viết một vài biến thể đơn giản hóa là

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26 và
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40. Trong mỗi ví dụ, bạn sẽ học một khía cạnh khác nhau hoặc kết hợp các tính năng về các đối số dòng lệnh Python.

Trên Mac OS và Linux,

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26 và
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40 sẽ được cài đặt sẵn, mặc dù các tính năng và thông tin trợ giúp đôi khi có thể khác nhau một chút giữa các hệ thống hoặc phân phối. Nếu bạn sử dụng Windows 10, thì phương pháp thuận tiện nhất là chạy
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26 và
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40 trong môi trường Linux được cài đặt trên WSL. Nếu bạn không có quyền truy cập vào một thiết bị đầu cuối để hiển thị các tiện ích UNIX tiêu chuẩn, thì bạn có thể có quyền truy cập vào các thiết bị đầu cuối trực tuyến:

  • Tạo một tài khoản miễn phí trên Pythonanywhere và bắt đầu một bảng điều khiển bash. a free account on PythonAnywhere and start a Bash Console.
  • Tạo một thiết bị đầu cuối bash tạm thời trên repl.it. a temporary Bash terminal on repl.it.

Đây là hai ví dụ, và bạn có thể tìm thấy những người khác.

Mảng
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12

Trước khi khám phá một số quy ước được chấp nhận và khám phá cách xử lý các đối số dòng lệnh Python, bạn cần biết rằng hỗ trợ cơ bản cho tất cả các đối số dòng lệnh Python được cung cấp bởi

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12. Các ví dụ trong các phần sau đây cho bạn biết cách xử lý các đối số dòng lệnh Python được lưu trữ trong
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 và để khắc phục các vấn đề điển hình xảy ra khi bạn cố gắng truy cập chúng. Bạn sẽ học:

  • Cách truy cập nội dung của
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12access the content of
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12
  • Cách giảm thiểu tác dụng phụ của bản chất toàn cầu của
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12mitigate the side effects of the global nature of
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12
  • Cách xử lý khoảng trắng trong các đối số dòng lệnh Pythonprocess whitespaces in Python command line arguments
  • Cách xử lý lỗi trong khi truy cập các đối số dòng lệnh Pythonhandle errors while accessing Python command line arguments
  • Cách ăn định dạng ban đầu của các đối số dòng lệnh Python được truyền qua byteingest the original format of the Python command line arguments passed by bytes

Bắt đầu nào!

Hiển thị đối số

Mô -đun

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
55 hiển thị một mảng có tên
$ sha1sum main.c
125a0f900ff6f164752600550879cbfabb098bc3  main.c
5 bao gồm các mục sau:

  1. $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    57 chứa tên của chương trình Python hiện tại.
    contains the name of the current Python program.
  2. $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    58, phần còn lại của danh sách, chứa bất kỳ và tất cả các đối số dòng lệnh Python được chuyển cho chương trình.
    , the rest of the list, contains any and all Python command line arguments passed to the program.

Ví dụ sau đây cho thấy nội dung của

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
5

Ở đây, cách thức hoạt động của mã này:

  • Dòng 2 nhập mô -đun Python nội bộ
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    55.
    imports the internal Python module
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    55.
  • Dòng 4 trích xuất tên của chương trình bằng cách truy cập phần tử đầu tiên của danh sách
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12.
    extracts the name of the program by accessing the first element of the list
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12.
  • Dòng 5 hiển thị các đối số dòng lệnh Python bằng cách tìm nạp tất cả các yếu tố còn lại của danh sách
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12.
    displays the Python command line arguments by fetching all the remaining elements of the list
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12.

Thực hiện tập lệnh

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
63 ở trên với danh sách các đối số tùy ý như sau:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
6

Đầu ra xác nhận rằng nội dung của

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
64 là tập lệnh Python
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
63 và các yếu tố còn lại của danh sách
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 chứa các đối số của tập lệnh,
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
67.

Để tóm tắt,

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 chứa tất cả các đối số dòng lệnh Python
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
63. Khi trình thông dịch Python thực hiện chương trình Python, nó phân tích dòng lệnh và điền vào
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 với các đối số.

Đảo ngược đối số đầu tiên

Bây giờ bạn có đủ nền trên

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12, bạn sẽ hoạt động trên các đối số được truyền ở dòng lệnh. Ví dụ
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
72 đảo ngược đối số đầu tiên được truyền ở dòng lệnh:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
7

Trong

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
72, quá trình đảo ngược đối số đầu tiên được thực hiện với các bước sau:

  • Dòng 5 tìm kiếm đối số đầu tiên của chương trình được lưu trữ tại Index
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    74 của
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12. Hãy nhớ rằng tên chương trình được lưu trữ tại Index
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    76 của
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12.
    fetches the first argument of the program stored at index
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    74 of
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12. Remember that the program name is stored at index
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    76 of
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12.
  • Dòng 6 in chuỗi đảo ngược.
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    78 là một cách pythonic để sử dụng một thao tác lát cắt để đảo ngược danh sách.
    prints the reversed string.
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    78 is a Pythonic way to use a slice operation to reverse a list.

Bạn thực thi tập lệnh như sau:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
8

Đúng như dự đoán,

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
72 hoạt động trên
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
80 và đảo ngược đối số duy nhất để xuất ra
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
81. Lưu ý rằng xung quanh chuỗi đa từ
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
80 với các trích dẫn đảm bảo rằng trình thông dịch xử lý nó như một đối số duy nhất, thay vì hai đối số. Bạn sẽ đi sâu vào các dấu phân cách đối số trong một phần sau.argument separators in a later section.

Đột biến
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 có sẵn trên toàn cầu cho chương trình Python đang chạy của bạn. Tất cả các mô -đun được nhập trong quá trình thực hiện quy trình đều có quyền truy cập trực tiếp vào
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12. Truy cập toàn cầu này có thể thuận tiện, nhưng
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 không thể thay đổi. Bạn có thể muốn thực hiện một cơ chế đáng tin cậy hơn để đưa các đối số chương trình đến các mô -đun khác nhau trong chương trình Python của bạn, đặc biệt là trong một chương trình phức tạp với nhiều tệp.globally available to your running Python program. All modules imported during the execution of the process have direct access to
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12. This global access might be convenient, but
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12 isn’t immutable. You may want to implement a more reliable mechanism to expose program arguments to different modules in your Python program, especially in a complex program with multiple files.

Quan sát những gì xảy ra nếu bạn giả mạo

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12:

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
9

Bạn gọi

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
88 để xóa và trả lại mục cuối cùng trong
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12.

Thực hiện tập lệnh ở trên:

 1// main.c
 2#include 
 3
 4int main[int argc, char *argv[]] {
 5    printf["Arguments count: %d\n", argc];
 6    for [int i = 0; i main
Arguments count: 1
Argument      0: main
0

Việc thực hiện chương trình Python với cùng các đối số dòng lệnh Python đưa ra điều này:

C:/>main
Arguments count: 1
Argument      0: main
1

Bởi vì bạn tương tác với trình thông dịch Shell hoặc dấu nhắc lệnh Windows, bạn cũng nhận được lợi ích của việc mở rộng thẻ đại diện do Shell cung cấp. Để chứng minh điều này, bạn có thể sử dụng lại

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
09, hiển thị từng đối số với số đối số và giá trị của nó:

C:/>main
Arguments count: 1
Argument      0: main
2

Bạn có thể thấy rằng shell tự động thực hiện mở rộng ký tự đại diện để bất kỳ tệp nào có tên cơ sở khớp với

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
02, bất kể phần mở rộng, là một phần của
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
12.

Việc mở rộng ký tự đại diện có sẵn trên Windows. Để có được hành vi tương tự, bạn cần thực hiện nó trong mã của mình. Để tái cấu trúc

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
09 để làm việc với việc mở rộng ký tự đại diện, bạn có thể sử dụng
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
21. Ví dụ sau đây hoạt động trên Windows và mặc dù nó không ngắn gọn như
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
09 ban đầu, cùng một mã hoạt động tương tự trên các nền tảng:

C:/>main
Arguments count: 1
Argument      0: main
3

Trong

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
23,
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
24 dựa vào
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
25 để xử lý các ký tự đại diện kiểu vỏ. Bạn có thể xác minh kết quả trên Windows và bất kỳ hệ điều hành nào khác:

C:/>main
Arguments count: 1
Argument      0: main
4

Điều này giải quyết vấn đề xử lý các tệp bằng các ký tự đại diện như dấu hoa thị [

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
28] hoặc dấu câu hỏi [
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
27], nhưng làm thế nào về
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
28?

Nếu bạn không chuyển bất kỳ tham số nào cho tiện ích

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26 gốc, thì nó dự kiến ​​sẽ đọc dữ liệu từ đầu vào tiêu chuẩn. Đây là văn bản bạn nhập tại thiết bị đầu cuối kết thúc khi bạn nhập Ctrl+D trên các hệ thống giống như Unix hoặc Ctrl+Z trên Windows. Các chuỗi điều khiển này gửi một kết thúc của tệp [EOF] đến thiết bị đầu cuối, dừng đọc từ
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
28 và trả về dữ liệu đã được nhập.standard input. This is the text you enter at the terminal that ends when you type Ctrl+D on Unix-like systems or Ctrl+Z on Windows. These control sequences send an end of file [EOF] to the terminal, which stops reading from
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
28 and returns the data that was entered.

Trong phần tiếp theo, bạn sẽ thêm vào mã của mình khả năng đọc từ luồng đầu vào tiêu chuẩn.

Đầu vào tiêu chuẩn

Khi bạn sửa đổi triển khai Python trước đó của

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26 để xử lý đầu vào tiêu chuẩn bằng cách sử dụng
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
32, bạn sẽ đến gần hơn với
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26 ban đầu:

C:/>main
Arguments count: 1
Argument      0: main
5

Hai quy ước được áp dụng cho phiên bản

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26 mới này:

  1. Không có bất kỳ đối số nào, chương trình hy vọng dữ liệu sẽ được cung cấp trong đầu vào tiêu chuẩn,
    $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    32, đây là đối tượng tệp có thể đọc được.
  2. Khi một dấu gạch nối [
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    39] được cung cấp dưới dạng đối số tệp ở dòng lệnh, chương trình diễn giải nó khi đọc tệp từ đầu vào tiêu chuẩn.

Hãy thử tập lệnh mới này mà không có bất kỳ đối số nào. Nhập Aphorism đầu tiên của Zen of Python, sau đó hoàn thành mục nhập với phím tắt Ctrl+D trên các hệ thống giống như UNIX hoặc Ctrl+Z trên Windows:Ctrl+D on Unix-like systems or Ctrl+Z on Windows:

C:/>main
Arguments count: 1
Argument      0: main
6

Bạn cũng có thể bao gồm một trong các đối số dưới dạng

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
28 được trộn với các đối số tệp khác như SO:

C:/>main
Arguments count: 1
Argument      0: main
7

Một cách tiếp cận khác trên các hệ thống giống UNIX là cung cấp

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
38 thay vì
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
39 để xử lý đầu vào tiêu chuẩn:

C:/>main
Arguments count: 1
Argument      0: main
8

Trên Windows, không có tương đương với

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
38, do đó, sử dụng
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
39 làm đối số tệp hoạt động như mong đợi.

Kịch bản

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
42 không bao gồm tất cả các xử lý lỗi cần thiết, nhưng bạn sẽ bao gồm một số tính năng còn thiếu sau này trong hướng dẫn này.

Đầu ra tiêu chuẩn và lỗi tiêu chuẩn

Xử lý dòng lệnh có thể có mối quan hệ trực tiếp với

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
28 để tôn trọng các quy ước được chi tiết trong phần trước. Sản lượng tiêu chuẩn, mặc dù không liên quan ngay lập tức, vẫn là một mối quan tâm nếu bạn muốn tuân thủ triết lý Unix. Để cho phép các chương trình nhỏ được kết hợp, bạn có thể phải tính đến ba luồng tiêu chuẩn:

  1. $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    28
  2. $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    45
  3. $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    46

Đầu ra của một chương trình trở thành đầu vào của một chương trình khác, cho phép bạn chuỗi các tiện ích nhỏ. Ví dụ: nếu bạn muốn sắp xếp các câu cách ngôn của Zen of Python, thì bạn có thể thực hiện những điều sau đây:

C:/>main
Arguments count: 1
Argument      0: main
9

Đầu ra trên được cắt ngắn để đọc tốt hơn. Bây giờ hãy tưởng tượng rằng bạn có một chương trình xuất ra cùng một dữ liệu nhưng cũng in một số thông tin gỡ lỗi:

# main.py
import sys

if __name__ == "__main__":
    print[f"Arguments count: {len[sys.argv]}"]
    for i, arg in enumerate[sys.argv]:
        print[f"Argument {i:>6}: {arg}"]
0

Thực hiện tập lệnh Python ở trên đưa ra:

# main.py
import sys

if __name__ == "__main__":
    print[f"Arguments count: {len[sys.argv]}"]
    for i, arg in enumerate[sys.argv]:
        print[f"Argument {i:>6}: {arg}"]
1

Ellipsis [

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
47] chỉ ra rằng đầu ra đã bị cắt ngắn để cải thiện khả năng đọc.

Bây giờ, nếu bạn muốn sắp xếp danh sách các câu cách ngôn, thì hãy thực thi lệnh như sau:

# main.py
import sys

if __name__ == "__main__":
    print[f"Arguments count: {len[sys.argv]}"]
    for i, arg in enumerate[sys.argv]:
        print[f"Argument {i:>6}: {arg}"]
2

Bạn có thể nhận ra rằng bạn đã không có ý định có đầu ra gỡ lỗi làm đầu vào của lệnh

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
48. Để giải quyết vấn đề này, bạn muốn gửi dấu vết đến luồng lỗi tiêu chuẩn, thay vào đó là
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
46:

# main.py
import sys

if __name__ == "__main__":
    print[f"Arguments count: {len[sys.argv]}"]
    for i, arg in enumerate[sys.argv]:
        print[f"Argument {i:>6}: {arg}"]
3

Thực hiện

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
50 để quan sát những điều sau:

# main.py
import sys

if __name__ == "__main__":
    print[f"Arguments count: {len[sys.argv]}"]
    for i, arg in enumerate[sys.argv]:
        print[f"Argument {i:>6}: {arg}"]
4

Bây giờ, các dấu vết được hiển thị cho thiết bị đầu cuối, nhưng chúng không được sử dụng làm đầu vào cho lệnh

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
48.

Phân tích cú pháp tùy chỉnh

Bạn có thể thực hiện

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40 bằng cách dựa vào biểu thức chính quy nếu các đối số không quá phức tạp. Tuy nhiên, mẫu Regex có thể nhanh chóng khiến việc duy trì tập lệnh trở nên khó khăn. Trước khi bạn thử nhận trợ giúp từ các thư viện cụ thể, một cách tiếp cận khác là tạo trình phân tích cú pháp tùy chỉnh. Trình phân tích cú pháp là một vòng lặp lấy từng đối số từng đối số và áp dụng logic tùy chỉnh dựa trên ngữ nghĩa của chương trình của bạn.custom parser. The parser is a loop that fetches each argument one after another and applies a custom logic based on the semantics of your program.

Một triển khai có thể để xử lý các đối số của

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
53 có thể như sau:

# main.py
import sys

if __name__ == "__main__":
    print[f"Arguments count: {len[sys.argv]}"]
    for i, arg in enumerate[sys.argv]:
        print[f"Argument {i:>6}: {arg}"]
5

$ gcc -o main main.c
$ ./main
Arguments count: 1
Argument      0: ./main
93 được đưa ra danh sách các đối số mà không có tên tệp Python và sử dụng
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
55 để nhận được lợi ích của
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
56, loại bỏ các yếu tố từ bên trái của bộ sưu tập. Khi các mục của danh sách các đối số mở ra, bạn áp dụng logic mà bạn mong đợi cho chương trình của bạn. Trong
$ gcc -o main main.c
$ ./main
Arguments count: 1
Argument      0: ./main
93, bạn có thể quan sát như sau:

  • Vòng lặp
    $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    58 là cốt lõi của hàm và chấm dứt khi không có nhiều đối số hơn để phân tích, khi sự trợ giúp được gọi hoặc khi xảy ra lỗi.
    $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    58 loop
    is at the core of the function, and terminates when there are no more arguments to parse, when the help is invoked, or when an error occurs.
  • Nếu tùy chọn
    $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    59 được phát hiện, thì đối số tiếp theo dự kiến ​​là dấu phân cách.
    $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    59
    option is detected, then the next argument is expected to be the separator.
  • $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    60 lưu trữ các số nguyên được sử dụng để tính toán trình tự. Cần có ít nhất một toán hạng và nhiều nhất là ba.
    stores the integers that are used to calculate the sequence. There should be at least one operand and at most three.

Một phiên bản đầy đủ của mã cho

$ gcc -o main main.c
$ ./main
Arguments count: 1
Argument      0: ./main
93 có sẵn bên dưới:

# main.py
import sys

if __name__ == "__main__":
    print[f"Arguments count: {len[sys.argv]}"]
    for i, arg in enumerate[sys.argv]:
        print[f"Argument {i:>6}: {arg}"]
6

Lưu ý rằng một số khía cạnh xử lý lỗi được giữ ở mức tối thiểu để giữ các ví dụ tương đối ngắn.

Cách tiếp cận thủ công này để phân tích các đối số dòng lệnh Python có thể là đủ cho một tập hợp các đối số đơn giản. Tuy nhiên, nó trở nên nhanh chóng dễ bị lỗi khi độ phức tạp tăng lên do như sau:

  • Một số lượng lớn các đối số of arguments
  • Sự phức tạp và sự phụ thuộc lẫn nhau giữa các đối số between arguments
  • Xác nhận để thực hiện chống lại các đối số to perform against the arguments

Cách tiếp cận tùy chỉnh là có thể tái sử dụng và yêu cầu phát minh lại bánh xe trong mỗi chương trình. Đến cuối hướng dẫn này, bạn sẽ cải thiện giải pháp thủ công này và học được một vài phương pháp tốt hơn.

Một vài phương pháp để xác thực các đối số dòng lệnh Python

Bạn đã thực hiện xác thực cho các đối số dòng lệnh Python trong một vài ví dụ như

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
62 và
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
53. Trong ví dụ đầu tiên, bạn đã sử dụng một biểu thức thông thường và trong ví dụ thứ hai, một trình phân tích cú pháp tùy chỉnh.

Cả hai ví dụ này đều tính đến các khía cạnh tương tự. Họ coi các tùy chọn dự kiến ​​là dạng ngắn [

 1// main.c
 2#include 
 3
 4int main[int argc, char *argv[]] {
 5    printf["Arguments count: %d\n", argc];
 6    for [int i = 0; i main
Arguments count: 1
Argument      0: main
00

Mô -đun được đề xuất để sử dụng từ thư viện tiêu chuẩn là

$ gcc -o main main.c
$ ./main
Arguments count: 1
Argument      0: ./main
71. Thư viện tiêu chuẩn cũng phơi bày
C:/>main
Arguments count: 1
Argument      0: main
00 nhưng nó chính thức không được dùng và chỉ được đề cập ở đây cho thông tin của bạn. Nó được thay thế bởi
$ gcc -o main main.c
$ ./main
Arguments count: 1
Argument      0: ./main
71 trong Python 3.2 và bạn đã giành chiến thắng khi thấy nó được thảo luận trong hướng dẫn này.

$ gcc -o main main.c
$ ./main
Arguments count: 1
Argument      0: ./main
71

Bạn sẽ xem lại

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
93, bản sao gần đây nhất của
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
26, để giới thiệu những lợi ích của
$ gcc -o main main.c
$ ./main
Arguments count: 1
Argument      0: ./main
71. Để hiệu ứng này, bạn sẽ sửa đổi
$ sha1sum main.c
125a0f900ff6f164752600550879cbfabb098bc3  main.c
3 và thêm
C:/>main
Arguments count: 1
Argument      0: main
09 để khởi tạo
C:/>main
Arguments count: 1
Argument      0: main
10:

$ python main.py Python Command Line Arguments
Arguments count: 5
Argument      0: main.py
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
7

Đối với chi phí của một vài dòng hơn so với triển khai trước đó, bạn sẽ có một cách tiếp cận sạch để thêm các tùy chọn

 1// main.c
 2#include 
 3
 4int main[int argc, char *argv[]] {
 5    printf["Arguments count: %d\n", argc];
 6    for [int i = 0; i main
Arguments count: 1
Argument      0: main
13 của đối tượng
C:/>main
Arguments count: 1
Argument      0: main
14. Đối tượng này được điền trên dòng 17 bằng cách gọi
C:/>main
Arguments count: 1
Argument      0: main
15.

Để xem tập lệnh đầy đủ với các sửa đổi được mô tả ở trên, hãy mở rộng khối mã bên dưới:

$ python main.py Python Command Line Arguments
Arguments count: 5
Argument      0: main.py
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
8

Để minh họa lợi ích ngay lập tức mà bạn có được bằng cách giới thiệu

$ gcc -o main main.c
$ ./main
Arguments count: 1
Argument      0: ./main
71 trong chương trình này, hãy thực hiện các mục sau:

$ python main.py Python Command Line Arguments
Arguments count: 5
Argument      0: main.py
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
9

Để đi sâu vào các chi tiết của

$ gcc -o main main.c
$ ./main
Arguments count: 1
Argument      0: ./main
71, hãy xem cách xây dựng các giao diện dòng lệnh trong Python với Argparse.

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
99

$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
99 tìm thấy nguồn gốc của nó trong hàm
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
99 C. Nó tạo điều kiện phân tích các tùy chọn dòng lệnh và xử lý, đối số tùy chọn và đối số. Xem lại
C:/>main
Arguments count: 1
Argument      0: main
21 từ
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
53 để sử dụng
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
99:

$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
0

C:/>main
Arguments count: 1
Argument      0: main
24 có các đối số sau:

  1. Danh sách đối số thông thường trừ tên tập lệnh,
    C:/>main
    Arguments count: 1
    Argument      0: main
    
    25
  2. Một chuỗi xác định các tùy chọn ngắn
  3. Một danh sách các chuỗi cho các tùy chọn dài

Lưu ý rằng một tùy chọn ngắn theo sau là một dấu hai chấm [

C:/>main
Arguments count: 1
Argument      0: main
26] mong đợi một đối số tùy chọn và một tùy chọn dài được kéo dài với một dấu hiệu tương đương [
 1// main.c
 2#include 
 3
 4int main[int argc, char *argv[]] {
 5    printf["Arguments count: %d\n", argc];
 6    for [int i = 0; i main
Arguments count: 1
Argument      0: main
28 giống như
$ ./main Python Command Line Arguments
Arguments count: 5
Argument      0: ./main
Argument      1: Python
Argument      2: Command
Argument      3: Line
Argument      4: Arguments
53 và có sẵn trong khối mã bị sụp đổ bên dưới:

$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
1

Tiếp theo, bạn sẽ xem xét một số gói bên ngoài sẽ giúp bạn phân tích các đối số dòng lệnh Python.

Một vài gói python bên ngoài

Dựa trên các quy ước hiện có mà bạn đã thấy trong hướng dẫn này, có một vài thư viện có sẵn trên Chỉ số gói Python [PYPI] thực hiện nhiều bước nữa để tạo điều kiện cho việc thực hiện và duy trì các giao diện dòng lệnh.

Các phần sau đây cung cấp một cái nhìn thoáng qua về Click và Python Prompt Bộ công cụ. Bạn chỉ được tiếp xúc với khả năng rất hạn chế của các gói này, vì cả hai đều yêu cầu một hướng dẫn đầy đủ nếu không phải là toàn bộ loạt phim để làm cho họ công bằng!

Nhấp chuột

Theo văn bản này, Click có lẽ là thư viện tiên tiến nhất để xây dựng giao diện dòng lệnh tinh vi cho chương trình Python. Nó được sử dụng bởi một số sản phẩm Python, đặc biệt là bình và màu đen. Trước khi bạn thử ví dụ sau, bạn cần cài đặt nhấp vào môi trường ảo Python hoặc môi trường địa phương của bạn. Nếu bạn không quen thuộc với khái niệm môi trường ảo, thì hãy kiểm tra các môi trường ảo Python: một mồi.Click is perhaps the most advanced library to build a sophisticated command line interface for a Python program. It’s used by several Python products, most notably Flask and Black. Before you try the following example, you need to install Click in either a Python virtual environment or your local environment. If you’re not familiar with the concept of virtual environments, then check out Python Virtual Environments: A Primer.

Để cài đặt nhấp chuột, hãy tiến hành như sau:

$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
2

Vì vậy, làm thế nào có thể nhấp vào giúp bạn xử lý các đối số dòng lệnh Python? Ở đây, một biến thể của chương trình

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40 bằng cách sử dụng nhấp chuột:

$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
3

Cài đặt

C:/>main
Arguments count: 1
Argument      0: main
31 thành
C:/>main
Arguments count: 1
Argument      0: main
32 đảm bảo rằng nhấp chuột không phân tích các đối số tiêu cực là tùy chọn. Số nguyên âm là các đối số
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40 hợp lệ.

Như bạn có thể đã quan sát, bạn nhận được rất nhiều miễn phí! Một vài nhà trang trí được chạm khắc tốt là đủ để chôn mã nồi hơi, cho phép bạn tập trung vào mã chính, đó là nội dung của

C:/>main
Arguments count: 1
Argument      0: main
34 trong ví dụ này.

Nhập duy nhất còn lại là

$ gcc -o main main.c
$ ./main
Arguments count: 1
Argument      0: ./main
72. Cách tiếp cận khai báo của trang trí lệnh chính,
C:/>main
Arguments count: 1
Argument      0: main
34, loại bỏ mã lặp đi lặp lại mà nếu không cần thiết. Đây có thể là bất kỳ điều nào sau đây:

  • Xác định quy trình trợ giúp hoặc sử dụng a help or usage procedure
  • Xử lý phiên bản của chương trình the version of the program
  • Nắm bắt và thiết lập các giá trị mặc định cho các tùy chọn and setting up default values for options
  • Xác thực các đối số, bao gồm cả loại arguments, including the type

Việc triển khai

$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
40 mới hầu như không làm trầy xước bề mặt. Click cung cấp nhiều tính năng khác sẽ giúp bạn tạo ra một giao diện dòng lệnh rất chuyên nghiệp:

  • Màu đầu ra
  • Nhắc nhở cho các đối số bị bỏ qua
  • Lệnh và lệnh phụ
  • Xác thực loại đối số
  • Gọi lại trên các tùy chọn và đối số
  • Xác thực đường dẫn tệp
  • Thanh tiến trình

Có nhiều tính năng khác là tốt. Kiểm tra viết các công cụ dòng lệnh Python bằng nhấp chuột để xem thêm các ví dụ cụ thể dựa trên nhấp chuột.

Bộ công cụ nhắc nhở Python

Có các gói Python phổ biến khác đang xử lý vấn đề giao diện dòng lệnh, như Docopt cho Python. Vì vậy, bạn có thể tìm thấy sự lựa chọn của bộ công cụ nhắc nhở một chút phản trực giác.

Bộ công cụ nhắc nhở Python cung cấp các tính năng có thể làm cho ứng dụng dòng lệnh của bạn rời khỏi triết lý Unix. Tuy nhiên, nó giúp thu hẹp khoảng cách giữa giao diện dòng lệnh Arcane và giao diện người dùng đồ họa chính thức. Nói cách khác, nó có thể giúp làm cho các công cụ và chương trình của bạn thân thiện với người dùng hơn.Python Prompt Toolkit provides features that may make your command line application drift away from the Unix philosophy. However, it helps to bridge the gap between an arcane command line interface and a full-fledged graphical user interface. In other words, it may help to make your tools and programs more user-friendly.

Bạn có thể sử dụng công cụ này bên cạnh việc xử lý các đối số dòng lệnh Python như trong các ví dụ trước, nhưng điều này cung cấp cho bạn một đường dẫn đến cách tiếp cận giống UI mà không cần bạn phải phụ thuộc vào bộ công cụ UI Python đầy đủ. Để sử dụng

C:/>main
Arguments count: 1
Argument      0: main
38, bạn cần cài đặt nó với
$ python -h
usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ ... complete help text not shown ... ]
95:

$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
4

Bạn có thể tìm thấy ví dụ tiếp theo một chút giả định, nhưng ý định là thúc đẩy các ý tưởng và đưa bạn ra khỏi các khía cạnh nghiêm ngặt hơn của dòng lệnh đối với các quy ước mà bạn đã thấy trong hướng dẫn này.

Như bạn đã thấy logic cốt lõi của ví dụ này, đoạn mã bên dưới chỉ trình bày mã lệch đáng kể so với các ví dụ trước:

$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
5

Mã ở trên bao gồm các cách để tương tác và có thể hướng dẫn người dùng nhập vào đầu vào dự kiến ​​và để xác thực đầu vào tương tác bằng cách sử dụng ba hộp thoại:

  1. C:/>main
    Arguments count: 1
    Argument      0: main
    
    40
  2. C:/>main
    Arguments count: 1
    Argument      0: main
    
    41
  3. C:/>main
    Arguments count: 1
    Argument      0: main
    
    42

Bộ công cụ Python Prompt phơi bày nhiều tính năng khác nhằm cải thiện sự tương tác với người dùng. Cuộc gọi đến người xử lý trong

$ sha1sum main.c
125a0f900ff6f164752600550879cbfabb098bc3  main.c
3 được kích hoạt bằng cách gọi một hàm được lưu trữ trong từ điển. Kiểm tra các câu lệnh chuyển đổi/trường hợp mô phỏng trong Python nếu bạn không bao giờ gặp phải thành ngữ Python này trước đây.

Bạn có thể thấy ví dụ đầy đủ của chương trình bằng cách sử dụng

C:/>main
Arguments count: 1
Argument      0: main
38 bằng cách mở rộng khối mã bên dưới:

$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ ... complete help text not shown ... ]
6

Khi bạn thực thi mã ở trên, bạn đã chào đón một hộp thoại khiến bạn phải hành động. Sau đó, nếu bạn chọn chuỗi hành động, một hộp thoại khác được hiển thị. Sau khi thu thập tất cả các dữ liệu, tùy chọn hoặc đối số cần thiết, hộp thoại biến mất và kết quả được in ở dòng lệnh, như trong các ví dụ trước:

Khi dòng lệnh phát triển và bạn có thể thấy một số nỗ lực tương tác với người dùng một cách sáng tạo hơn, các gói khác như pyinwirer cũng cho phép bạn tận dụng một cách tiếp cận rất tương tác.

Để khám phá thêm thế giới của giao diện người dùng dựa trên văn bản [TUI], hãy xem các giao diện người dùng console xây dựng và phần bên thứ ba trong hướng dẫn của bạn về chức năng in Python.Text-Based User Interface [TUI], check out Building Console User Interfaces and the Third Party section in Your Guide to the Python Print Function.

Nếu bạn quan tâm đến việc nghiên cứu các giải pháp chỉ dựa vào giao diện người dùng đồ họa, thì bạn có thể xem xét kiểm tra các tài nguyên sau:

  • Cách xây dựng một ứng dụng GUI Python với WxPython
  • Python và Pyqt: Xây dựng máy tính máy tính để bàn GUI
  • Xây dựng một ứng dụng di động với khung Kivy Python

Sự kết luận

Trong hướng dẫn này, bạn đã điều hướng nhiều khía cạnh khác nhau của các đối số dòng lệnh Python. Bạn nên cảm thấy chuẩn bị để áp dụng các kỹ năng sau vào mã của mình:

  • Các quy ước và tiêu chuẩn giả của các đối số dòng lệnh Pythonconventions and pseudo-standards of Python command line arguments
  • Nguồn gốc của
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12 trong Pythonorigins of
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12 in Python
  • Việc sử dụng
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12 để cung cấp sự linh hoạt trong việc chạy các chương trình Python của bạnusage of
    $ python -h
    usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ ... complete help text not shown ... ]
    
    12 to provide flexibility in running your Python programs
  • Các thư viện tiêu chuẩn Python như
    $ gcc -o main main.c
    $ ./main
    Arguments count: 1
    Argument      0: ./main
    
    71 hoặc
    $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    99 đó là xử lý dòng lệnh trừu tượngPython standard libraries like
    $ gcc -o main main.c
    $ ./main
    Arguments count: 1
    Argument      0: ./main
    
    71 or
    $ ./main Python Command Line Arguments
    Arguments count: 5
    Argument      0: ./main
    Argument      1: Python
    Argument      2: Command
    Argument      3: Line
    Argument      4: Arguments
    
    99 that abstract command line processing
  • Các gói Python mạnh mẽ như
    $ gcc -o main main.c
    $ ./main
    Arguments count: 1
    Argument      0: ./main
    
    72 và
    C:/>main
    Arguments count: 1
    Argument      0: main
    
    50 để cải thiện hơn nữa khả năng sử dụng của các chương trình của bạnpowerful Python packages like
    $ gcc -o main main.c
    $ ./main
    Arguments count: 1
    Argument      0: ./main
    
    72 and
    C:/>main
    Arguments count: 1
    Argument      0: main
    
    50 to further improve the usability of your programs

Cho dù bạn đang chạy một tập lệnh nhỏ hoặc một ứng dụng dựa trên văn bản phức tạp, khi bạn hiển thị giao diện dòng lệnh, bạn sẽ cải thiện đáng kể trải nghiệm người dùng về phần mềm Python của mình. Trên thực tế, bạn có thể là một trong những người dùng đó!command line interface you’ll significantly improve the user experience of your Python software. In fact, you’re probably one of those users!

Lần tới khi bạn sử dụng ứng dụng của mình, bạn sẽ đánh giá cao tài liệu bạn đã cung cấp với tùy chọn

 1// main.c
 2#include 
 3
 4int main[int argc, char *argv[]] {
 5    printf["Arguments count: %d\n", argc];
 6    for [int i = 0; i 

Chủ Đề