{{{
from django.core.management import BaseCommand
class Command(BaseCommand):
def add_arguments(self, parser):
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--value', type=str)
group.add_argument('--flag', action='store_true')
def handle(self, *args, **options):
pass
}}}
Running `./manage.py test --flag` or `./manage.py --value foo`, the
command works as expected. Using `call_command` to call the command from
my code fails if I pass `--flag`, e.g.:
{{{
from django.core.management import call_command
call_command('test', flag=True)
}}}
This gets me the following traceback:
{{{
argparse.ArgumentError: argument --flag: ignored explicit argument 'True'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/mark/.local/share/virtualenvs/backend-KlVhwr95/lib/python3.8
/site-packages/django/core/management/__init__.py", line 147, in
call_command
defaults = parser.parse_args(args=parse_args)
File "/home/mark/.local/share/virtualenvs/backend-KlVhwr95/lib/python3.8
/site-packages/django/core/management/base.py", line 57, in parse_args
return super().parse_args(args, namespace)
File "/usr/lib/python3.8/argparse.py", line 1780, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python3.8/argparse.py", line 1819, in parse_known_args
self.error(str(err))
File "/home/mark/.local/share/virtualenvs/backend-KlVhwr95/lib/python3.8
/site-packages/django/core/management/base.py", line 63, in error
raise CommandError("Error: %s" % message)
django.core.management.base.CommandError: Error: argument --flag: ignored
explicit argument 'True'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32047>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Mark Gajdosik):
Probably stating the obvious, but I get the same error when running the
command from console and passing value to the `--flag` explicitly, e.g.
`./manage.py test --flag=True`.
--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:1>
Comment (by felixxm):
Mark's response via email:
> 1. I'm not using your trac as a support channel. This was the first time
I used it and I doubt I'll use it again.
> 2. RE the actual issue: docs for call_comamnd clearly state: "command
options that take no arguments are passed as keywords with True or False".
This works fine, until the arg is placed inside a group. The docs don't
mention this.
>
> I understand that Django devs are busy, but at least try understanding
what the ticket is saying before rushing to close it. Thank you.
>
> Kind Regards,
> Mark Gajdosik
--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:3>
* cc: Hasan Ramezani, Alex Tomic (added)
* stage: Unreviewed => Accepted
Comment:
> 1. I'm not using your trac as a support channel. This was the first time
I used it and I doubt I'll use it again.
I'm sorry you had a bad experience. From the ticket description I assumed
that you're trying to find a way to pass a boolean argument, that's why I
took it as a support question.
> 2. RE the actual issue: docs for call_comamnd clearly state: "command
options that take no arguments are passed as keywords with True or False".
This works fine, until the arg is placed inside a group. The docs don't
mention this.
Good catch, I can reproduce this issue for a required mutually exclusive
groups with boolean arguments. We should probably change
[https://github.com/django/django/blob/91669cc566918fde4fe98f867cf4c406f64504e6/django/core/management/__init__.py#L140-L146
preparing boolean arguments] before passing them to `parse_args()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:4>
* status: closed => new
* resolution: invalid =>
--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:5>
--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:6>
* owner: nobody => Hasan Ramezani
* status: new => assigned
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:8>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:9>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"6eb3f53bdd5089e3ab229780fe339fecfc30a7ee" 6eb3f53b]:
{{{
#!CommitTicketReference repository=""
revision="6eb3f53bdd5089e3ab229780fe339fecfc30a7ee"
Fixed #32047 -- Fixed call_command() crash if a constant option from
required mutually exclusive group is passed in options.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:11>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"efe74fff25e1e85402ecb73d80eea7625246f4ea" efe74ff]:
{{{
#!CommitTicketReference repository=""
revision="efe74fff25e1e85402ecb73d80eea7625246f4ea"
Refs #32047 -- Added test for using call_command() with constant required
options.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:12>