Fixed #33657 -- Allowed customizing formatter class of argument parsers.

This commit is contained in:
Abhinav Yadav 2022-06-20 21:04:52 +05:30 committed by GitHub
parent d7f5bfd241
commit 2887b9f67c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -286,10 +286,10 @@ class BaseCommand:
Create and return the ``ArgumentParser`` which will be used to Create and return the ``ArgumentParser`` which will be used to
parse the arguments to this command. parse the arguments to this command.
""" """
kwargs.setdefault("formatter_class", DjangoHelpFormatter)
parser = CommandParser( parser = CommandParser(
prog="%s %s" % (os.path.basename(prog_name), subcommand), prog="%s %s" % (os.path.basename(prog_name), subcommand),
description=self.help or None, description=self.help or None,
formatter_class=DjangoHelpFormatter,
missing_args_message=getattr(self, "missing_args_message", None), missing_args_message=getattr(self, "missing_args_message", None),
called_from_command_line=getattr(self, "_called_from_command_line", None), called_from_command_line=getattr(self, "_called_from_command_line", None),
**kwargs, **kwargs,

View File

@ -1,4 +1,5 @@
import os import os
from argparse import ArgumentDefaultsHelpFormatter
from io import StringIO from io import StringIO
from unittest import mock from unittest import mock
@ -408,8 +409,14 @@ class CommandTests(SimpleTestCase):
def test_create_parser_kwargs(self): def test_create_parser_kwargs(self):
"""BaseCommand.create_parser() passes kwargs to CommandParser.""" """BaseCommand.create_parser() passes kwargs to CommandParser."""
epilog = "some epilog text" epilog = "some epilog text"
parser = BaseCommand().create_parser("prog_name", "subcommand", epilog=epilog) parser = BaseCommand().create_parser(
"prog_name",
"subcommand",
epilog=epilog,
formatter_class=ArgumentDefaultsHelpFormatter,
)
self.assertEqual(parser.epilog, epilog) self.assertEqual(parser.epilog, epilog)
self.assertEqual(parser.formatter_class, ArgumentDefaultsHelpFormatter)
def test_outputwrapper_flush(self): def test_outputwrapper_flush(self):
out = StringIO() out = StringIO()