Salut à tous !

Je dois traduire la sortie d’aide produite par ArgParse pour l’application dont le dépôt est disponible à l’adresse suivante :

https://framagit.org/python-with-fra...ref_type=heads

(Normalement, le lien dirige vers la branche en cours de modification, où je mets en place les traductions.)

Je parviens à traduire l’intégralité des différentes chaînes de caractères, à l’exception de la chaine « default » qui apparaît lorsqu’un paramètre de l’application a une valeur par défaut. Le fond de mon problème, c’est de trouver où est définie la chaîne de caractères à afficher pour annoncer une valeur par défaut. J’ai fait pas mal de recherche sur le net et aussi essayer de chercher dans le code source de ArgParse, mais je ne parviens pas à trouver où cette chaîne de caractère est définie.

A priori, il faut que je fixe la bonne chaîne de caractère dans la fonction « create_parser » dans le fichier « hformatter.py ». Voici le contenu de ce fichier :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
"""
Utility class to format help text.
 
:module: hformatter
:author: Le*Bars, Yoann
 
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public Licence  as published by the
Free Software Foundation; either version 3 of the Licence, or (at your
option) any later version. See file `LICENSE` or go to:
 
https://www.gnu.org/licenses/gpl-3.0.html
"""
 
 
from typing import Iterable
import argparse
 
 
# Title for usage section.
usage_string: str = 'Usage: '
 
 
class HelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
    """
    Class to format help output.
 
    :param str __usage_string: Title for usage section.
    """
 
    __usage_string: str
 
    def __init__(self, prog: str, indent_increment: int = 2, max_help_position: int = 24,
                 width: int | None = None) -> None:
        self.__usage_string = usage_string
        super().__init__(prog, indent_increment, max_help_position, width)
 
    def add_usage(self, usage: str, actions: Iterable[argparse.Action],
                  groups: Iterable[argparse._ArgumentGroup],
                  prefix: str | None = None) -> None:
        """
        Reformat usage message.
 
        :param str usage: Program command line description.
        :param str actions: Action identifier.
        :param str groups: Groups identifier.
        :param str prefix: Prefix to usage explanation.
 
        :returns: Object describing usage string.
        :rtype: None
        """
 
        if prefix is None:
            prefix = self.__usage_string
 
        return super().add_usage(usage, actions, groups, prefix)
 
 
def create_parser(program_description: str, positional_name: str, optional_name: str,
                  program_version: str, version_message: str,
                  help_message: str, usage_message: str = 'Usage: ') -> argparse.ArgumentParser:
    """
    Generates an argument parser.
 
    :param str program_description: String describing the aims of the program.
    :param str positional_name: String for positional arguments title.
    :param str optional_name: String for optional arguments title.
    :param str program_version: String describing program version.
    :param str version_message: String describing the version program option.
    :param str help_message: String describing the help program option.
    :param str usage_message: Title for usage section. Default to “Usage: ”
 
    :returns: An argument parser.
    :rtype: argparse.ArgumentParser
    """
 
    global usage_string
    usage_string = usage_message
    # Command line parser.
    parser = argparse.ArgumentParser(add_help=False,
                                     formatter_class=HelpFormatter,
                                     description=program_description)
    parser._positionals.title = positional_name     # pylint: disable=protected-access
    parser._optionals.title = optional_name         # pylint: disable=protected-access
    parser.add_argument('-V', '--version', action='version', version='%(prog)s ' + program_version,
                        help=version_message)
    parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS,
                        help=help_message)
 
    return parser
 
 
# Defines the public names of this module.
__all__ = ['create_parser']
Il faut que je trouve un équivalent pour « default » à la ligne 86 (qui change le titre pour les arguments positionnels) :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
parser._positionals.title = positional_name
Est-ce que quelqu’un a une idée de comment faire ?

À bientôt.