From 33173e98c1c9bbf0fd7c9ad3592f85c4be505dfb Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Sun, 5 Apr 2009 17:28:09 +0000 Subject: [PATCH] [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. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10401 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/__init__.py | 15 +++++++++++++-- .../user_commands/management/commands/dance.py | 7 ++++++- tests/modeltests/user_commands/models.py | 7 +++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index 21fad3fc51..66c8e1a56d 100644 --- a/django/core/management/__init__.py +++ b/django/core/management/__init__.py @@ -1,6 +1,6 @@ import os import sys -from optparse import OptionParser +from optparse import OptionParser, NO_DEFAULT import imp import django @@ -146,6 +146,7 @@ def call_command(name, *args, **options): call_command('shell', plain=True) call_command('sqlall', 'myapp') """ + # Load the command object. try: app_name = get_commands()[name] if isinstance(app_name, BaseCommand): @@ -155,7 +156,17 @@ def call_command(name, *args, **options): klass = load_command_class(app_name, name) except KeyError: raise CommandError, "Unknown command: %r" % name - return klass.execute(*args, **options) + + # Grab out a list of defaults from the options. optparse does this for us + # when the script runs from the command line, but since call_command can + # be called programatically, we need to simulate the loading and handling + # of defaults (see #10080 for details). + defaults = dict([(o.dest, o.default) + for o in klass.option_list + if o.default is not NO_DEFAULT]) + defaults.update(options) + + return klass.execute(*args, **defaults) class LaxOptionParser(OptionParser): """ diff --git a/tests/modeltests/user_commands/management/commands/dance.py b/tests/modeltests/user_commands/management/commands/dance.py index 5886cd1d8f..a504d486d6 100644 --- a/tests/modeltests/user_commands/management/commands/dance.py +++ b/tests/modeltests/user_commands/management/commands/dance.py @@ -1,3 +1,4 @@ +from optparse import make_option from django.core.management.base import BaseCommand class Command(BaseCommand): @@ -5,5 +6,9 @@ class Command(BaseCommand): args = '' requires_model_validation = True + option_list =[ + make_option("-s", "--style", default="Rock'n'Roll") + ] + def handle(self, *args, **options): - print "I don't feel like dancing." \ No newline at end of file + print "I don't feel like dancing %s." % options["style"] diff --git a/tests/modeltests/user_commands/models.py b/tests/modeltests/user_commands/models.py index 8dd7205f98..10ccdb8e2c 100644 --- a/tests/modeltests/user_commands/models.py +++ b/tests/modeltests/user_commands/models.py @@ -17,8 +17,8 @@ __test__ = {'API_TESTS': """ >>> from django.core import management # Invoke a simple user-defined command ->>> management.call_command('dance') -I don't feel like dancing. +>>> management.call_command('dance', style="Jive") +I don't feel like dancing Jive. # Invoke a command that doesn't exist >>> management.call_command('explode') @@ -26,5 +26,8 @@ Traceback (most recent call last): ... CommandError: Unknown command: 'explode' +# Invoke a command with default option `style` +>>> management.call_command('dance') +I don't feel like dancing Rock'n'Roll. """}