#33430 closed Bug (invalid)
Calling command via `call_command` with option that is `required` and has set `dest` leads to an error
| Reported by: | Adam Mičuda | Owned by: | ravi kunwar |
|---|---|---|---|
| Component: | Core (Management commands) | Version: | 3.2 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Calling command via call_command with option that is required and has set dest leads to an error:
"Error: the following arguments are required: <option>"
Code to simulate behaviour:
from django.core.management.base import BaseCommand, CommandParser
class Command(BaseCommand):
def add_arguments(self, parser: CommandParser):
# parser.add_argument('--from') # OK
# parser.add_argument('--from', dest='from_') # OK
# parser.add_argument('--from', required=True) # OK
parser.add_argument('--from', required=True, dest='from_') # leads to an error
def handle(self, *args, **options):
...
and calling the command:
call_command('bug_report', **{ 'from': '2022-01-11' })
Change History (5)
comment:1 by , 4 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 4 years ago
comment:3 by , 4 years ago
| Resolution: | → invalid |
|---|---|
| Status: | assigned → closed |
You should pass dest in options as documented:
Some command options have different names when using
call_command()instead ofdjango-adminormanage.py. For example,django-admin createsuperuser --no-inputtranslates tocall_command('createsuperuser', interactive=False). To find what keyword argument name to use forcall_command(), check the command’s source code for thedestargument passed toparser.add_argument().
comment:4 by , 4 years ago
Hi @Ravi,
I'm sorry for not providing these informations. I use Python 3.9.6 and Django 3.2.10.
Hi @Mariusz,
Oh, you are right. Thank you. I have one note to this: I think the behaviour is kind of confusing from dev experience.
Lets have an option with the dest set, e.g.
parser.add_argument('--from', dest='from_')
It is possible to call call_command as
call_command('bug_report', **{ 'from': '2022-01-11' })
and
call_command('bug_report', **{ 'from_': '2022-01-11' })
either. But if the option is required the first call is not possible. I believe the behaviour should be consistent regardless of whether the option is required or not. What do you think?
comment:5 by , 4 years ago
But if the option is required the first call is not possible. I believe the behaviour should be consistent regardless of whether the option is required or not. What do you think?
The current behavior is consistent. In the first call, i.e.
call_command('bug_report', **{ 'from': '2022-01-11' })
from is simply ignored, so an error is raised only when it's required.
hey Adam Mičuda
whic python and django version you are using ??