python添加空行_如何在Python click模块生成的用法消息的末尾添加多个空行?

博客围绕在Python中使用click库为输出添加空行的问题展开。原代码添加空行时遇到问题,移除特定字符后换行符不显示。给出解决方案,可通过重写click.Command.get_help()方法,或直接修改get_help()添加空行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a question that's somewhat similar to this SO Q&A, however I want to add additional blank lines to an epilog at the end of the output generated by click.

I have the following code:

EPILOG='\n' + '-' * 20

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)

#------------------

@click.group(cls=SpecialEpilog, epilog=EPILOG, invoke_without_command=True)

def cli():

"""Wraps cloud.tenable.com Nessus API calls in useful ways

\b

The CLI provides access to these subcommands:

- agent

- os

- vuln

Each subcommand can perform useful API queries within their respective domain.

"""

pass

#------------------

# main

cli.add_command(os)

cli.add_command(agent)

cli.add_command(vuln)

This produces the following usage output:

Usage: nessus_query [OPTIONS] COMMAND [ARGS]...

Wraps cloud.tenable.com Nessus API calls in useful ways

The CLI provides access to these subcommands:

- agent

- os

- vuln

Each subcommand can perform useful API queries within their respective

domain.

Options:

--help Show this message and exit.

Commands:

agent API calls focusing on assets' details - Works...

os API calls focusing on operating systems -...

vuln API calls focusing on vulnerabilities - Works...

--------------------

$ myprompt>

My question:

I cannot figure out a method which doesn't require printable characters. If I remove the dash sequence above, the newline characters (\n) no longer get displayed. In other words the above usage goes to this:

...

Commands:

agent API calls focusing on assets' details - Works...

os API calls focusing on operating systems -...

vuln API calls focusing on vulnerabilities - Works...

$ myprompt>

解决方案

The issue is that click does an optimization to remove any blank lines at the end of the help. The behavior is in click.Command.get_help() and can be overridden like:

Code:

class SpecialEpilog(click.Group):

def get_help(self, ctx):

""" standard get help, but without rstrip """

formatter = ctx.make_formatter()

self.format_help(ctx, formatter)

return formatter.getvalue()

Test Code:

import click

EPILOG = '\n\n'

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)

def get_help(self, ctx):

""" standard get help, but without rstrip """

formatter = ctx.make_formatter()

self.format_help(ctx, formatter)

return formatter.getvalue()

@click.group(cls=SpecialEpilog, epilog=EPILOG, invoke_without_command=True)

def cli():

pass

@cli.command()

def os(*args, **kwargs):

pass

@cli.command()

def agent(*args, **kwargs):

pass

cli(['--help'])

But all I need is blanks, not an epilog:

If all you need is some blank lines, then we can ignore the epilog altogether and simply modify get_help() to add same like:

class AddSomeBlanksToHelp(click.Group):

def get_help(self, ctx):

return super(AddSomeBlanksToHelp, self).get_help(ctx) + '\n\n'

@click.group(cls=AddSomeBlanksToHelp, invoke_without_command=True)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值