如何在“ --help”菜单中定义单击子命令的顺序。 [英] How can I define the order of click sub-commands in "--help"
问题描述
我有这样的代码:
import click
@click.group()
def entry_point():
pass
entry_point.add_command(lidtk.data.download_documents.main)
entry_point.add_command(lidtk.data.create_ml_dataset.main)
entry_point.add_command(lidtk.classifiers.text_cat.textcat_ngram.cli)
可提供帮助文本:
lidtk --help
Usage: lidtk [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
create-dataset Create sharable dataset from downloaded...
download Download 1000 documents of each language.
textcat
这是全部,与我的内容非常接近想。但我想将顺序更改为:
which is over all pretty close to what I want. But I would like to change the order to:
Commands:
download Download 1000 documents of each language.
create-dataset Create sharable dataset from downloaded...
textcat
如何
推荐答案
帮助列出的命令顺序由设置
方法。因此,一种满足更改帮助列表顺序需求的方法是继承 click.Group
类的list_commands() click.Group
并覆盖 list_commands
The order of the commands listed by help is set by the list_commands()
method of the click.Group
class. So, one way to approach the desire to change the help listing order is to inherit for click.Group
and override list_commands
to give the desired order.
该类会覆盖点击。 Group.command()
方法,用于装饰命令功能。它增加了指定 help_priority
的功能,该功能允许根据需要修改排序顺序:
This class overrides the click.Group.command()
method which is used to decorate command functions. It adds the ability to specify a help_priority
, which allows the sort order to be modified as desired:
class SpecialHelpOrder(click.Group):
def __init__(self, *args, **kwargs):
self.help_priorities = {}
super(SpecialHelpOrder, self).__init__(*args, **kwargs)
def get_help(self, ctx):
self.list_commands = self.list_commands_for_help
return super(SpecialHelpOrder, self).get_help(ctx)
def list_commands_for_help(self, ctx):
"""reorder the list of commands when listing the help"""
commands = super(SpecialHelpOrder, self).list_commands(ctx)
return (c[1] for c in sorted(
(self.help_priorities.get(command, 1), command)
for command in commands))
def command(self, *args, **kwargs):
"""Behaves the same as `click.Group.command()` except capture
a priority for listing command names in help.
"""
help_priority = kwargs.pop('help_priority', 1)
help_priorities = self.help_priorities
def decorator(f):
cmd = super(SpecialHelpOrder, self).command(*args, **kwargs)(f)
help_priorities[cmd.name] = help_priority
return cmd
return decorator
使用自定义类
通过 cls
参数添加到 click.group()
装饰器中,任何通过<$ c $添加到组的命令可以向c> group.command()传递 help_priority
。优先级默认为1,并且低位数字会首先打印。
Using the Custom Class
By passing the cls
parameter to the click.group()
decorator, any commands added to the group via the the group.command()
can be passed a help_priority
. The priorities default to 1, and lower numbers are printed first.
@click.group(cls=SpecialHelpOrder)
def cli():
"""My Excellent CLI"""
@cli.command(help_priority=5)
def my_command():
....
这是怎么工作的?
这是因为c lick是一个精心设计的OO框架。 @ click.group()
装饰器通常会实例化 click.Group
对象,但允许此行为被以下行为覆盖: cls
参数。因此,从我们自己的类中的 click.Group
继承并遍历所需的方法是相对容易的事情。
How does this work?
This works because click is a well designed OO framework. The @click.group()
decorator usually instantiates a click.Group
object but allows this behavior to be over ridden with the cls
parameter. So it is a relatively easy matter to inherit from click.Group
in our own class and over ride the desired methods.
此处的步骤:
- 覆盖
Group.command()
,以便修饰命令可以传递help_priority
。在重写的装饰器中,捕获所需的优先级,以供以后使用。 - 覆盖
Group.get_help()
。在重写的方法中,将Group.list_commands
替换为list_commands
,这将根据需要对命令进行排序。
- Override
Group.command()
so that decorated commands can be passed ahelp_priority
. In the over ridden decorator, capture the desired priority for later - Override
Group.get_help()
. In the over ridden method, substituteGroup.list_commands
with alist_commands
which will order the commands as desired.
测试代码:
Test Code:
import click
@click.group(cls=SpecialHelpOrder)
def cli():
pass
@cli.command()
def command1():
'''Command #1'''
@cli.command(help_priority=5)
def command2():
'''Command #2'''
@cli.command()
def command3():
'''Command #3'''
if __name__ == '__main__':
cli('--help'.split())
< h3>测试结果:
Test Results:
Usage: test.py [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
command1 Command #1
command3 Command #3
command2 Command #2
这篇关于如何在“ --help”菜单中定义单击子命令的顺序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!