Error: Got unexpected extra argument (main-func)

拟墨画扇 提交于 2020-01-22 02:32:40

问题


I'm trying to call a function from a package module inside the command of click group. But, getting the following error:

Error: Got unexpected extra argument (main-func)

Updated: Let say I have a package that contains a module mod.py. The module has a click command mod_func that 'll take user input and print & return it. The package is initialized with importing the command from the module.

Next, I want to import the package inside another module, let's say tool.py, and want to call mod_func using another click command main_func.

I can call the main_func command using python tool.py main_func and it is throwing the above-mentioned error. How can I import a package and call a click command from its module inside another module?

Task: The goal is to write a modular code for the CLI app. In the package I want to have config modules, for example, a module to config mqtt connection and to subscribe to topics based on user input. I want to run these modules from the core module. Once the mqtt connection is established (the execution of mqtt connection module is successful from the core module), I'll call some other commands from the core module to send the topics data to the database, for example.

The project structure is:

.
├── __init__.py
├── pkg
│   ├── __init__.py
│   └── mod.py
└── tool.py

In package pkg, the contents of mod.py are:

import click


@click.command()
@click.option('--port', prompt='port', default=8545, help='Port for HTTP connections')
def mod_func(port):
    """
    Setting web3 provider for connecting to local and remote JSON-RPC servers.
    """

    print(port)
    return port

and __init__.py is:

from .mod import mod_func

The contents of tool.py are:

import click

import pkg as pkg

cli = click.Group()


@cli.command()
def main_func():
    print('Setting up web3 Provider')
    pkg.mod_func()


if __name__ == '__main__':
    cli()

I'm getting following output when running $ python tool.py main-func:

Setting up web3 Provider
port [8545]: 2
Usage: tool.py [OPTIONS]
Try "tool.py --help" for help.

Error: Got unexpected extra argument (main-func)

any help how can I fix this? thanks

来源:https://stackoverflow.com/questions/59581531/error-got-unexpected-extra-argument-main-func

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!