我正在尝试在单击中注册命令行选项。一切都很好,直到我将params
参数添加到构造函数。
class InitCommand(click.Command):
def __init__(self):
super().__init__(
name='init',
short_help='Initialize the needed scaffolding.',
help='something helpful, but longer',
# params=[] ### <-- Works fine
params=[click.Option('--force', default=False)]
)
我有以下错误:
Traceback (most recent call last):
File "/usr/local/bin/aquapy", line 11, in <module>
load_entry_point('aquapy-cli', 'console_scripts', 'aquapy')()
File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 561, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2631, in load_entry_point
return ep.load()
File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2291, in load
return self.resolve()
File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2297, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Users/******/__main__.py", line 3, in <module>
from .bootstrap import main
File "/Users/******/bootstrap.py", line 17, in <module>
main.add_command(InitCommand())
File "/Users/******/commands/init_command.py", line 10, in __init__
params=[click.Option('--force', default=False)]
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1460, in __init__
Parameter.__init__(self, param_decls, type=type, **attrs)
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1266, in __init__
self._parse_decls(param_decls or (), expose_value)
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1533, in _parse_decls
raise TypeError('Name defined twice')
TypeError: Name defined twice
问题是click.Option()
期望第一个参数列为列表,而不是Decorator版本@click.option()
。
params=[click.Option(['--force'], default=False)]
将命令选项包装在列表中解决了问题。