Standardized indentation in docs/howto/custom-management-commands.txt.

This commit is contained in:
Tim Graham 2015-01-17 13:38:01 -05:00
parent bd93032191
commit 4b8d3bbab5
1 changed files with 72 additions and 76 deletions

View File

@ -69,13 +69,15 @@ look like this::
self.stdout.write('Successfully closed poll "%s"' % poll_id) self.stdout.write('Successfully closed poll "%s"' % poll_id)
Before Django 1.8, management commands were based on the :py:mod:`optparse` .. versionchanged:: 1.8
module, and positional arguments were passed in ``*args`` while optional
arguments were passed in ``**options``. Now that management commands use Before Django 1.8, management commands were based on the :py:mod:`optparse`
:py:mod:`argparse` for argument parsing, all arguments are passed in module, and positional arguments were passed in ``*args`` while optional
``**options`` by default, unless you name your positional arguments to ``args`` arguments were passed in ``**options``. Now that management commands use
(compatibility mode). You are encouraged to exclusively use ``**options`` for :py:mod:`argparse` for argument parsing, all arguments are passed in
new commands. ``**options`` by default, unless you name your positional arguments to
``args`` (compatibility mode). You are encouraged to exclusively use
``**options`` for new commands.
.. _management-commands-output: .. _management-commands-output:
@ -253,7 +255,7 @@ All attributes can be set in your derived class and can be used in
.. attribute:: BaseCommand.missing_args_message .. attribute:: BaseCommand.missing_args_message
.. versionadded:: 1.8 .. versionadded:: 1.8
If your command defines mandatory positional arguments, you can customize If your command defines mandatory positional arguments, you can customize
the message error returned in the case of missing arguments. The default is the message error returned in the case of missing arguments. The default is
@ -266,21 +268,18 @@ All attributes can be set in your derived class and can be used in
.. deprecated:: 1.8 .. deprecated:: 1.8
You should now override the :meth:`~BaseCommand.add_arguments` method to You should now override the :meth:`~BaseCommand.add_arguments` method
add custom arguments accepted by your command. to add custom arguments accepted by your command. See :ref:`the example
See :ref:`the example above <custom-commands-options>`. above <custom-commands-options>`.
.. attribute:: BaseCommand.output_transaction .. attribute:: BaseCommand.output_transaction
A boolean indicating whether the command outputs SQL A boolean indicating whether the command outputs SQL statements; if
statements; if ``True``, the output will automatically be ``True``, the output will automatically be wrapped with ``BEGIN;`` and
wrapped with ``BEGIN;`` and ``COMMIT;``. Default value is ``COMMIT;``. Default value is ``False``.
``False``.
.. attribute:: BaseCommand.requires_system_checks .. attribute:: BaseCommand.requires_system_checks
.. versionadded:: 1.7
A boolean; if ``True``, the entire Django project will be checked for A boolean; if ``True``, the entire Django project will be checked for
potential problems prior to executing the command. Default value is ``True``. potential problems prior to executing the command. Default value is ``True``.
@ -293,15 +292,16 @@ All attributes can be set in your derived class and can be used in
Make sure you know what you are doing if you decide to change the value of Make sure you know what you are doing if you decide to change the value of
this option in your custom command if it creates database content that this option in your custom command if it creates database content that
is locale-sensitive and such content shouldn't contain any translations (like is locale-sensitive and such content shouldn't contain any translations
it happens e.g. with django.contrib.auth permissions) as making the locale (like it happens e.g. with django.contrib.auth permissions) as making the
differ from the de facto default 'en-us' might cause unintended effects. See locale differ from the de facto default 'en-us' might cause unintended
the `Management commands and locales`_ section above for further details. effects. Seethe `Management commands and locales`_ section above for
further details.
This option can't be ``False`` when the This option can't be ``False`` when the
:data:`~BaseCommand.can_import_settings` option is set to ``False`` too :data:`~BaseCommand.can_import_settings` option is set to ``False`` too
because attempting to set the locale needs access to settings. This condition because attempting to set the locale needs access to settings. This
will generate a :class:`CommandError`. condition will generate a :class:`CommandError`.
Methods Methods
------- -------
@ -321,7 +321,7 @@ the :meth:`~BaseCommand.handle` method must be implemented.
.. method:: BaseCommand.add_arguments(parser) .. method:: BaseCommand.add_arguments(parser)
.. versionadded:: 1.8 .. versionadded:: 1.8
Entry point to add parser arguments to handle command line arguments passed Entry point to add parser arguments to handle command line arguments passed
to the command. Custom commands should override this method to add both to the command. Custom commands should override this method to add both
@ -351,7 +351,7 @@ the :meth:`~BaseCommand.handle` method must be implemented.
.. method:: BaseCommand.check(app_configs=None, tags=None, display_num_errors=False) .. method:: BaseCommand.check(app_configs=None, tags=None, display_num_errors=False)
.. versionadded:: 1.7 .. versionadded:: 1.7
Uses the system check framework to inspect the entire Django project for Uses the system check framework to inspect the entire Django project for
potential problems. Serious problems are raised as a :class:`CommandError`; potential problems. Serious problems are raised as a :class:`CommandError`;
@ -363,7 +363,7 @@ the :meth:`~BaseCommand.handle` method must be implemented.
.. method:: BaseCommand.validate(app=None, display_num_errors=False) .. method:: BaseCommand.validate(app=None, display_num_errors=False)
.. deprecated:: 1.7 .. deprecated:: 1.7
Replaced with the :djadmin:`check` command Replaced with the :djadmin:`check` command
If ``app`` is None, then all installed apps are checked for errors. If ``app`` is None, then all installed apps are checked for errors.
@ -390,17 +390,16 @@ each application.
.. class:: LabelCommand .. class:: LabelCommand
A management command which takes one or more arbitrary arguments A management command which takes one or more arbitrary arguments (labels) on
(labels) on the command line, and does something with each of the command line, and does something with each of them.
them.
Rather than implementing :meth:`~BaseCommand.handle`, subclasses must implement Rather than implementing :meth:`~BaseCommand.handle`, subclasses must implement
:meth:`~LabelCommand.handle_label`, which will be called once for each label. :meth:`~LabelCommand.handle_label`, which will be called once for each label.
.. method:: LabelCommand.handle_label(label, **options) .. method:: LabelCommand.handle_label(label, **options)
Perform the command's actions for ``label``, which will be the Perform the command's actions for ``label``, which will be the string as
string as given on the command line. given on the command line.
.. class:: NoArgsCommand .. class:: NoArgsCommand
@ -425,16 +424,13 @@ Command exceptions
.. class:: CommandError .. class:: CommandError
Exception class indicating a problem while executing a management Exception class indicating a problem while executing a management command.
command.
If this exception is raised during the execution of a management If this exception is raised during the execution of a management command from a
command from a command line console, it will be caught and turned into a command line console, it will be caught and turned into a nicely-printed error
nicely-printed error message to the appropriate output stream (i.e., stderr); message to the appropriate output stream (i.e., stderr); as a result, raising
as a result, raising this exception (with a sensible description of the this exception (with a sensible description of the error) is the preferred way
error) is the preferred way to indicate that something has gone to indicate that something has gone wrong in the execution of a command.
wrong in the execution of a command.
If a management command is called from code through If a management command is called from code through :ref:`call_command
:ref:`call_command <call-command>`, it's up to you to catch the exception <call-command>`, it's up to you to catch the exception when needed.
when needed.