Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child command behavior #31

Open
wsande opened this issue Oct 14, 2021 · 1 comment
Open

Child command behavior #31

wsande opened this issue Oct 14, 2021 · 1 comment
Assignees
Labels

Comments

@wsande
Copy link

wsande commented Oct 14, 2021

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()

@italorossi
Copy link
Owner

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 italorossi self-assigned this Oct 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants