How to add multiple blank lines to the end of the usage message produced by the Python click module?

折月煮酒 提交于 2020-01-24 21:30:26

问题


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>

回答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)


来源:https://stackoverflow.com/questions/44124680/how-to-add-multiple-blank-lines-to-the-end-of-the-usage-message-produced-by-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!