Hướng dẫn does python have tab completion feature? - python có tính năng hoàn thành tab không?

Các công cụ dòng lệnh là mạnh mẽ. Những thứ như make có các trang thủ công trải dài, tốt, các trang, trong khi chỉ là danh sách các tiểu ban git dài hơn so với có thể phù hợp trên màn hình thiết bị đầu cuối 80 x 24 tiêu chuẩn.

$ git <TAB>
add                  filter-branch        rebase
am                   format-patch         reflog
annotate             fsck                 relink
...
describe             prco                 unassume
--More--

Lưu ý --More-- ở phía dưới? Đó là niềm vui của việc phân trang.

Lưu ý

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
0 ở đầu? Điều đó thể hiện thực sự nhấn phím tab. Ah, niềm vui hoàn thành tab shell.

Tab hoàn thành là một phần không thể thiếu của việc viết bất cứ điều gì trên dòng lệnh. Cá nhân, tôi không thể hình ảnh cố gắng

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
1 (bí danh đến
import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
2) một nhánh mà không có
import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
0 để thực hiện công việc nặng nhọc. Họ nói "E" là nguyên âm phổ biến nhất và "T" phụ âm phổ biến nhất. Việc sử dụng bàn phím của tôi có lẽ trông giống như thế này hơn:

Hướng dẫn does python have tab completion feature? - python có tính năng hoàn thành tab không?
Cách sử dụng quan trọng của tôi

Bây giờ có một công cụ gọi là

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
4 giống như git cho dữ liệu. Nó có thể được xem như là sự kết hợp đa nền tảng của gitmake được thiết kế để xử lý dữ liệu lớn và nhiều kho lưu trữ đám mây, cũng như theo dõi các thí nghiệm học máy. Như bạn có thể tưởng tượng, hỗ trợ rằng nhiều từ thông dụng có nghĩa là nó cũng có một số lượng lớn các tiểu ban và tùy chọn.

Mỗi khi thêm một tính năng mới, người bảo trì và người đóng góp phải cập nhật các tập lệnh hoàn thành tab cho nhiều vỏ được hỗ trợ. Tốt nhất, đó là một nỗi đau, và tồi tệ nhất, dễ bị lỗi. Nếu bạn đã làm việc để duy trì các ứng dụng CLI, bạn sẽ thông cảm.

Chắc chắn mã phân tích cú pháp bạn đã viết là đủ thông tin để tự động hóa hoàn thành tab? Chắc chắn bạn không nên duy trì và đồng bộ hóa các tập lệnh hoàn thành tab riêng biệt?

Tin tốt:

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
7 là một công cụ mới thực hiện tất cả các công việc này.

Bất kỳ ứng dụng Python CLI nào sử dụng

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
8,
import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
9 hoặc
shtab --shell=bash hello.main.get_main_parser \
  | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/hello >/dev/null
0 có thể hoàn thành tab miễn phí!

Chỉ cần trao đối tượng phân tích cú pháp của bạn cho

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
7 (thông qua CLI hoặc API Python) và tập lệnh hoàn thành tab sẽ được tạo cho vỏ ưa thích của bạn. Nó dễ dàng như:

  • CLI:
    shtab --shell=bash hello.main.get_main_parser \
      | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/hello >/dev/null
    2, hoặc
  • API Python:
    shtab --shell=bash hello.main.get_main_parser \
      | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/hello >/dev/null
    3.

import argparse def get_main_parser(): parser = argparse.ArgumentParser(prog="hello") parser.add_argument( "who", help="good question", nargs="?", default="world") parser.add_argument( "--what", help="a better question", default="hello", choices=["hello", "goodbye"]) return parser if __name__ == "__main__": parser = get_main_parser() args = parser.parse_args() print("{}, {}!".format(args.what, args.who))8 Ví dụ

Giả sử bạn có một số mã trong một mô -đun

shtab --shell=bash hello.main.get_main_parser \
  | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/hello >/dev/null
5:

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))

Để hoàn thành tab cho

shtab --shell=bash hello.main.get_main_parser \
  | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/hello >/dev/null
6, chỉ cần cài đặt
import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
7 và sau đó chạy:

shtab --shell=bash hello.main.get_main_parser \
  | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/hello >/dev/null

Người dùng zsh? Không thành vấn đề. Đơn giản chỉ cần chạy:

shtab --shell=zsh hello.main.get_main_parser \
  | sudo tee /usr/local/share/zsh/site-functions/_hello >/dev/null
# note the underscore `_` prefix in the filename

Một cách khéo léo, bạn có thể cài đặt các lần hoàn thành của riêng ____ 17 bằng cách làm theo các ví dụ trên thay thế

shtab --shell=bash hello.main.get_main_parser \
  | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/hello >/dev/null
9 bằng
import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
7.

Hướng dẫn does python have tab completion feature? - python có tính năng hoàn thành tab không?
________ hoàn thành
import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
4 hoàn thành trong
shtab --shell=bash hello.main.get_main_parser \
  | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/hello >/dev/null
6 và
shtab --shell=zsh hello.main.get_main_parser \
  | sudo tee /usr/local/share/zsh/site-functions/_hello >/dev/null
# note the underscore `_` prefix in the filename
4

Sử dụng

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
7, đây là những gì hoàn thành của ____ 14 trông giống như khi được cài đặt:

% dvc <TAB>
Completing dvc commands
add         -- Track data files or directories with DVC.
cache       -- Manage cache settings.
checkout    -- Checkout data files from cache.
commit      -- Save changed data to cache and update DVC-files.
completion  -- Prints out shell tab completion scripts.
At Top: Hit TAB for more, or the character to insert

