2017-10-03 4 views
2

J'utilise le argparse de Python, et je veux qu'il y ait moins d'indentation du texte d'aide d'argument. C'est ce que argparse est la génération:Comment réduire le niveau d'indentation de l'argument argument dans argparse?

$ ./help.py -h 
usage: help.py [-h] [--program-argument PROGRAM_ARGUMENT] 

Description of program 

optional arguments: 
    -h, --help   show this help message and exit 
    --program-argument PROGRAM_ARGUMENT 
         This is some help text about --program-argument. For example: 

          --program-argment "You can supply a string as the program argument" 

Je veux de générer quelque chose comme ceci:

$ ./help.py -h 
usage: help.py [-h] [--program-argument PROGRAM_ARGUMENT] 

Description of program 

optional arguments: 
    -h, --help   show this help message and exit 
    --program-argument PROGRAM_ARGUMENT 
     This is some help text about --program-argument. For example: 

      --program-argment "You can supply a string as the program argument" 

Est-ce réalisable? Ceci est mon code:

#! /usr/bin/env python 
import argparse 

HELP_TEXT = """\ 
This is some help text about --program-argument. For example: 

    --program-argment "You can supply a string as the program argument" 
""" 


if __name__ == '__main__': 
    argument_parser = argparse.ArgumentParser(
     formatter_class=argparse.RawTextHelpFormatter, 
     description=('Description of program')) 
    argument_parser.add_argument(
     '--program-argument', 
     help=HELP_TEXT 
    ) 
    args, unknown = argument_parser.parse_known_args() 

Répondre

3

argparse formatteurs prennent en charge plusieurs valeurs d'initialisation qui peuvent aider à contrôler une mise en forme. Ils dérivent tous de HelpFormatter, qui a cette méthode __init__.

class HelpFormatter(object): 
    """Formatter for generating usage messages and argument help strings. 

    Only the name of this class is considered a public API. All the methods 
    provided by the class are considered an implementation detail. 
    """ 

    def __init__(self, 
       prog, 
       indent_increment=2, 
       max_help_position=24, 
       width=None): 
    # stuff 

Le max_help_position est utilisé pour déterminer dans quelle mesure d'aide tiret sous-messages, vous pouvez donc essayer de réduire à quelque chose comme 10 ou 12 pour obtenir moins indentation de vos messages.

#!/usr/bin/env python 
import argparse 

HELP_TEXT = """\ 
This is some help text about --program-argument. For example: 

    --program-argment "You can supply a string as the program argument" 
""" 

less_indent_formatter = lambda prog: argparse.RawTextHelpFormatter(prog, max_help_position=10) 


if __name__ == '__main__': 
    argument_parser = argparse.ArgumentParser(
     formatter_class=less_indent_formatter, 
     description=('Description of program')) 
    argument_parser.add_argument(
     '--program-argument', 
     help=HELP_TEXT 
    ) 
    args, unknown = argument_parser.parse_known_args() 

Il en résulte:

usage: help.py [-h] [--program-argument PROGRAM_ARGUMENT] 

Description of program 

optional arguments: 
    -h, --help 
      show this help message and exit 
    --program-argument PROGRAM_ARGUMENT 
      This is some help text about --program-argument. For example: 

       --program-argment "You can supply a string as the program argument" 

Une valeur de 6 ressemble à ceci:

usage: help.py [-h] [--program-argument PROGRAM_ARGUMENT] 

Description of program 

optional arguments: 
    -h, --help 
     show this help message and exit 
    --program-argument PROGRAM_ARGUMENT 
     This is some help text about --program-argument. For example: 

      --program-argment "You can supply a string as the program argument" 
+0

Pourquoi 'less_indent_formatter' formatter doivent être' lambda'? Notre linter se plaignait de cela, alors j'ai changé pour 'def' mais j'ai eu des erreurs comme' length of metavar tuple ne correspond pas à nargs'. – Drew

+0

@Drew le lambda était juste le plus pratique, une méthode fonctionne aussi bien. Si vous êtes confronté au problème 'longueur of metavar', assurez-vous que l'argument de la méthode s'appelle' prog' car c'est ainsi que 'argparse' l'appellera. Si vous avez besoin d'aide, faites-le moi savoir. Je répondrai quand je serai à la maison et que j'aurai accès à un ordinateur. – birryree

+0

Ah ouais j'ai changé le nom de l'argument en 'programme'. Je n'ai pas réalisé qu'il a été passé via kwarg. – Drew