Opened 16 years ago

Closed 13 years ago

Last modified 13 years ago

#10080 closed Bug (fixed)

call_command doesn't take into account command's default options

Reported by: Alexander Koshelev Owned by: Alexander Koshelev
Component: Core (Other) 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

When I want to use manage.py command from python I must specify all options on which this command is depend.

Almost everyone custom command's author write execute method something like this:

class Command(BaseCommand):
    option_list = [make_option("--some_option", default="foobar")]

    def execute(self, *args, **options):
        #...
        some_option = options["some_option"]
        #...

And when I call it using call_command without some_option is specified I get the KeyError. But it works fine in command line because optparse provides default values.

>>> call_command("some_command")
Traceback (most recent call last):
...
KeyError: 'some_option'

Patch solves this inconsistence.

Attachments (2)

10080.diff (2.4 KB ) - added by Alexander Koshelev 16 years ago.
Patch
10080-2.diff (1.6 KB ) - added by Claude Paroz 13 years ago.
Also fill defaults dict with options without defaults

Download all attachments as: .zip

Change History (12)

by Alexander Koshelev, 16 years ago

Attachment: 10080.diff added

Patch

comment:1 by (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:2 by Jacob, 16 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 by Jacob, 16 years ago

Resolution: fixed
Status: newclosed

(In [10401]) [1.0.X] Fixed #10080: call_command now takes option defaults into account, sparing individual commands from any difference between call_command and being run from the shell. Thanks, Alex Koshelev. Backport of 10400 from trunk.

comment:4 by bjourne, 14 years ago

Resolution: fixed
Status: closedreopened

The patch solves the problem if the option has a default value, it still doesn't work if no default value is specified. In those cases, optparse sets the options value to None but call_command plainly excludes it which leads to problems with some commands. This update makes it work for me:

    defaults = {}
    for o in klass.option_list:
        v = o.default
        if v is NO_DEFAULT:
            v = None
        defaults[o.dest] = v

comment:5 by anonymous, 14 years ago

milestone: 1.1

comment:6 by Chris Beaven, 14 years ago

Needs tests: set
Severity: Normal
Type: Bug

by Claude Paroz, 13 years ago

Attachment: 10080-2.diff added

Also fill defaults dict with options without defaults

comment:7 by Claude Paroz, 13 years ago

Easy pickings: unset
Needs tests: unset
UI/UX: unset

comment:8 by Jannis Leidel, 13 years ago

Triage Stage: AcceptedReady for checkin

comment:9 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: reopenedclosed

In [17467]:

Fixed #10080 -- Slightly extended the fix made in r10401 by also taking command line options into account that don't have have a default set. Thanks, Claude Paroz.

comment:10 by Ramiro Morales, 13 years ago

In [17678]:

Fixed #17820 -- Fixed more occurrences of redundant handling of management commands options.

They had been missen in the first attempts or had been introduced afterwards.

Refs #13760, #10080, #17799.

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