Tất cả các đề xuất hoàn thành được đảm bảo trong sync với mã! Những người bảo trì

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
4 rất ngạc nhiên khi thấy không dưới 84 cam kết chạm vào các kịch bản hoàn thành cũ của họ. Churn như vậy bây giờ là một điều của quá khứ!

Bạn có thể nhận thấy một trong những tiểu ban được cung cấp bởi

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
4 là
shtab --shell=zsh hello.main.get_main_parser \
  | sudo tee /usr/local/share/zsh/site-functions/_hello >/dev/null
# note the underscore `_` prefix in the filename
9. Dưới đây là một ví dụ nhanh về cách cung cấp sự tiện lợi như vậy cho người dùng:

Tích hợp ví dụ thư viện

Cảm thấy tối thiểu? Làm thế nào về việc thêm

% dvc <TAB>
Completing dvc commands
add         -- Track data files or directories with DVC.
cache       -- Manage cache settings.
checkout    -- Checkout data files from cache.
commit      -- Save changed data to cache and update DVC-files.
completion  -- Prints out shell tab completion scripts.
At Top: Hit TAB for more, or the character to insert
0 vào chính ứng dụng của bạn cho giao diện người dùng sạch hơn? Và chúng ta hãy sử dụng
shtab --shell=bash hello.main.get_main_parser \
  | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/hello >/dev/null
0 để chuyển đổi cú pháp gọn gàng của ____ 19 thành
import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
8 trong khi chúng ta đang ở đó.

"""Greetings and partings.

Usage:
  greeter [options] [<you>] [<me>]

Options:
  -g, --goodbye  : Say "goodbye" (instead of "hello")
  -b, --print-bash-completion  : Output a bash tab-completion script
  -z, --print-zsh-completion  : Output a zsh tab-completion script

Arguments:
  <you>  : Your name [default: Anon]
  <me>  : My name [default: Casper]
"""
import sys, argopt, shtab

parser = argopt.argopt(__doc__)
if __name__ == "__main__":
    args = parser.parse_args()
    if args.print_bash_completion:
        print(shtab.complete(parser, shell="bash"))
        sys.exit(0)
    if args.print_zsh_completion:
        print(shtab.complete(parser, shell="zsh"))
        sys.exit(0)

    msg = "k thx bai!" if args.goodbye else "hai!"
    print("{} says '{}' to {}".format(args.me, msg, args.you))

Thử nó ra

Có nhiều lựa chọn và tính năng hơn. Tài liệu bao gồm các ví dụ về làm việc với hoàn thành tệp tùy chỉnh và cung cấp tiểu ban

shtab --shell=zsh hello.main.get_main_parser \
  | sudo tee /usr/local/share/zsh/site-functions/_hello >/dev/null
# note the underscore `_` prefix in the filename
9 khi tích hợp chặt chẽ hơn với các ứng dụng hiện có.

Hãy thử nó với

% dvc <TAB>
Completing dvc commands
add         -- Track data files or directories with DVC.
cache       -- Manage cache settings.
checkout    -- Checkout data files from cache.
commit      -- Save changed data to cache and update DVC-files.
completion  -- Prints out shell tab completion scripts.
At Top: Hit TAB for more, or the character to insert
5 hoặc
% dvc <TAB>
Completing dvc commands
add         -- Track data files or directories with DVC.
cache       -- Manage cache settings.
checkout    -- Checkout data files from cache.
commit      -- Save changed data to cache and update DVC-files.
completion  -- Prints out shell tab completion scripts.
At Top: Hit TAB for more, or the character to insert
6!

Nó có giá trị thời gian không?

Hướng dẫn does python have tab completion feature? - python có tính năng hoàn thành tab không?
Nó đáng giá xkcd#1205

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="hello")
    parser.add_argument(
        "who", help="good question", nargs="?", default="world")
    parser.add_argument(
        "--what", help="a better question", default="hello",
        choices=["hello", "goodbye"])
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("{}, {}!".format(args.what, args.who))
7 sẽ ở hàng thứ hai, xa bên trái (thậm chí có thể ngoài lưới). Thật đáng để dành nhiều ngày để có được đúng nhưng chỉ mất vài giây để cài đặt.

Python có tự động hoàn thành không?

Tính năng tự động hoàn thành có sẵn trong cả giao diện dòng lệnh Python (Python) và giao diện dòng lệnh Python. Tính năng này có thể cung cấp cho bạn gợi ý và tiết kiệm thời gian khi bạn không chắc chắn về cách đánh vần chính xác của các lớp, phương thức và thuộc tính.. This feature can give you hints and save time when you are not sure about the exact spelling of classes, methods and properties.

Làm cách nào để kích hoạt tự động hoàn thành trong python idle?

Hoàn thành mã và các mẹo gọi Python Idle có chức năng hoàn thành mã cơ bản.Nó chỉ có thể tự động hoàn thành tên của các chức năng và các lớp.Để sử dụng tự động hoàn thành trong trình chỉnh sửa, chỉ cần nhấn phím tab sau một chuỗi văn bản.press the tab key after a sequence of text.

Idle có đi kèm với Python không?

IDLE đi kèm với việc triển khai mặc định của ngôn ngữ Python kể từ 01.5.Bản phát hành 2B1.Nó được đóng gói như một phần tùy chọn của bao bì Python với nhiều phân phối Linux, Windows và Mac.

Lời nhắc của vỏ tương tác của cửa sổ Python là gì?

Trên Windows, hãy hiển thị dấu nhắc lệnh và nhập "PY" hoặc bắt đầu phiên Python tương tác bằng cách chọn "Python (dòng lệnh)", "IDLE" hoặc chương trình tương tự từ menu ứng dụng / thanh tác vụ."py", or start an interactive Python session by selecting "Python (command line)", "IDLE", or similar program from the task bar / app menu.