Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#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 ravi kunwar, 2 years ago

Owner: changed from nobody to ravi kunwar
Status: newassigned

comment:2 by ravi kunwar, 2 years ago

hey Adam Mičuda
whic python and django version you are using ??

comment:3 by Mariusz Felisiak, 2 years ago

Resolution: invalid
Status: assignedclosed

You should pass dest in options as documented:

Some command options have different names when using call_command() instead of django-admin or manage.py. For example, django-admin createsuperuser --no-input translates to call_command('createsuperuser', interactive=False). To find what keyword argument name to use for call_command(), check the command’s source code for the dest argument passed to parser.add_argument().

comment:4 by Adam Mičuda, 2 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 Mariusz Felisiak, 2 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.

Note: See TracTickets for help on using tickets.
Back to Top