prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.28k stars 715 forks source link

Question: autocomplete for custom cmd derived from click, How? #1762

Open evan0greenup opened 1 year ago

evan0greenup commented 1 year ago

I am writing a shell-like command REPL, the format of command is similar to shell. But it has nothing with shell. The program take input and use shlex to split and feed it to a collections of objects whose type is subclass of click.BaseCommand.

Something like that:

command_dict: dict[str, click.BaseCommand] 

while True:
    cmd_text = prompt_toolkit.prompt("$>>")
    args = shlex.split(cmd_text)
    command_dict[args[0]].main(args, standalone_mode=False)

What I hope is to make prompt_toolkit.prompt("$>>") provide autocomplete depend on the content in command_dict without any redundant definition, in other words, when the content in command_dict changed (for example add new command, or the Command object in command_dict become Group object, and add new sub-commands), the auto-complete content will automatically change accordingly.

Is it possible?

What is the best practice to achieve that?