Opened 7 years ago

Closed 7 years ago

Last modified 6 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 Changed 7 years ago by Claude Paroz

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 Changed 7 years ago by Tim Graham

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 Changed 7 years ago by Claude Paroz

Triage Stage: AcceptedReady for checkin

Thanks for the nice worded patch.

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

Resolution: fixed
Status: newclosed

In 5695c142:

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

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

In 7b2ee757:

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

Backport of 5695c142d282f4681a8d43bb55ec49f11f3fc40e from master

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

In a30482a4:

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

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

In ceb6a64f:

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

Backport of a30482a4b6e5d13e7325238fdc902fbca7e714f4 from master

comment:8 Changed 6 years ago by Tim Graham <timograham@…>

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