ishell icon indicating copy to clipboard operation
ishell copied to clipboard

Child command behavior

Open wsande opened this issue 3 years ago • 1 comments

Apologies if this isn't an appropriate use of the issue tracker. I'm new at this...

I came across ishell recently and it looks very nice! Seems like a good fit for a CLI I'm trying to build. Something like the Cisco example. But the behavior of child commands is not quite what I expect - I'm probably not using it correctly, so any help would be greatly appreciated.

I've started with a very simple example with a 'show' command and 3 sub-commands - 'modules', 'hosts' and 'ports'. The first 2 are just stubs, and I'm trying to get the correct nested command behavior with "ports". There are 4 ports defined, each just a key-value pair, (port number and description) for now.

This mostly works as I would expect. I'll just list what doesn't work.

  • Typing 'show ports 10' at the base prompt should display the details for port 10. That doesn't work, But if I type 'show' and then tab-complete my way to 'ports 10' it works.
  • Once I've done that, I'm in the "show ports" command, so I should be able to 'TAB' to get a list of ports or just type a port number to show details for that port number. Neither of these work. I have to back out to the CLI root and then tab-complete my way in again.
  • Sometimes after running 'show ports XX' the prompt is '(show)' and sometimes it's '(show ports)'. I don't understand the inconsistency.

I'm sure I'm just not using the library correctly. I've tried a bunch of variations and can't get the expected behavior. Any guidance you can give would be appreciated. My minimal code is below.

Thanks, ws

from ishell.console import Console
from ishell.command import Command
from ishell.utils import _print

def get_ports():
    # return a dicitonary of ports for testing
    port_info = {'10': "blah", '20' : "blah blah", '30': "blahdy blah blah", '40': "blahdy blahdy blah"}
    return port_info


class Hosts(Command):
    def run(self, line):
        _print("Here are the Hosts: Sunnyvale  Cupertino  Palo Alto")


class Modules(Command):
    def run(self, line):
        _print("Here are the Modules:  Hub1, Leaf1, Leaf2, Leaf3")


class Ports(Command):
    def args(self):
        return(get_ports().keys())

    def run(self, line):
        self.prompt = '(show ports)'
        port_info = get_ports()
        arg = line.split()[-1]
        if arg != 'ports':    # print details for a single port for "show ports 10" e.g.
            ss = "%s: %s" % (arg, port_info[arg])
            _print(ss)
        else:                 # print the list of port numbers for "show ports"  e.g.
            for port in port_info.keys():
                ss = "%s" % port
                _print(ss)

        self.loop()


class Show(Command):
    """
    Children:  ports, modules, hosts
    """
    def args(self):
        return ['ports', 'modules', 'hosts']

    def run(self, line):
        pass
        self.prompt = '(show)'
        ports = Ports('ports', help='Ports', dynamic_args=True)
        modules = Modules('modules', help='Modules', dynamic_args=True)
        hosts = Hosts('hosts', help='Hosts', dynamic_args=True)

        self.addChild(ports)
        self.addChild(modules)
        self.addChild(hosts)

        self.loop()


def main():
    console = Console(prompt="TEST_CLI", prompt_delim =">")

    show = Show("show", help="Show resources", dynamic_args=True)
    console.addChild(show)
    console.loop()


if __name__ == '__main__':
    main()

wsande avatar Oct 14 '21 23:10 wsande

You're mixing command args with sub-commands. You have ports as a dynamic arg of show and when you run show ports you dynamically created a ports command and attached it in the show shell, then the next time you hit enter it gets a different behavior.

I suggest you remove the dynamic args from show since they're not dynamic at all and define Ports, Modules and Hosts as separate classes each one with his own def run(self, line) function.

italorossi avatar Oct 18 '21 13:10 italorossi