Skip to content

Commit 82e00f0

Browse files
committed
fix: handle backward-compatibility for format_* methods
Signed-off-by: Frost Ming <[email protected]>
1 parent 364a84f commit 82e00f0

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Lib/argparse.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,13 +2737,25 @@ def print_usage(self, file=None):
27372737
if file is None:
27382738
file = _sys.stdout
27392739
formatter = self._get_formatter(file=file)
2740-
self._print_message(self.format_usage(formatter=formatter), file)
2740+
try:
2741+
usage_text = self.format_usage(formatter=formatter)
2742+
except TypeError:
2743+
# Backward compatibility for formatter classes that
2744+
# do not accept the 'formatter' keyword argument.
2745+
usage_text = self.format_usage()
2746+
self._print_message(usage_text, file)
27412747

27422748
def print_help(self, file=None):
27432749
if file is None:
27442750
file = _sys.stdout
27452751
formatter = self._get_formatter(file=file)
2746-
self._print_message(self.format_help(formatter=formatter), file)
2752+
try:
2753+
help_text = self.format_help(formatter=formatter)
2754+
except TypeError:
2755+
# Backward compatibility for formatter classes that
2756+
# do not accept the 'formatter' keyword argument.
2757+
help_text = self.format_help()
2758+
self._print_message(help_text, file)
27472759

27482760
def _print_message(self, message, file=None):
27492761
if message:

0 commit comments

Comments
 (0)