[Twisted-Python] Cisco-style hierarchial CLI

All, 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: client: sh <question mark> server: ip - show IP items server: foo - show Foo items server: bar - show Bar items client: sh f <question mark> server: ver - show foo version server: connections - show foo connections client: sh f c <question mark> server: <cr> - show connection summary server: <int> - show connection detail client: sh f c 3 server: <some stuff> Similarly, for configure mode: client: configure server: ok client: subsystem server(subsystem): ok client: <question mark> server(subsystem): trusted - configure trusted ports server(subsystem): untrusted - configure untrusted ports server(subsystem): map - map from trusted to untrusted client: trust server(subsystem-trusted): ok client: <question mark> server(subsystem-trusted): ip - add a trusted port by IP server(subsystem-trusted): phys - add a trusted physical port server(subsystem-trusted): vlan - add a trusted vlan client: vlan 1 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? -- Regards, Phil +------------------------------------------+ | Phil Mayers | | Network & Infrastructure Group | | Information & Communication Technologies | | Imperial College | +------------------------------------------+

Phil Mayers 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: ... Any such beast exist?
Have a look at twisted.python.usage's idea of subCommands, that should help. You need to build tab completion etc. on top of that; but ideally, t.p.usage will allow you to have the same thing available straight from shell: $ mycommand sub1 --option=42 sub2 -f sub3 Doing stuff... or $ mycommand your-app-here> sub1 --option=42 sub1> sub2 -f sub1 sub2> sub3 Doing stuff... sub1 sub2> Additionally, this snippet allows you to implement the subcommand modularly, with twisted's plugin architecture. --8<-- from twisted.python import usage, plugin class SubCommandOptions(usage.Options): def __init__(self): usage.Options.__init__(self) self.subCommands = [] plugins = plugin.getPlugIns('YourApplicationNameHere-command') plugins.sort() for p in plugins: module = p.load() description = getattr(p, 'description', None) self.subCommands.append((p.name, None, getattr(module, 'Command'), description)) self.subCommands.sort() def postOptions(self): if not hasattr(self, 'subOptions'): self.opt_help() --8<-- --8<-- # plugins.tml register( type="YourApplicationNameHere-command", module="YourApp.commands.sub1", name="sub1", description="Frobble the kibits", ) --8<-- --8<-- # YourApp/commands/sub1.py from twisted.python import usage class Command(usage.Options): def postOptions(self): do_stuff_here() --8<--
participants (2)
-
Phil Mayers
-
Tommi Virtanen