Skip to content Skip to sidebar Skip to footer

How To Add Multiple Blank Lines To The End Of The Usage Message Produced By The Python Click Module?

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 foll

Solution 1:

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)

Post a Comment for "How To Add Multiple Blank Lines To The End Of The Usage Message Produced By The Python Click Module?"