[Twisted-Python] Cisco-style hierarchial CLI

exarkun at divmod.com exarkun at divmod.com
Mon Mar 22 09:09:40 EST 2004

On Sat, 20 Mar 2004 18:43:36 +0000, Phil Mayers <p.mayers at imperial.ac.uk> wrote:
> I'm putting together some glue-ware for a system, and would like to hide
> the underlying nature of it. It's a bit of software that fits in and
> around routers / switches etc. and there's a pretty common CLI for those
> these days, which consists of a hierarchial context-based set of
> commands, plus completion, e.g. on Cisco, although I suspect everyone is
> familiar with the concept:
> [snip examples]
> The hiearchial configure mode is mapped onto a hierarchial configuration
> file, e.g. the above would generate:
> subsystem
>   trusted
>     vlan 1
>   !
> !
> It's main features are:
> 1) Tab completion and partial -> full conversion
> 2) Nested modes
> Relevant to twisted is I want to replace the manhole interactive
> interpreter.
> Any such beast exist?

  twisted.python.usage has some support for subcommands.  It doesn't support arbitrary abbreviations (if I recall correctly), but could probably be made to without much difficulty.

  Tab completion would have to be offered by whatever UI was presented, of course, and facilitated by some easily-done introspection into the Options class.

  t.p.usage.Options instances set their "subOptions" attribute when a sub-command is encountered.  I think you could exploit this fact to support nested states, by grabbing the subOptions object and asking it to parse further input.  I'm not sure how "backing up" should work, but you could probably accomplish it by keeping a stack of all the options classes that you get from the subOptions attribute.

  Of course it supports arbitrarily deep nesting, so multilevel commands are support.  The API is pretty simple, you just create an Options subclass with a subCommands class attribute bound to a
list of lists of the form ["longname", "shortname", OtherOptionsSubclass, "Description"].

  The existing code won't get you all the way there (especially the help formatting), but it seems like a reasonable place to start.


More information about the Twisted-Python mailing list