I am trying to create a command line application with hierarchical commands (my sub-commands will have sub-commands). However, when I attempt a very basic application, I get an AttributeError
.
I am able to replicate this with a simple example.
Directory layout:
.
├── 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")
If I run this application, I get the following error:
$ 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'
My directory structure is set up based on this answer.
I am using python 3.6 and Click version 7.0.
How do I resolve this attribute error so that I can have a hierarchy of commands and keep the commands split into multiple files?