如何解决具有Click命令别名的常规用法输出
我正在将此代码段用于我的自定义组(来自here)以允许使用前缀。
class AliasedGroup(click.Group):
def get_command(self,ctx,cmd_name):
rv = click.Group.get_command(self,cmd_name)
if rv is not None:
return rv
matches = [x for x in self.list_commands(ctx)
if x.startswith(cmd_name)]
if not matches:
return None
elif len(matches) == 1:
return click.Group.get_command(self,matches[0])
ctx.fail('Too many matches: %s' % ','.join(sorted(matches)))
使用情况输出实际上是愚蠢的:它显示命令的前缀而不是完整显示它们:
用法:test_core a c [选项]
我想看
用法:test_core组合添加[OPTIONS]
即使我打test_core a c -h
。
我已经调查过了,看起来没有一个明显的解决方案。格式化程序逻辑不知道其原始名称。也许可以MultiCommand.resolve_command
被覆盖以处理MultiCommand/Group.get_command
的覆盖版本,该版本也返回原始命令名。但这可能会破坏某些事情,也许有一些更简单的方法。
完整代码:
import click
class AliasedGroup(click.Group):
def get_command(self,cmd_name)
if rv is not None:
return rv
matches = [x for x in self.list_commands(ctx)
if x.startswith(cmd_name)]
if not matches:
return None
elif len(matches) == 1:
return click.Group.get_command(self,'.join(sorted(matches)))
@click.group(cls=AliasedGroup,context_settings={'help_option_names': ['-h','--help']})
def cli():
pass
@cli.group(cls=AliasedGroup)
def add():
pass
@add.command()
@click.option('--yarr')
def combined():
pass
cli(['a','c','-h'],prog_name='test_core')
解决方法
您需要跟踪使用的别名。
由于click
使用了大量上下文实例,因此别名保留在全局变量中。
您需要实现自己的HelpFormatter。这涵盖了帮助构造的所有用途。
在write_usage
中,将别名替换为完整的命令名称。跟踪填充的别名以覆盖test_core a a -h
的情况,作为test_core add auto -h
的命令。如果在prog
中找不到别名,请不要尝试使用下一个别名(while
而不是for
)。
import click
clickAliases = []
class AliasedGroup(click.Group):
def get_command(self,ctx,cmd_name):
rv = click.Group.get_command(self,cmd_name)
if rv is not None:
return rv
matches = [x for x in self.list_commands(ctx)
if x.startswith(cmd_name)]
if not matches:
return None
elif len(matches) == 1:
clickAliases.append((cmd_name,matches[0]))
return click.Group.get_command(self,matches[0])
ctx.fail('Too many matches: %s' % ','.join(sorted(matches)))
class MyHelpFormatter(click.HelpFormatter):
def write_usage(self,prog,args="",prefix="Usage: "):
if clickAliases:
parts = prog.split()
partIdx = 0
for alias,cmd in clickAliases:
while partIdx < len(parts):
if parts[partIdx] == alias:
parts[partIdx] = cmd
partIdx += 1
break
partIdx += 1
prog = ' '.join(parts)
click.HelpFormatter.write_usage(self,args,prefix)
def make_formatter(self):
return MyHelpFormatter(width=self.terminal_width,max_width=self.max_content_width)
click.Context.make_formatter = make_formatter
# version 8.x makes if easier with
# click.Context.formatter_class = MyHelpFormatter
@click.group(cls=AliasedGroup,context_settings={'help_option_names': ['-h','--help']})
def cli():
pass
@cli.group(cls=AliasedGroup)
def add():
click.echo("add command")
@add.command()
@click.option('--yarr')
def combined(yarr):
click.echo(f"combined command: {yarr}")
# simulate command arguments - for debugging
# cli(['a','c','-h'],prog_name='test_core')
# normal start
cli(prog_name='test_core')
端子输出
$ python test_core.py a c -h
add command
Usage: test_core add combined [OPTIONS]
Options:
--yarr TEXT
-h,--help Show this message and exit.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。