#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 , 10 years ago
| Component: | Testing framework → Documentation |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Cleanup/optimization |
comment:2 by , 10 years ago
| Has patch: | set |
|---|---|
| Summary: | call_command doesn't raise same errors from option parsing as running command → Clarify call_command()'s handling of args and options |
Makes sense, Claude. I expanded that a bit with some examples: PR.
comment:3 by , 10 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Thanks for the nice worded patch.
Note:
See TracTickets
for help on using tickets.
This could be clearer in the docs. Something like that?
docs/ref/django-admin.txt
.