Re: [Django] #32153: Support for list arguments inside exclusive required groups

4 views
Skip to first unread message

Django

unread,
Oct 27, 2020, 2:08:45 PM10/27/20
to django-...@googlegroups.com
#32153: Support for list arguments inside exclusive required groups
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage:
exclusive group, nargs, error | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Mark Gajdosik:

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.

Django

unread,
Oct 27, 2020, 2:15:33 PM10/27/20
to django-...@googlegroups.com

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>

Django

unread,
Oct 27, 2020, 7:43:19 PM10/27/20
to django-...@googlegroups.com
#32153: Support for list arguments inside exclusive required groups
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage:
exclusive group, nargs, error | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Oct 28, 2020, 3:09:31 AM10/28/20
to django-...@googlegroups.com
#32153: Support for list arguments inside exclusive required groups
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: nobody
Type: Bug | Status: new
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage: Accepted
exclusive group, nargs, error |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Oct 29, 2020, 3:50:03 PM10/29/20
to django-...@googlegroups.com
#32153: Support for list arguments inside exclusive required groups
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage: Accepted
exclusive group, nargs, error |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* 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>

Django

unread,
Oct 30, 2020, 4:32:53 AM10/30/20
to django-...@googlegroups.com
#32153: Support for list arguments inside exclusive required groups
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage: Accepted
exclusive group, nargs, error |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:7>

Django

unread,
Oct 30, 2020, 6:37:28 AM10/30/20
to django-...@googlegroups.com
#32153: Support for list arguments inside exclusive required groups
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage: Accepted
exclusive group, nargs, error |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Oct 30, 2020, 7:07:36 AM10/30/20
to django-...@googlegroups.com
#32153: Support for list arguments inside exclusive required groups
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage: Ready for
exclusive group, nargs, error | checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:9>

Django

unread,
Oct 30, 2020, 8:09:08 AM10/30/20
to django-...@googlegroups.com
#32153: Support for list arguments inside exclusive required groups
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: Hasan
| Ramezani
Type: Bug | Status: closed

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution: fixed

Keywords: call_command, | Triage Stage: Ready for
exclusive group, nargs, error | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Django

unread,
Oct 30, 2020, 11:34:54 AM10/30/20
to django-...@googlegroups.com
#32153: Support for list arguments inside exclusive required groups
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: Hasan
| Ramezani
Type: Bug | Status: closed
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution: fixed
Keywords: call_command, | Triage Stage: Ready for
exclusive group, nargs, error | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mark Gajdosik):

Thank you both for expediting this so quickly!

--
Ticket URL: <https://code.djangoproject.com/ticket/32153#comment:11>

Reply all
Reply to author
Forward
0 new messages