Python Click 帮助格式化换行问题

16

我发现我的EPILOG里没有保留换行?我想知道为什么只有当一行有74个字符时,换行符才会保留?

我看到EPILOG中没有保留换行符,想知道为什么只有当一行长度为74个字符时才能保留?请问原因是什么?

# http://click.pocoo.org/5/commands/

import click, sys

def main_caller(*args, **kwargs):
    print('act on arguments', args, kwargs)

EPILOG = '''
# oneline
# twoline

\n
# oneline with 74char                                                   x
# twoline with 74char                                                   x
'''

@click.group(help='wwwwwwwwww', epilog=EPILOG, invoke_without_command=True, chain=True)
@click.argument('start_or_stop')
@click.option('-v', '--verbose', default=False, help='Print Verbose messages')
@click.option('-l', '--logfile', help='Path to logfile to store log messages')
@click.option('-a', '--action', multiple=True, type=click.Choice(['act1', 'act2', 'act3']), default=['act1', 'act2'])
def cli(*args, **kwargs):
    '''foo bar'''
    pass

@cli.command()
@click.option('--debug/--no-debug', default=False)
def cmd1(*args, **kwargs):
    print('cmd1', args, kwargs)
    return 'cmd11111'

@cli.command()
@click.option('-x', '--xxx', default='x')
def cmd2(*args, **kwargs):
    print('cmd2', args, kwargs)
    return 'cmd22222'

@cli.resultcallback()
def process_pipeline(*args, **kwargs):
    print('process', args, kwargs)
    print('args', sys.argv[1:])     

if __name__ == '__main__':
    cli()

输出结果为:

/click_sandbox.py --help
2017/02/24 19:31:43 Platform overridden to 'RHEL5_64'
Usage: click_sandbox.py [OPTIONS] START_OR_STOP COMMAND1 [ARGS]... [COMMAND2
                        [ARGS]...]...

  wwwwwwwwww

Options:
  -v, --verbose TEXT             Print Verbose messages
  -l, --logfile TEXT             Path to logfile to store log messages
  -a, --action [act1|act2|act3]
  --help                         Show this message and exit.

Commands:
  cmd1
  cmd2

  # oneline # twoline

  # oneline with 74char                                                   x
  # twoline with 74char                                                   x
2个回答

28
在 Click 7.0 中,您可以使用 \b 标记来指示格式保留,例如:
This paragraph is formatted normally and Click does
not preserve new lines.

\b
This paragraph is formatted as it
appears in the source:
item 1
item 2

This paragraph is formatted normally and Click does
not preserve new lines.

从文档中看来,这个功能似乎在所有版本中都可用。 - Alex
1
从对Click 7.0的实验中,似乎需要在帮助文本中触发的序列是'\b\n',即退格键后跟换行符。 - Andreas Maier
4
@AndreasMaier - 在我的帮助字符串前加上\b\n可以防止重新换行。谢谢! - jkr
1
点击此处讨论重新包装链接:https://click.palletsprojects.com/en/8.0.x/documentation/#preventing-rewrapping - Sumanth Lingappa

5

您的换行符未被保留,因为 epilog writer 进行了单词换行。这可以通过创建 click.Group 的子类并创建一个不进行单词换行的 format_epilog() 方法来解决:

class SpecialEpilog(click.Group):
    def format_epilog(self, ctx, formatter):
        if self.epilog:
            formatter.write_paragraph()
            for line in self.epilog.split('\n'):
                formatter.write_text(line)

# Tell click to use our epilog formatter
@click.group(cls=SpecialEpilog,
    help='wwwwwwwwww', epilog=EPILOG, invoke_without_command=True, chain=True)
....

只要EPILOG有可打印字符,这个方法就有效。如果我只加入几个换行符('\n\n\n'),除非我用可打印字符锚定它们,否则它什么也不做。有什么想法可以修改上面的代码以类似的方式添加几个换行符呢? - slm
@slm,好问题。这不是我考虑过的用例。浏览框架后,答案似乎是肯定的,但需要更多的覆盖。如果你有问题,请随时提出,我很乐意写出答案。 - Stephen Rauch
@StephenRauch - 当然没问题,最近我一直在努力解决这个问题,但是发现没有简单的方法可以实现(我对Python还比较陌生)。 - slm
@StephenRauch - https://dev59.com/06Hia4cB1Zd3GeqPbvZ7 - slm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接