python自动换行符_python单击帮助格式化换行符

I am seeing that a newline is not being preserved in my EPILOG? I want to know why if I see that newline remains only when a line has 74 characters?

# 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()

Output is:

/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

解决方案

Your newlines are not being preserved because the epilog writer does word wrapping. This can be solved with a subclass to click.Group, by creating a format_epilog() which does not do word wrapping:

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)

....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值