1
votes

I'm using python click framework to define commands. I want to define this kind of command:

config bgp neighbor neighbor_addr detail local_addr as_number ...

for example:

config bgp neighbor 1.1.1.1 detail 1.1.1.10 300 ...

So I use codes like this:

#
# 'bgp' group
#

@cli.group()
def bgp():
    """BGP-related configuration tasks"""
    pass

#
# 'neighbor' subcommand
#
@bgp.command()
@click.pass_context
@click.argument('neighbor_addr', type=str, required=True)
def neighboraddr(neighbor_addr):
    """Config BGP neighbor infomations"""
    ctx.obj['NEIGHBOR_ADDR'] = neighbor_addr

# neighborinfo subcommand
@neighboraddr.command()
@click.pass_context
@click.argument('name', type=str, required=True)
@click.argument('local_peer_addr', metavar='<ipaddr_or_hostname>', required=True)
@click.argument('asn', type=int, required=True)
@click.argument('admin_status', type=str, required=False)
@click.argument('rrclient', type=int, required=False)
@click.argument('nhopself', type=int, required=False)
@click.argument('holdtime', type=int, required=False)
@click.argument('keepalive', type=int, required=False)
def neighborinfo(ctx, name, local_peer_addr, asn, admin_status, rrclient,
    nhopself, holdtime, keepalive):
    """Config BGP neighbor infomations details"""
    _config_bgp_neighbor_info(ctx.obj['NEIGHBOR_ADDR'], name, local_peer_addr, asn,
        admin_status, rrclient, nhopself, holdtime, keepalive)

But I got errors like this:

root@switch1:~# config bgp neighboraddr 1.1.1.1 neighborinfo ? Traceback (most recent call last): File "/usr/bin/config", line 9, in load_entry_point('sonic-utilities==1.2', 'console_scripts', 'config')() File "/usr/lib/python2.7/dist-packages/click/core.py", line 716, in call return self.main(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/click/core.py", line 696, in main rv = self.invoke(ctx) File "/usr/lib/python2.7/dist-packages/click/core.py", line 1060, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/lib/python2.7/dist-packages/click/core.py", line 1060, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/lib/python2.7/dist-packages/click/core.py", line 1057, in invoke Command.invoke(self, ctx) File "/usr/lib/python2.7/dist-packages/click/core.py", line 889, in invoke return ctx.invoke(self.callback, **ctx.params) File "/usr/lib/python2.7/dist-packages/click/core.py", line 534, in invoke return callback(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/click/decorators.py", line 17, in new_func return f(get_current_context(), *args, **kwargs) TypeError: neighboraddr() got multiple values for keyword argument 'neighbor_addr'

So how to define this command?

1
@click.argument('neighbor_addr', type=str, required=True, nargs=2)?Georgy
No... This could not workbatmancn
what does ... mean?Azat Ibrakov

1 Answers

0
votes

You missed ctx argument in neighboraddr command.

@bgp.command()
@click.pass_context
@click.argument('neighbor_addr', type=str, required=True)
def neighboraddr(ctx, neighbor_addr):
    """Config BGP neighbor infomations"""
    ctx.obj['NEIGHBOR_ADDR'] = neighbor_addr