Old description:
> Are there any plans to add support for the following?
>
> {{{
> 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('--foo', nargs='+', type=int)
>
> def handle(self, *args, **options):
> pass
> }}}
>
> When calling the above using `call_command`:
>
> {{{
> call_command('call_command_test', foo=[1, 2, 3])
> # Raises: django.core.management.base.CommandError: Error: one of the
> arguments --foo is required
>
> call_command('call_command_test', '--foo=1', '--foo=2', '--foo=3')
> # Option 'foo' will be of value [3]
> }}}
>
> I can't think of any workarounds. Thank you!
New description:
Are there any plans to add support for the following?
{{{
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('--foo', nargs='+', type=int)
def handle(self, *args, **options):
pass
}}}
When calling the above using `call_command`:
{{{
call_command('call_command_test', foo=[1, 2, 3])
# Raises: django.core.management.base.CommandError: Error: one of the
arguments --foo is required
call_command('call_command_test', '--foo=1', '--foo=2', '--foo=3')
# Option 'foo' will be of value [3]
}}}
I can't think of any workarounds other than setting `type=str` (somehow,
that works fine) and coercing manually. Thank you!
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> I can't think of any workarounds other than setting `type=str` (somehow,
> that works fine) and coercing manually. Thank you!
New description:
Are there any plans to add support for the following?
{{{
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('--foo', nargs='+', type=int)
def handle(self, *args, **options):
pass
}}}
When calling the above using `call_command`:
{{{
call_command('call_command_test', foo=[1, 2, 3])
# Raises: django.core.management.base.CommandError: Error: argument --foo:
invalid int value: '[1, 2, 3]'
call_command('call_command_test', '--foo=1', '--foo=2', '--foo=3')
# Option 'foo' will be of value [3]
}}}
I can't think of any workarounds other than setting `type=str` (somehow,
that works fine) and coercing manually. Thank you!
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:2>
Comment (by Mark Gajdosik):
Replying to [comment:3 Hasan Ramezani]:
> I think we need to add proper handling for `required` arguments with
`nargs` as well.
That would always be neat. I couldn't get it to work with the current
{}={} syntax and the list comprehension:
{{{
# Any required arguments which are passed in via **options must be passed
# to parse_args().
for opt in parser_actions:
if (
opt.dest in options and
(opt.required or opt in mutually_exclusive_required_options)
):
parse_args.append(min(opt.option_strings))
value = arg_options[opt.dest]
if isinstance(value, (list, tuple)):
parse_args += map(str, value)
else:
parse_args.append(str(value))
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:4>
* type: Uncategorized => Bug
* component: Uncategorized => Core (Management commands)
* stage: Unreviewed => Accepted
Comment:
Thanks for this report. Yes we should change `parse_args` to the list of
arguments and their values without using the ` = ` syntax, e.g. pass `['--
foo', '1', '2']` instead of `['--foo=1 2']`. All of these calls should be
supported:
{{{
management.call_command('command', '--foo 1 2')
management.call_command('command', foo=[1, 2])
management.call_command('command', foo, [1, 2])
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:5>
* owner: nobody => Hasan Ramezani
* status: new => assigned
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/13620 PR]
I think we shouldn't support `management.call_command('command', '--foo 1
2')`.
I think python `argparse` also doesn't support that.
Please correct me if I get it wrong.
--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:6>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:7>
* needs_better_patch: 1 => 0
Comment:
After reconsideration, I agree that we shouldn't support this, it's not
described in the [https://docs.python.org/3/library/argparse.html#option-
value-syntax "Option value syntax"] and it can be tricky to support all
options.
--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:8>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:9>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"f06beea92999407cc8dad3c47f006b7c727095a6" f06beea9]:
{{{
#!CommitTicketReference repository=""
revision="f06beea92999407cc8dad3c47f006b7c727095a6"
Fixed #32153 -- Fixed management commands when using required list
options.
Thanks Mark Gajdosik for the report and initial patch.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:10>
Comment (by Mark Gajdosik):
Thank you both for expediting this so quickly!
--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:11>