2017-05-23 4 views
1

Ich habe eine Frage, die etwas ähnlich zu this SO Q&A ist, aber ich möchte zusätzliche Leerzeilen zu einem Epilog am Ende der Ausgabe von click erzeugt werden.Wie fügt man am Ende der vom Python-Click-Modul erzeugten Verwendungsnachricht mehrere leere Zeilen hinzu?

Ich habe den folgenden 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) 

Daraus ergibt sich die folgende Nutzungs Ausgabe:

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> 

Meine Frage:

Ich kann keine Methode, herauszufinden, Das erfordert kein druckbares Zeichen Ters. Wenn ich die obige Bindestrich-Sequenz entferne, werden die Zeilenumbruchzeichen (\n) nicht mehr angezeigt. Mit anderen Worten geht die oben Nutzung dieses:

... 
Commands: 
    agent API calls focusing on assets' details - Works... 
    os  API calls focusing on operating systems -... 
    vuln API calls focusing on vulnerabilities - Works... 
$ myprompt> 

Antwort

1

Das Problem ist, dass click macht eine Optimierung keine Leerzeilen am Ende der Hilfe zu entfernen. Das Verhalten ist in click.Command.get_help() und kann wie außer Kraft gesetzt werden:

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

Testcode:

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']) 

Aber alles was ich brauche ist Rohlingen, kein Epilog:

Wenn alles Sie müssen, sind einige Leerzeilen, dann können wir den Epilog ganz ignorieren und einfach get_help() gleiche wie hinzufügen ändern:

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) 
+1

Dank war das zweite Stück genau das, was ich wollte! – slm

Verwandte Themen