AttributeError: 'function' object has no attribute 'name' when using Click to create Hierarchical command groups
AttributeError: 'function' object has no attribute 'name' when using Click to create Hierarchical command groups
我正在尝试创建一个带有分层命令的命令行应用程序(我的子命令将有子命令)。但是,当我尝试一个非常基本的应用程序时,我得到一个 AttributeError
.
我可以用一个简单的例子来复制这个。
目录布局:
.
├── cli.py
└── commands
├── config_cmds.py
├── __init__.py
cli.py
# -*- coding: utf-8 -*-
import sys
import click
from commands.config_cmds import configcmd
@click.group()
@click.version_option()
def cli(args=None):
"""A command line application"""
return 0
cli.add_command(configcmd)
if __name__ == "__main__":
sys.exit(cli()) # pragma: no cover
config_cmds.py
import click
@click.group
@click.version_option()
def configcmd():
"""Configuration management for this CLI"""
click.echo("In config")
如果我运行这个应用程序,我得到以下错误:
$ python cli.py
Traceback (most recent call last):
File "cli.py", line 15, in <module>
cli.add_command(configcmd)
File "/home/frank/.virtualenvs/clitest/lib/python3.6/site-packages/click/core.py", line 1221, in add_command
name = name or cmd.name
AttributeError: 'function' object has no attribute 'name'
我的目录结构是根据答案设置的。
我正在使用 python 3.6 和 Click 版本 7.0。
如何解决此属性错误,以便我可以拥有命令层次结构并将命令拆分为多个文件?
您需要调用 click.group()
装饰器,例如:
@click.group()
@click.version_option()
def configcmd():
"""Configuration management for this CLI"""
click.echo("In config")
测试代码:
import sys
import click
from commands.config_cmd import configcmd
@click.group()
@click.version_option()
def cli(args=None):
"""A command line application"""
return 0
cli.add_command(configcmd)
@configcmd.command()
def test_cmd():
click.echo('In test_cmd')
if __name__ == "__main__":
commands = (
'configcmd test_cmd',
'configcmd --help',
'--help',
'',
)
import sys, time
time.sleep(1)
print('Click Version: {}'.format(click.__version__))
print('Python Version: {}'.format(sys.version))
for cmd in commands:
try:
time.sleep(0.1)
print('-----------')
print('> ' + cmd)
time.sleep(0.1)
cli(cmd.split())
except BaseException as exc:
if str(exc) != '0' and \
not isinstance(exc, (click.ClickException, SystemExit)):
raise
结果:
Click Version: 6.7
Python Version: 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
-----------
> configcmd test_cmd
In config
In test_cmd
-----------
> configcmd --help
Usage: test.py configcmd [OPTIONS] COMMAND [ARGS]...
Configuration management for this CLI
Options:
--version Show the version and exit.
--help Show this message and exit.
Commands:
test_cmd
-----------
> --help
Usage: test.py [OPTIONS] COMMAND [ARGS]...
A command line application
Options:
--version Show the version and exit.
--help Show this message and exit.
Commands:
configcmd Configuration management for this CLI
-----------
>
Usage: test.py [OPTIONS] COMMAND [ARGS]...
A command line application
Options:
--version Show the version and exit.
--help Show this message and exit.
Commands:
configcmd Configuration management for this CLI
我正在尝试创建一个带有分层命令的命令行应用程序(我的子命令将有子命令)。但是,当我尝试一个非常基本的应用程序时,我得到一个 AttributeError
.
我可以用一个简单的例子来复制这个。
目录布局:
.
├── cli.py
└── commands
├── config_cmds.py
├── __init__.py
cli.py
# -*- coding: utf-8 -*-
import sys
import click
from commands.config_cmds import configcmd
@click.group()
@click.version_option()
def cli(args=None):
"""A command line application"""
return 0
cli.add_command(configcmd)
if __name__ == "__main__":
sys.exit(cli()) # pragma: no cover
config_cmds.py
import click
@click.group
@click.version_option()
def configcmd():
"""Configuration management for this CLI"""
click.echo("In config")
如果我运行这个应用程序,我得到以下错误:
$ python cli.py
Traceback (most recent call last):
File "cli.py", line 15, in <module>
cli.add_command(configcmd)
File "/home/frank/.virtualenvs/clitest/lib/python3.6/site-packages/click/core.py", line 1221, in add_command
name = name or cmd.name
AttributeError: 'function' object has no attribute 'name'
我的目录结构是根据
我正在使用 python 3.6 和 Click 版本 7.0。
如何解决此属性错误,以便我可以拥有命令层次结构并将命令拆分为多个文件?
您需要调用 click.group()
装饰器,例如:
@click.group()
@click.version_option()
def configcmd():
"""Configuration management for this CLI"""
click.echo("In config")
测试代码:
import sys
import click
from commands.config_cmd import configcmd
@click.group()
@click.version_option()
def cli(args=None):
"""A command line application"""
return 0
cli.add_command(configcmd)
@configcmd.command()
def test_cmd():
click.echo('In test_cmd')
if __name__ == "__main__":
commands = (
'configcmd test_cmd',
'configcmd --help',
'--help',
'',
)
import sys, time
time.sleep(1)
print('Click Version: {}'.format(click.__version__))
print('Python Version: {}'.format(sys.version))
for cmd in commands:
try:
time.sleep(0.1)
print('-----------')
print('> ' + cmd)
time.sleep(0.1)
cli(cmd.split())
except BaseException as exc:
if str(exc) != '0' and \
not isinstance(exc, (click.ClickException, SystemExit)):
raise
结果:
Click Version: 6.7
Python Version: 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
-----------
> configcmd test_cmd
In config
In test_cmd
-----------
> configcmd --help
Usage: test.py configcmd [OPTIONS] COMMAND [ARGS]...
Configuration management for this CLI
Options:
--version Show the version and exit.
--help Show this message and exit.
Commands:
test_cmd
-----------
> --help
Usage: test.py [OPTIONS] COMMAND [ARGS]...
A command line application
Options:
--version Show the version and exit.
--help Show this message and exit.
Commands:
configcmd Configuration management for this CLI
-----------
>
Usage: test.py [OPTIONS] COMMAND [ARGS]...
A command line application
Options:
--version Show the version and exit.
--help Show this message and exit.
Commands:
configcmd Configuration management for this CLI