summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhumbertotm <humberto.htm@gmail.com>2018-06-11 10:20:50 -0700
committerTim Graham <timograham@gmail.com>2018-06-16 15:54:59 -0400
commite95008f2411d50929873f634c3e14ebac811fd28 (patch)
tree0b158a14a3df09b1f568dcb81ac43c0a6fed5b4b
parent11bfe3a83d79c832bd861b6b87f254197fde1659 (diff)
Fixed #29152 -- Allowed passing kwargs to ArgumentParser initialization in management commands.
-rw-r--r--django/core/management/base.py3
-rw-r--r--docs/howto/custom-management-commands.txt13
-rw-r--r--tests/user_commands/tests.py6
3 files changed, 21 insertions, 1 deletions
diff --git a/django/core/management/base.py b/django/core/management/base.py
index e3374f678b..595fb29e21 100644
--- a/django/core/management/base.py
+++ b/django/core/management/base.py
@@ -244,7 +244,7 @@ class BaseCommand:
"""
return django.get_version()
- def create_parser(self, prog_name, subcommand):
+ def create_parser(self, prog_name, subcommand, **kwargs):
"""
Create and return the ``ArgumentParser`` which will be used to
parse the arguments to this command.
@@ -255,6 +255,7 @@ class BaseCommand:
formatter_class=DjangoHelpFormatter,
missing_args_message=getattr(self, 'missing_args_message', None),
called_from_command_line=getattr(self, '_called_from_command_line', None),
+ **kwargs
)
parser.add_argument('--version', action='version', version=self.get_version())
parser.add_argument(
diff --git a/docs/howto/custom-management-commands.txt b/docs/howto/custom-management-commands.txt
index dadbaf9742..f20252dc0c 100644
--- a/docs/howto/custom-management-commands.txt
+++ b/docs/howto/custom-management-commands.txt
@@ -255,6 +255,19 @@ the :meth:`~BaseCommand.handle` method must be implemented.
super().__init__(*args, **kwargs)
# ...
+.. method:: BaseCommand.create_parser(prog_name, subcommand, **kwargs)
+
+ Returns a ``CommandParser`` instance, which is an
+ :class:`~argparse.ArgumentParser` subclass with a few customizations for
+ Django.
+
+ You can customize the instance by overriding this method and calling
+ ``super()`` with ``kwargs`` of :class:`~argparse.ArgumentParser` parameters.
+
+ .. versionchanged:: 2.2
+
+ ``kwargs`` was added.
+
.. method:: BaseCommand.add_arguments(parser)
Entry point to add parser arguments to handle command line arguments passed
diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py
index e90d29bb0f..eb30065488 100644
--- a/tests/user_commands/tests.py
+++ b/tests/user_commands/tests.py
@@ -220,6 +220,12 @@ class CommandTests(SimpleTestCase):
with self.assertRaisesMessage(CommandError, msg):
management.call_command('subparser', 'test', 12)
+ def test_create_parser_kwargs(self):
+ """BaseCommand.create_parser() passes kwargs to CommandParser."""
+ epilog = 'some epilog text'
+ parser = BaseCommand().create_parser('prog_name', 'subcommand', epilog=epilog)
+ self.assertEqual(parser.epilog, epilog)
+
class CommandRunTests(AdminScriptTestCase):
"""