如何将每个带有一组子命令的Click命令拆分为多个文件? [英] How can I split my Click commands, each with a set of sub-commands, into multiple files?

查看:60
本文介绍了如何将每个带有一组子命令的Click命令拆分为多个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个大型点击应用程序,但是浏览不同的命令/子命令变得越来越困难.如何将命令组织到单独的文件中?是否可以将命令及其子命令组织到单独的类中?

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 separate files? Is it possible to organize commands and their subcommands into separate classes?

以下是我想如何将其分开的示例:

Here's an example of how I would like to separate it:

import click

@click.group()
@click.version_option()
def cli():
    pass #Entry Point

command_cloudflare.py

@cli.group()
@click.pass_context
def cloudflare(ctx):
    pass

@cloudflare.group('zone')
def cloudflare_zone():
    pass

@cloudflare_zone.command('add')
@click.option('--jumpstart', '-j', default=True)
@click.option('--organization', '-o', default='')
@click.argument('url')
@click.pass_obj
@__cf_error_handler
def cloudflare_zone_add(ctx, url, jumpstart, organization):
    pass

@cloudflare.group('record')
def cloudflare_record():
    pass

@cloudflare_record.command('add')
@click.option('--ttl', '-t')
@click.argument('domain')
@click.argument('name')
@click.argument('type')
@click.argument('content')
@click.pass_obj
@__cf_error_handler
def cloudflare_record_add(ctx, domain, name, type, content, ttl):
    pass

@cloudflare_record.command('edit')
@click.option('--ttl', '-t')
@click.argument('domain')
@click.argument('name')
@click.argument('type')
@click.argument('content')
@click.pass_obj
@__cf_error_handler
def cloudflare_record_edit(ctx, domain):
    pass

command_uptimerobot.py

@cli.group()
@click.pass_context
def uptimerobot(ctx):
    pass

@uptimerobot.command('add')
@click.option('--alert', '-a', default=True)
@click.argument('name')
@click.argument('url')
@click.pass_obj
def uptimerobot_add(ctx, name, url, alert):
    pass

@uptimerobot.command('delete')
@click.argument('names', nargs=-1, required=True)
@click.pass_obj
def uptimerobot_delete(ctx, names):
    pass

推荐答案

为此使用CommandCollection的缺点是它会合并您的命令,并且仅适用于命令组. imho更好的替代方法是使用add_command获得相同的结果.

The downside of using CommandCollection for this is that it merges your commands and works only with command groups. The imho better alternative is to use add_command to achieve the same result.

我有一个带有以下树的项目:

I have a project with the following tree:

cli/
├── __init__.py
├── cli.py
├── group1
│   ├── __init__.py
│   ├── commands.py
└── group2
    ├── __init__.py
    └── commands.py

每个子命令都有其自己的模块,这使得使用更多帮助程序类和文件甚至管理复杂的实现变得异常简单.在每个模块中,commands.py文件包含@click批注.示例group2/commands.py:

Each subcommand has its own module, what makes it incredibly easy to manage even complex implementations with many more helper classes and files. In each module, the commands.py file contains the @click annotations. Example group2/commands.py:

import click


@click.command()
def version():
    """Display the current version."""
    click.echo(_read_version())

如有必要,您可以轻松地在模块中创建更多的类,然后在这里使用import并在其中使用它们,从而为您的CLI提供Python的类和模块的全部功能.

If necessary, you could easily create more classes in the module, and import and use them here, thus giving your CLI the full power of Python's classes and modules.

我的cli.py是整个CLI的入口点:

My cli.py is the entry point for the whole CLI:

import click

from .group1 import commands as group1
from .group2 import commands as group2

@click.group()
def entry_point():
    pass

entry_point.add_command(group1.command_group)
entry_point.add_command(group2.version)

使用此设置,可以很轻松地按关注点分离命令,并围绕它们构建可能需要的其他功能.到目前为止,它为我提供了很好的服务...

With this setup, it is very easy to separate your commands by concerns, and also build additional functionality around them that they might need. It has served me very well so far...

参考: http://click.pocoo.org/6/quickstart/#nesting-commands

这篇关于如何将每个带有一组子命令的Click命令拆分为多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