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

Add a subcommand example #120

Open tschwaller opened 9 years ago

tschwaller commented 9 years ago

Add an example how to implement a subcommand, e.g.

vlan 100 (vlan 100)> name "VLAN for NFS traffic" (vlan 100)> exit ...

The prompt changes and also the available commands in the subcontext.

jonathanslenders commented 9 years ago

Hi @tschwaller,

Thanks for the suggestion. I will try to create such an example.

There are several approaches. One way is to run a new CLI with another prompt and completer.

Another way it to reuse the same, but make both dynamic. The prompt itself can be made dynamic through the get_prompt_tokens function (See this example: https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/examples/colored-prompt.py). The completer can be made dynamic by having one completer, that depending on the state calls a second or third completer to do the actual completions.

Jonathan

tschwaller commented 9 years ago

Hi Jonathan (@jonathanslenders)

Thanks for the suggestion. I will try to create such an example.

thanks for looking into that! Appreciate it! The Python configshell might give you some inspiration how others solved that "problem".

https://github.com/agrover/configshell-fb http://www.risingtidesystems.com/git/?p=configshell.git;a=summary (Original version)

A simple example from me: ;-) https://tomsreadme.wordpress.com/2013/01/08/example-shell/

I looked at the colored-prompt.py example before and is was actually the reason why I opened this ticket to go one step further. The next step would be to use YAML to describe a shell similar to klish doing it with XML, but that is probably asking too much. ;-)

http://libcode.org/projects/klish/

Looking forward to the subcommand example!

Tom

jonathanslenders commented 5 years ago

Hi @tschwaller,

This is a very long time ago, but now I have an open pull request for this: https://github.com/prompt-toolkit/python-prompt-toolkit/pull/929

Would that solve your issue?

[edit] I don't think it's exactly this, but related.