问题
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