"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' })
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33430>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => ravi kunwar
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/33430#comment:1>
Comment (by ravi kunwar):
hey Adam Mičuda
whic python and django version you are using ??
--
Ticket URL: <https://code.djangoproject.com/ticket/33430#comment:2>
* status: assigned => closed
* resolution: => invalid
Comment:
You should pass `dest` in `options` as
[https://docs.djangoproject.com/en/dev/ref/django-admin/#running-
management-commands-from-your-code 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()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/33430#comment:3>
Comment (by Adam Mičuda):
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?
--
Ticket URL: <https://code.djangoproject.com/ticket/33430#comment:4>
Comment (by Mariusz Felisiak):
> 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/33430#comment:5>