AttributeError:“函数”对象在使用“单击”创建分层命令组时没有属性“name”

2024-09-28 21:03:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个带有层次命令的命令行应用程序(我的子命令将有子命令)。但是,当我尝试一个非常基本的应用程序时,我得到一个AttributeError。在

我可以用一个简单的例子来复制这一点。在

目录布局:

.
├── cli.py
└── commands
    ├── config_cmds.py
    ├── __init__.py

在cli.py文件

^{pr2}$

配置_命令.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'

我的目录结构是基于this答案设置的。在

我使用的是python3.6并单击version7.0。在

如何解决此属性错误,以便可以有一个命令层次结构并将命令拆分为多个文件?在


Tags: 文件namepy命令目录config应用程序cli
1条回答
网友
1楼 · 发布于 2024-09-28 21:03:38

您需要调用click.group()修饰符,如下所示:

@click.group()
@click.version_option()
def configcmd():
    """Configuration management for this CLI"""
    click.echo("In config")

测试代码:

^{pr2}$

结果:

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

相关问题 更多 >