Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#26294 closed Cleanup/optimization (fixed)

Clarify call_command()'s handling of args and options

Reported by: Malcolm Box Owned by: nobody
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Minimal example:

management/commands/bug.py

class Command(BaseCommand):
   def add_arguments(self, parser):
       parser.add_argument('-n', action='store', dest='number', type=int)

   def handle(self, **options):
       int(options['number'])

Execute as:

./manage.py bug -n nan

error: argument -n: invalid int value: 'nan'

Test using call_command:

call_command('bug', n='nan')

Raises a ValueError trying to convert options['number']

The core problem seems to be that kwargs to call_command aren't passed through the argparse command processing in the way that *args is.

There's a work-around, which is to use the call_command('bug', '-n nan') format - perhaps this could be called out in the docs?

Change History (8)

comment:1 by Claude Paroz, 8 years ago

Component: Testing frameworkDocumentation
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

This could be clearer in the docs. Something like that?

  • docs/ref/django-admin.txt

    diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
    index 6a7752f..0a5de46 100644
    a b To call a management command from code use ``call_command``.  
    17631763  the name of the command to call.
    17641764
    17651765``*args``
    1766   a list of arguments accepted by the command.
     1766  a list of arguments accepted by the command and fed to the parser.
    17671767
    17681768``**options``
    1769   named options accepted on the command-line.
     1769  named options accepted on the command-line. Those will be directly passed to
     1770  the command without triggering the argument parser.
    17701771
    17711772Examples::

comment:2 by Tim Graham, 8 years ago

Has patch: set
Summary: call_command doesn't raise same errors from option parsing as running commandClarify call_command()'s handling of args and options

Makes sense, Claude. I expanded that a bit with some examples: PR.

comment:3 by Claude Paroz, 8 years ago

Triage Stage: AcceptedReady for checkin

Thanks for the nice worded patch.

comment:4 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In 5695c142:

Fixed #26294 -- Clarified call_command()'s handling of args and options.

comment:5 by Tim Graham <timograham@…>, 8 years ago

In 7b2ee757:

[1.9.x] Fixed #26294 -- Clarified call_command()'s handling of args and options.

Backport of 5695c142d282f4681a8d43bb55ec49f11f3fc40e from master

comment:6 by Tim Graham <timograham@…>, 7 years ago

In a30482a4:

Refs #26294 -- Fixed typo in docs/ref/django-admin.txt.

comment:7 by Tim Graham <timograham@…>, 7 years ago

In ceb6a64f:

[1.11.x] Refs #26294 -- Fixed typo in docs/ref/django-admin.txt.

Backport of a30482a4b6e5d13e7325238fdc902fbca7e714f4 from master

comment:8 by Tim Graham <timograham@…>, 7 years ago

In 4cb51412:

[1.10.x] Refs #26294 -- Fixed typo in docs/ref/django-admin.txt.

Backport of a30482a4b6e5d13e7325238fdc902fbca7e714f4 from master

Note: See TracTickets for help on using tickets.
Back to Top