#23306 closed Uncategorized (worksforme)
Cannot call a command with --name option
| Reported by: | Raffaele Salmaso | Owned by: | nobody |
|---|---|---|---|
| Component: | Core (Management commands) | Version: | |
| Severity: | Normal | Keywords: | |
| Cc: | raffaele.salmaso@… | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
call_command use (name, *args, **options) as signature, making impossible to call a command with --name option as
call_command('my_cmd', name='my_fancy_name')
Change History (5)
comment:1 by , 11 years ago
comment:2 by , 11 years ago
| Resolution: | → worksforme |
|---|---|
| Status: | new → closed |
This is not true for current master (see [2cc8ffe2580080]) where you should now be able to call call_command('my_cmd', '--name', 'my_fancy_name') (https://docs.djangoproject.com/en/dev/ref/django-admin/#call-command).
In 1.7 though, I'd suggest using a temporary workaround by "manually" instanciating the command and calling execute. Feel free to request more help if this is not clear for you.
comment:3 by , 11 years ago
Too recent that I've missed :D
But I've some problems running it as call_command("makemigrations", "migrations", "--empty", "--verbosity", "0") (tests/migrations/test_commands.py:279)
test_makemigrations_empty_migration (migrations.test_commands.MakeMigrationsTests) ... App '--verbosity' could not be found. Is it in INSTALLED_APPS? App '0' could not be found. Is it in INSTALLED_APPS? App '--empty' could not be found. Is it in INSTALLED_APPS? ERROR
[tested against dfde0b3b063c0ec511fe6d58789468b6c229c59b]
It seems that the "error" is the first argument ("migrations") because call_command("makemigrations", "--help") works fine. It seems a problem with makemigrations command itself. I'll dig it a bit and I'll open a new ticket.
comment:4 by , 11 years ago
That's a good catch, you spotted a flaw in my recent patch. The call_command code misses the part that begins with # Move positional args out of options to mimic legacy optparse in the run_from_argv code. I'd be grateful if you would open a ticket for that.
comment:5 by , 11 years ago
Yep, I've just found it :)
Tomorrow I'll open a ticket and a PR (added some tests)
Working on https://code.djangoproject.com/ticket/23302 I'm adding some tests to migrations/test_commands and hits a limitation with call_command: cannot pass name='xyz' as option.
The more compatible way to call it is to have another function with desired signature. So in https://github.com/rsalmaso/django/tree/23306 (PR https://github.com/django/django/pull/3079) I've added a new function
exec_command(name, args=None, options=None)(code is the same of old call_command).Needs some docs if can be accepted.
(My first PR, hope techically is all ok, I'm fighting with git more than I though)