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?
--
Ticket URL: <https://code.djangoproject.com/ticket/26294>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* component: Testing framework => Documentation
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
This could be clearer in the docs. Something like that?
{{{#!diff
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index 6a7752f..0a5de46 100644
--- a/docs/ref/django-admin.txt
+++ b/docs/ref/django-admin.txt
@@ -1763,10 +1763,11 @@ To call a management command from code use
``call_command``.
the name of the command to call.
``*args``
- a list of arguments accepted by the command.
+ a list of arguments accepted by the command and fed to the parser.
``**options``
- named options accepted on the command-line.
+ named options accepted on the command-line. Those will be directly
passed to
+ the command without triggering the argument parser.
Examples::
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26294#comment:1>
* has_patch: 0 => 1
Comment:
Makes sense, Claude. I expanded that a bit with some examples:
[https://github.com/django/django/pull/6284 PR].
--
Ticket URL: <https://code.djangoproject.com/ticket/26294#comment:2>
* stage: Accepted => Ready for checkin
Comment:
Thanks for the nice worded patch.
--
Ticket URL: <https://code.djangoproject.com/ticket/26294#comment:3>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"5695c142d282f4681a8d43bb55ec49f11f3fc40e" 5695c142]:
{{{
#!CommitTicketReference repository=""
revision="5695c142d282f4681a8d43bb55ec49f11f3fc40e"
Fixed #26294 -- Clarified call_command()'s handling of args and options.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26294#comment:4>
Comment (by Tim Graham <timograham@…>):
In [changeset:"7b2ee75745a5304b222b71b3273ff14a40da47f4" 7b2ee757]:
{{{
#!CommitTicketReference repository=""
revision="7b2ee75745a5304b222b71b3273ff14a40da47f4"
[1.9.x] Fixed #26294 -- Clarified call_command()'s handling of args and
options.
Backport of 5695c142d282f4681a8d43bb55ec49f11f3fc40e from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26294#comment:5>
Comment (by Tim Graham <timograham@…>):
In [changeset:"a30482a4b6e5d13e7325238fdc902fbca7e714f4" a30482a4]:
{{{
#!CommitTicketReference repository=""
revision="a30482a4b6e5d13e7325238fdc902fbca7e714f4"
Refs #26294 -- Fixed typo in docs/ref/django-admin.txt.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26294#comment:6>
Comment (by Tim Graham <timograham@…>):
In [changeset:"ceb6a64f2f5d8e4a5309d9558f89a6d1f0b6308d" ceb6a64f]:
{{{
#!CommitTicketReference repository=""
revision="ceb6a64f2f5d8e4a5309d9558f89a6d1f0b6308d"
[1.11.x] Refs #26294 -- Fixed typo in docs/ref/django-admin.txt.
Backport of a30482a4b6e5d13e7325238fdc902fbca7e714f4 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26294#comment:7>
Comment (by Tim Graham <timograham@…>):
In [changeset:"4cb51412919a0aacdb26c87d409ab3afaf53ca18" 4cb51412]:
{{{
#!CommitTicketReference repository=""
revision="4cb51412919a0aacdb26c87d409ab3afaf53ca18"
[1.10.x] Refs #26294 -- Fixed typo in docs/ref/django-admin.txt.
Backport of a30482a4b6e5d13e7325238fdc902fbca7e714f4 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26294#comment:8>