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