#32047: Management commands: Mutually exclusive argument groups don't work with
'store_true' arguments & call_command
-------------------------------------+-------------------------------------
Reporter: Mark | Owner: nobody
Gajdosik |
Type: Bug | Status: new
Component: Core | Version: 3.1
(Management commands) | Keywords: call_command,
Severity: Normal | exclusive group, store_true,
Triage Stage: | action, explicit, error
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have the following management command (called `test.py`):
{{{
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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/055.d9c2fe28cacf89991d429f33ca0d2c9c%40djangoproject.com.