I have one large click application that I\'ve developed, but navigating through the different commands/subcommands is getting rough. How do I organize my commands into separ
It took me a while to figure this out but I figured I'd put this here to remind myself when I forget how to do i again I think part of the problem is that the add_command function is mentioned on click's github page but not the main examples page
first lets create an initial python file called root.py
import click
from cli_compile import cli_compile
from cli_tools import cli_tools
@click.group()
def main():
"""Demo"""
if __name__ == '__main__':
main.add_command(cli_tools)
main.add_command(cli_compile)
main()
Next lets put some tools commands in a file called cli_tools.py
import click
# Command Group
@click.group(name='tools')
def cli_tools():
"""Tool related commands"""
pass
@cli_tools.command(name='install', help='test install')
@click.option('--test1', default='1', help='test option')
def install_cmd(test1):
click.echo('Hello world')
@cli_tools.command(name='search', help='test search')
@click.option('--test1', default='1', help='test option')
def search_cmd(test1):
click.echo('Hello world')
if __name__ == '__main__':
cli_tools()
Next lets put some compile commands in a file called cli_compile.py
import click
@click.group(name='compile')
def cli_compile():
"""Commands related to compiling"""
pass
@cli_compile.command(name='install2', help='test install')
def install2_cmd():
click.echo('Hello world')
@cli_compile.command(name='search2', help='test search')
def search2_cmd():
click.echo('Hello world')
if __name__ == '__main__':
cli_compile()
running root.py should now give us
Usage: root.py [OPTIONS] COMMAND [ARGS]...
Demo
Options:
--help Show this message and exit.
Commands:
compile Commands related to compiling
tools Tool related commands
running "root.py compile" should give us
Usage: root.py compile [OPTIONS] COMMAND [ARGS]...
Commands related to compiling
Options:
--help Show this message and exit.
Commands:
install2 test install
search2 test search
You'll also notice you can run the cli_tools.py or cli_compile.py directly as well as I included a main statement in there